Codebase list golang-github-mitchellh-mapstructure / c554f1b
Import upstream version 1.4.2 Debian Janitor 2 years ago
5 changed file(s) with 96 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
0 ## unreleased
0 ## 1.4.2
11
2 * Fix regression where `*time.Time` value would be set to empty and not be sent
2 * Custom name matchers to support any sort of casing, formatting, etc. for
3 field names. [GH-250]
4 * Fix possible panic in ComposeDecodeHookFunc [GH-251]
5
6 ## 1.4.1
7
8 * Fix regression where `*time.Time` value would be set to empty and not be sent
39 to decode hooks properly [GH-232]
410
511 ## 1.4.0
6161 func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
6262 return func(f reflect.Value, t reflect.Value) (interface{}, error) {
6363 var err error
64 var data interface{}
64 data := f.Interface()
65
6566 newFrom := f
6667 for _, f1 := range fs {
6768 data, err = DecodeHookExec(f1, newFrom, t)
8383 }
8484 }
8585
86 func TestComposeDecodeHookFunc_safe_nofuncs(t *testing.T) {
87 f := ComposeDecodeHookFunc()
88 type myStruct2 struct {
89 MyInt int
90 }
91
92 type myStruct1 struct {
93 Blah map[string]myStruct2
94 }
95
96 src := &myStruct1{Blah: map[string]myStruct2{
97 "test": {
98 MyInt: 1,
99 },
100 }}
101
102 dst := &myStruct1{}
103 dConf := &DecoderConfig{
104 Result: dst,
105 ErrorUnused: true,
106 DecodeHook: f,
107 }
108 d, err := NewDecoder(dConf)
109 if err != nil {
110 t.Fatal(err)
111 }
112 err = d.Decode(src)
113 if err != nil {
114 t.Fatal(err)
115 }
116 }
117
86118 func TestStringToSliceHookFunc(t *testing.T) {
87119 f := StringToSliceHookFunc(",")
88120
191191 // source and target types.
192192 type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
193193
194 // DecodeHookFuncRaw is a DecodeHookFunc which has complete access to both the source and target
194 // DecodeHookFuncValue is a DecodeHookFunc which has complete access to both the source and target
195195 // values.
196196 type DecodeHookFuncValue func(from reflect.Value, to reflect.Value) (interface{}, error)
197197
257257 // The tag name that mapstructure reads for field names. This
258258 // defaults to "mapstructure"
259259 TagName string
260
261 // MatchName is the function used to match the map key to the struct
262 // field name or tag. Defaults to `strings.EqualFold`. This can be used
263 // to implement case-sensitive tag values, support snake casing, etc.
264 MatchName func(mapKey, fieldName string) bool
260265 }
261266
262267 // A Decoder takes a raw interface value and turns it into structured
373378
374379 if config.TagName == "" {
375380 config.TagName = "mapstructure"
381 }
382
383 if config.MatchName == nil {
384 config.MatchName = strings.EqualFold
376385 }
377386
378387 result := &Decoder{
13391348 continue
13401349 }
13411350
1342 if strings.EqualFold(mK, fieldName) {
1351 if d.config.MatchName(mK, fieldName) {
13431352 rawMapKey = dataValKey
13441353 rawMapVal = dataVal.MapIndex(dataValKey)
13451354 break
24302430 }
24312431 }
24322432
2433 func TestDecoder_MatchName(t *testing.T) {
2434 t.Parallel()
2435
2436 type Target struct {
2437 FirstMatch string `mapstructure:"first_match"`
2438 SecondMatch string
2439 NoMatch string `mapstructure:"no_match"`
2440 }
2441
2442 input := map[string]interface{}{
2443 "first_match": "foo",
2444 "SecondMatch": "bar",
2445 "NO_MATCH": "baz",
2446 }
2447
2448 expected := Target{
2449 FirstMatch: "foo",
2450 SecondMatch: "bar",
2451 }
2452
2453 var actual Target
2454 config := &DecoderConfig{
2455 Result: &actual,
2456 MatchName: func(mapKey, fieldName string) bool {
2457 return mapKey == fieldName
2458 },
2459 }
2460
2461 decoder, err := NewDecoder(config)
2462 if err != nil {
2463 t.Fatalf("err: %s", err)
2464 }
2465
2466 err = decoder.Decode(input)
2467 if err != nil {
2468 t.Fatalf("err: %s", err)
2469 }
2470
2471 if !reflect.DeepEqual(expected, actual) {
2472 t.Fatalf("Decode() expected: %#v, got: %#v", expected, actual)
2473 }
2474 }
2475
24332476 func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) {
24342477 var result Slice
24352478 err := Decode(input, &result)