-
Notifications
You must be signed in to change notification settings - Fork 63
/
hooks.go
154 lines (135 loc) · 3.58 KB
/
hooks.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package config
import (
"encoding"
stderrors "errors"
"fmt"
"net"
"net/url"
"reflect"
"github.com/asaskevich/govalidator"
"github.com/c2h5oh/datasize"
"github.com/facebookgo/stack"
"github.com/pkg/errors"
"github.com/yandex/pandora/lib/confutil"
"github.com/yandex/pandora/lib/tag"
"go.uber.org/zap"
)
var InvalidURLError = errors.New("string is not valid URL")
var (
urlPtrType = reflect.TypeOf(&url.URL{})
urlType = reflect.TypeOf(url.URL{})
)
// StringToURLHook converts string to url.URL or *url.URL
func StringToURLHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != urlPtrType && t != urlType {
return data, nil
}
str := data.(string)
if !govalidator.IsURL(str) { // checks more than url.Parse
return nil, errors.WithStack(InvalidURLError)
}
urlPtr, err := url.Parse(str)
if err != nil {
return nil, errors.WithStack(err)
}
if t == urlType {
return *urlPtr, nil
}
return urlPtr, nil
}
var ErrInvalidIP = stderrors.New("string is not valid IP")
// StringToIPHook converts string to net.IP
func StringToIPHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(net.IP{}) {
return data, nil
}
str := data.(string)
ip := net.ParseIP(str)
if ip == nil {
return nil, errors.WithStack(ErrInvalidIP)
}
return ip, nil
}
// StringToDataSizeHook converts string to datasize.Single
func StringToDataSizeHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(datasize.B) {
return data, nil
}
var size datasize.ByteSize
err := size.UnmarshalText([]byte(data.(string)))
return size, err
}
var textUnmarshallerType = func() reflect.Type {
var ptr *encoding.TextUnmarshaler
return reflect.TypeOf(ptr).Elem()
}
func TextUnmarshallerHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t.Implements(textUnmarshallerType()) {
val := reflect.Zero(t)
if t.Kind() == reflect.Ptr {
val = reflect.New(t.Elem())
}
err := unmarhsallText(val, data)
return val.Interface(), err
}
if reflect.PtrTo(t).Implements(textUnmarshallerType()) {
val := reflect.New(t)
err := unmarhsallText(val, data)
return val.Elem().Interface(), err
}
return data, nil
}
func unmarhsallText(v reflect.Value, data interface{}) error {
unmarshaller := v.Interface().(encoding.TextUnmarshaler)
// unmarshaller.UnmarshalText([]byte(data.(string)))
err := unmarshaller.UnmarshalText([]byte(data.(string)))
return err
}
// DebugHook used to debug config decode.
func DebugHook(f reflect.Type, t reflect.Type, data interface{}) (p interface{}, err error) {
p, err = data, nil
if !tag.Debug {
return
}
callers := stack.Callers(2)
var decodeCallers int
for _, caller := range callers {
if caller.Name == "(*Decoder).decode" {
decodeCallers++
}
}
zap.L().Debug("Config decode",
zap.Int("depth", decodeCallers),
zap.Stringer("type", t),
zap.Stringer("from", f),
zap.String("data", fmt.Sprint(data)),
)
return
}
// VariableInjectHook injects values into ${VAR_NAME} placeholders
func VariableInjectHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
str := data.(string)
res, err := confutil.ResolveCustomTags(str, t)
if err == confutil.ErrNoTagsFound {
return data, nil
}
if err != nil {
return data, err
}
return res, nil
}