equal
deleted
inserted
replaced
1 package mapstructure |
1 package mapstructure |
2 |
2 |
3 import ( |
3 import ( |
4 "errors" |
4 "errors" |
|
5 "fmt" |
|
6 "net" |
5 "reflect" |
7 "reflect" |
6 "strconv" |
8 "strconv" |
7 "strings" |
9 "strings" |
8 "time" |
10 "time" |
9 ) |
11 ) |
110 return data, nil |
112 return data, nil |
111 } |
113 } |
112 |
114 |
113 // Convert it by parsing |
115 // Convert it by parsing |
114 return time.ParseDuration(data.(string)) |
116 return time.ParseDuration(data.(string)) |
|
117 } |
|
118 } |
|
119 |
|
120 // StringToIPHookFunc returns a DecodeHookFunc that converts |
|
121 // strings to net.IP |
|
122 func StringToIPHookFunc() DecodeHookFunc { |
|
123 return func( |
|
124 f reflect.Type, |
|
125 t reflect.Type, |
|
126 data interface{}) (interface{}, error) { |
|
127 if f.Kind() != reflect.String { |
|
128 return data, nil |
|
129 } |
|
130 if t != reflect.TypeOf(net.IP{}) { |
|
131 return data, nil |
|
132 } |
|
133 |
|
134 // Convert it by parsing |
|
135 ip := net.ParseIP(data.(string)) |
|
136 if ip == nil { |
|
137 return net.IP{}, fmt.Errorf("failed parsing ip %v", data) |
|
138 } |
|
139 |
|
140 return ip, nil |
|
141 } |
|
142 } |
|
143 |
|
144 // StringToIPNetHookFunc returns a DecodeHookFunc that converts |
|
145 // strings to net.IPNet |
|
146 func StringToIPNetHookFunc() DecodeHookFunc { |
|
147 return func( |
|
148 f reflect.Type, |
|
149 t reflect.Type, |
|
150 data interface{}) (interface{}, error) { |
|
151 if f.Kind() != reflect.String { |
|
152 return data, nil |
|
153 } |
|
154 if t != reflect.TypeOf(net.IPNet{}) { |
|
155 return data, nil |
|
156 } |
|
157 |
|
158 // Convert it by parsing |
|
159 _, net, err := net.ParseCIDR(data.(string)) |
|
160 return net, err |
115 } |
161 } |
116 } |
162 } |
117 |
163 |
118 // StringToTimeHookFunc returns a DecodeHookFunc that converts |
164 // StringToTimeHookFunc returns a DecodeHookFunc that converts |
119 // strings to time.Time. |
165 // strings to time.Time. |