short_url.go 545 B

123456789101112131415161718192021222324252627282930
  1. package domain
  2. import (
  3. "math/rand"
  4. "strconv"
  5. )
  6. type ShortURL struct {
  7. URL
  8. target TargetURL
  9. }
  10. func (su ShortURL) Target() TargetURL {
  11. return su.target
  12. }
  13. /*
  14. MakeShortURL creates a ShortURL instance from a given RedirectURL/target pair.
  15. */
  16. func NewUnspecifiedShortURL(target TargetURL) (ShortURL, error) {
  17. var su ShortURL
  18. if target.IsEmpty() {
  19. return su, MakeError(nil, TargetInvalid.Other, "empty URL")
  20. }
  21. // FIXME: will cause collisions.
  22. url := URL(strconv.Itoa(rand.Intn(1 << 31)))
  23. su = ShortURL{url, target}
  24. return su, nil
  25. }