-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_config.go
65 lines (52 loc) · 1.7 KB
/
url_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package short
import (
"fmt"
"time"
)
// UrlConfig may be used to customize the way a url is shortened.
// A UrlConfig instance can be created by calling `DefaultUrlConfig()`.
type UrlConfig interface {
getConfig() *urlConfig
// WithAlias sets a short url alias instead of generating a random one.
// E.g.: if the alias is `tastypizzas` the shortened url could be https://link.com/tastypizzas
WithAlias(alias string) UrlConfig
// WithOverrideAlias set the override configuration.
// When override is `true` it will insert a new or override an existing shortened url.
// This field is ignored when there is no alias.
WithOverrideAlias(override bool) UrlConfig
// WithExpirationDate sets an expiration date for the shortened url.
// Once the expiration date has expired the url becomes invalid or allocated for other urls.
WithExpirationDate(expriationDate time.Time) UrlConfig
}
type urlConfig struct {
alias string
overrideAlias bool
expirationDate *time.Time
err error
}
// DefaultConfig returns a configuration with default values.
// default alias: "" (empty string).
// default overrideAlias: false.
// default expirationDate: no expiration.
func DefaultUrlConfig() UrlConfig {
return &urlConfig{}
}
func (u *urlConfig) getConfig() *urlConfig {
return u
}
func (u urlConfig) WithAlias(alias string) UrlConfig {
if !isAlphaNumeric(alias) {
u.err = fmt.Errorf("alias %s contains non-alphanumeric characters", alias)
} else {
u.alias = alias
}
return &u
}
func (u urlConfig) WithOverrideAlias(override bool) UrlConfig {
u.overrideAlias = override
return &u
}
func (u urlConfig) WithExpirationDate(expriationDate time.Time) UrlConfig {
u.expirationDate = &expriationDate
return &u
}