|
@@ -0,0 +1,26 @@
|
|
|
|
+package domain
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "errors"
|
|
|
|
+ "math/rand"
|
|
|
|
+ "strconv"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type ShortURL struct {
|
|
|
|
+ URL
|
|
|
|
+ TargetURL
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+MakeShortURL creates a ShortURL instance from a given URL/target pair.
|
|
|
|
+*/
|
|
|
|
+func NewUnspecifiedShortURL(targetURL TargetURL) (*ShortURL, error) {
|
|
|
|
+ if targetURL.IsEmpty() {
|
|
|
|
+ return nil, errors.New("cannot target empty URL")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // FIXME: will cause collisions.
|
|
|
|
+ url := URL(strconv.Itoa(rand.Intn(1 << 31)))
|
|
|
|
+ u := ShortURL{url, targetURL}
|
|
|
|
+ return &u, nil
|
|
|
|
+}
|