-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
90 lines (74 loc) · 1.79 KB
/
url.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package config
import (
"fmt"
"net/url"
"reflect"
"regexp"
)
var urlType = reflect.TypeOf(url.URL{})
type urlSetter struct {
val *url.URL
tag reflect.StructTag
}
func (us *urlSetter) String() string {
if us.val == nil {
return (&url.URL{}).String()
}
return us.val.String()
}
func (us *urlSetter) Set(val string) error {
uval, err := url.Parse(val)
if err != nil {
return &ConversionError{Value: val, ToType: urlType}
}
pairs := [][2]string{
[2]string{"scheme", uval.Scheme},
[2]string{"host", uval.Host},
[2]string{"path", uval.Path},
}
for i := range pairs {
key, s := pairs[i][0], pairs[i][1]
if tag := us.tag.Get(key); tag != "" {
r, err := regexp.Compile(tag)
if err != nil {
return &ValidationError{Value: uval, Message: err.Error()}
}
if !r.MatchString(s) {
msg := fmt.Sprintf(
"'%s' did not match regular expression '%s'", s, r,
)
return &ValidationError{Value: val, Message: msg}
}
}
}
*us.val = *uval
return nil
}
func (*urlSetter) SetInt(val int64) error {
return &ConversionError{Value: val, ToType: urlType}
}
func (*urlSetter) SetUint(val uint64) error {
return &ConversionError{Value: val, ToType: urlType}
}
func (*urlSetter) SetFloat(val float64) error {
return &ConversionError{Value: val, ToType: urlType}
}
func (*urlSetter) SetBool(val bool) error {
return &ConversionError{Value: val, ToType: urlType}
}
func (us *urlSetter) Get() interface{} {
if us.val == nil {
return new(*url.URL)
}
return *us.val
}
type urlSetterCreator struct{}
func (urlSetterCreator) Type() reflect.Type {
return urlType
}
func (urlSetterCreator) Setter(val reflect.Value, tag reflect.StructTag) Setter {
return &urlSetter{val: val.Addr().Interface().(*url.URL), tag: tag}
}
func init() {
DefaultSetterRegistry.Add(urlSetterCreator{})
}