aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/mitchellh/mapstructure/decode_hooks.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mitchellh/mapstructure/decode_hooks.go')
-rw-r--r--vendor/github.com/mitchellh/mapstructure/decode_hooks.go79
1 files changed, 71 insertions, 8 deletions
diff --git a/vendor/github.com/mitchellh/mapstructure/decode_hooks.go b/vendor/github.com/mitchellh/mapstructure/decode_hooks.go
index 115ae67..1f0abc6 100644
--- a/vendor/github.com/mitchellh/mapstructure/decode_hooks.go
+++ b/vendor/github.com/mitchellh/mapstructure/decode_hooks.go
@@ -2,6 +2,8 @@ package mapstructure
2 2
3import ( 3import (
4 "errors" 4 "errors"
5 "fmt"
6 "net"
5 "reflect" 7 "reflect"
6 "strconv" 8 "strconv"
7 "strings" 9 "strings"
@@ -38,12 +40,6 @@ func DecodeHookExec(
38 raw DecodeHookFunc, 40 raw DecodeHookFunc,
39 from reflect.Type, to reflect.Type, 41 from reflect.Type, to reflect.Type,
40 data interface{}) (interface{}, error) { 42 data interface{}) (interface{}, error) {
41 // Build our arguments that reflect expects
42 argVals := make([]reflect.Value, 3)
43 argVals[0] = reflect.ValueOf(from)
44 argVals[1] = reflect.ValueOf(to)
45 argVals[2] = reflect.ValueOf(data)
46
47 switch f := typedDecodeHook(raw).(type) { 43 switch f := typedDecodeHook(raw).(type) {
48 case DecodeHookFuncType: 44 case DecodeHookFuncType:
49 return f(from, to, data) 45 return f(from, to, data)
@@ -121,6 +117,74 @@ func StringToTimeDurationHookFunc() DecodeHookFunc {
121 } 117 }
122} 118}
123 119
120// StringToIPHookFunc returns a DecodeHookFunc that converts
121// strings to net.IP
122func 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
146func 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
161 }
162}
163
164// StringToTimeHookFunc returns a DecodeHookFunc that converts
165// strings to time.Time.
166func StringToTimeHookFunc(layout string) DecodeHookFunc {
167 return func(
168 f reflect.Type,
169 t reflect.Type,
170 data interface{}) (interface{}, error) {
171 if f.Kind() != reflect.String {
172 return data, nil
173 }
174 if t != reflect.TypeOf(time.Time{}) {
175 return data, nil
176 }
177
178 // Convert it by parsing
179 return time.Parse(layout, data.(string))
180 }
181}
182
183// WeaklyTypedHook is a DecodeHookFunc which adds support for weak typing to
184// the decoder.
185//
186// Note that this is significantly different from the WeaklyTypedInput option
187// of the DecoderConfig.
124func WeaklyTypedHook( 188func WeaklyTypedHook(
125 f reflect.Kind, 189 f reflect.Kind,
126 t reflect.Kind, 190 t reflect.Kind,
@@ -132,9 +196,8 @@ func WeaklyTypedHook(
132 case reflect.Bool: 196 case reflect.Bool:
133 if dataVal.Bool() { 197 if dataVal.Bool() {
134 return "1", nil 198 return "1", nil
135 } else {
136 return "0", nil
137 } 199 }
200 return "0", nil
138 case reflect.Float32: 201 case reflect.Float32:
139 return strconv.FormatFloat(dataVal.Float(), 'f', -1, 64), nil 202 return strconv.FormatFloat(dataVal.Float(), 'f', -1, 64), nil
140 case reflect.Int: 203 case reflect.Int: