Merge pull request #9455 from hashicorp/dnephin/update-mapstructure
Update mapstructure dependency
This commit is contained in:
commit
2803a7e0bc
2
go.mod
2
go.mod
|
@ -67,7 +67,7 @@ require (
|
||||||
github.com/mitchellh/copystructure v1.0.0
|
github.com/mitchellh/copystructure v1.0.0
|
||||||
github.com/mitchellh/go-testing-interface v1.14.0
|
github.com/mitchellh/go-testing-interface v1.14.0
|
||||||
github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452
|
github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452
|
||||||
github.com/mitchellh/mapstructure v1.3.3
|
github.com/mitchellh/mapstructure v1.4.1-0.20210112042008-8ebf2d61a8b4
|
||||||
github.com/mitchellh/pointerstructure v1.0.0
|
github.com/mitchellh/pointerstructure v1.0.0
|
||||||
github.com/mitchellh/reflectwalk v1.0.1
|
github.com/mitchellh/reflectwalk v1.0.1
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -380,8 +380,8 @@ github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452/go.mod h1:
|
||||||
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||||
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
|
github.com/mitchellh/mapstructure v1.4.1-0.20210112042008-8ebf2d61a8b4 h1:MGwxzM4mdkhmCfDyEmSfng7tE1QRIUGbedKdaMksvjw=
|
||||||
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
github.com/mitchellh/mapstructure v1.4.1-0.20210112042008-8ebf2d61a8b4/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
github.com/mitchellh/pointerstructure v1.0.0 h1:ATSdz4NWrmWPOF1CeCBU4sMCno2hgqdbSrRPFWQSVZI=
|
github.com/mitchellh/pointerstructure v1.0.0 h1:ATSdz4NWrmWPOF1CeCBU4sMCno2hgqdbSrRPFWQSVZI=
|
||||||
github.com/mitchellh/pointerstructure v1.0.0/go.mod h1:k4XwG94++jLVsSiTxo7qdIfXA9pj9EAeo0QsNNJOLZ8=
|
github.com/mitchellh/pointerstructure v1.0.0/go.mod h1:k4XwG94++jLVsSiTxo7qdIfXA9pj9EAeo0QsNNJOLZ8=
|
||||||
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
language: go
|
|
||||||
|
|
||||||
go:
|
|
||||||
- "1.14.x"
|
|
||||||
- tip
|
|
||||||
|
|
||||||
script:
|
|
||||||
- go test
|
|
||||||
- go test -bench . -benchmem
|
|
|
@ -1,3 +1,15 @@
|
||||||
|
## unreleased
|
||||||
|
|
||||||
|
* Fix regression where `*time.Time` value would be set to empty and not be sent
|
||||||
|
to decode hooks properly [GH-232]
|
||||||
|
|
||||||
|
## 1.4.0
|
||||||
|
|
||||||
|
* A new decode hook type `DecodeHookFuncValue` has been added that has
|
||||||
|
access to the full values. [GH-183]
|
||||||
|
* Squash is now supported with embedded fields that are struct pointers [GH-205]
|
||||||
|
* Empty strings will convert to 0 for all numeric types when weakly decoding [GH-206]
|
||||||
|
|
||||||
## 1.3.3
|
## 1.3.3
|
||||||
|
|
||||||
* Decoding maps from maps creates a settable value for decode hooks [GH-203]
|
* Decoding maps from maps creates a settable value for decode hooks [GH-203]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package mapstructure
|
package mapstructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
@ -16,10 +17,11 @@ func typedDecodeHook(h DecodeHookFunc) DecodeHookFunc {
|
||||||
// Create variables here so we can reference them with the reflect pkg
|
// Create variables here so we can reference them with the reflect pkg
|
||||||
var f1 DecodeHookFuncType
|
var f1 DecodeHookFuncType
|
||||||
var f2 DecodeHookFuncKind
|
var f2 DecodeHookFuncKind
|
||||||
|
var f3 DecodeHookFuncValue
|
||||||
|
|
||||||
// Fill in the variables into this interface and the rest is done
|
// Fill in the variables into this interface and the rest is done
|
||||||
// automatically using the reflect package.
|
// automatically using the reflect package.
|
||||||
potential := []interface{}{f1, f2}
|
potential := []interface{}{f1, f2, f3}
|
||||||
|
|
||||||
v := reflect.ValueOf(h)
|
v := reflect.ValueOf(h)
|
||||||
vt := v.Type()
|
vt := v.Type()
|
||||||
|
@ -38,13 +40,15 @@ func typedDecodeHook(h DecodeHookFunc) DecodeHookFunc {
|
||||||
// that took reflect.Kind instead of reflect.Type.
|
// that took reflect.Kind instead of reflect.Type.
|
||||||
func DecodeHookExec(
|
func DecodeHookExec(
|
||||||
raw DecodeHookFunc,
|
raw DecodeHookFunc,
|
||||||
from reflect.Type, to reflect.Type,
|
from reflect.Value, to reflect.Value) (interface{}, error) {
|
||||||
data interface{}) (interface{}, error) {
|
|
||||||
switch f := typedDecodeHook(raw).(type) {
|
switch f := typedDecodeHook(raw).(type) {
|
||||||
case DecodeHookFuncType:
|
case DecodeHookFuncType:
|
||||||
return f(from, to, data)
|
return f(from.Type(), to.Type(), from.Interface())
|
||||||
case DecodeHookFuncKind:
|
case DecodeHookFuncKind:
|
||||||
return f(from.Kind(), to.Kind(), data)
|
return f(from.Kind(), to.Kind(), from.Interface())
|
||||||
|
case DecodeHookFuncValue:
|
||||||
|
return f(from, to)
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("invalid decode hook signature")
|
return nil, errors.New("invalid decode hook signature")
|
||||||
}
|
}
|
||||||
|
@ -56,22 +60,16 @@ func DecodeHookExec(
|
||||||
// The composed funcs are called in order, with the result of the
|
// The composed funcs are called in order, with the result of the
|
||||||
// previous transformation.
|
// previous transformation.
|
||||||
func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
|
func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
|
||||||
return func(
|
return func(f reflect.Value, t reflect.Value) (interface{}, error) {
|
||||||
f reflect.Type,
|
|
||||||
t reflect.Type,
|
|
||||||
data interface{}) (interface{}, error) {
|
|
||||||
var err error
|
var err error
|
||||||
|
var data interface{}
|
||||||
|
newFrom := f
|
||||||
for _, f1 := range fs {
|
for _, f1 := range fs {
|
||||||
data, err = DecodeHookExec(f1, f, t, data)
|
data, err = DecodeHookExec(f1, newFrom, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
newFrom = reflect.ValueOf(data)
|
||||||
// Modify the from kind to be correct with the new data
|
|
||||||
f = nil
|
|
||||||
if val := reflect.ValueOf(data); val.IsValid() {
|
|
||||||
f = val.Type()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -215,3 +213,44 @@ func WeaklyTypedHook(
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RecursiveStructToMapHookFunc() DecodeHookFunc {
|
||||||
|
return func(f reflect.Value, t reflect.Value) (interface{}, error) {
|
||||||
|
if f.Kind() != reflect.Struct {
|
||||||
|
return f.Interface(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var i interface{} = struct{}{}
|
||||||
|
if t.Type() != reflect.TypeOf(&i).Elem() {
|
||||||
|
return f.Interface(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m := make(map[string]interface{})
|
||||||
|
t.Set(reflect.ValueOf(m))
|
||||||
|
|
||||||
|
return f.Interface(), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TextUnmarshallerHookFunc returns a DecodeHookFunc that applies
|
||||||
|
// strings to the UnmarshalText function, when the target type
|
||||||
|
// implements the encoding.TextUnmarshaler interface
|
||||||
|
func TextUnmarshallerHookFunc() DecodeHookFuncType {
|
||||||
|
return func(
|
||||||
|
f reflect.Type,
|
||||||
|
t reflect.Type,
|
||||||
|
data interface{}) (interface{}, error) {
|
||||||
|
if f.Kind() != reflect.String {
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
result := reflect.New(t).Interface()
|
||||||
|
unmarshaller, ok := result.(encoding.TextUnmarshaler)
|
||||||
|
if !ok {
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
if err := unmarshaller.UnmarshalText([]byte(data.(string))); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -72,6 +72,17 @@
|
||||||
// "name": "alice",
|
// "name": "alice",
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
// When decoding from a struct to a map, the squash tag squashes the struct
|
||||||
|
// fields into a single map. Using the example structs from above:
|
||||||
|
//
|
||||||
|
// Friend{Person: Person{Name: "alice"}}
|
||||||
|
//
|
||||||
|
// Will be decoded into a map:
|
||||||
|
//
|
||||||
|
// map[string]interface{}{
|
||||||
|
// "name": "alice",
|
||||||
|
// }
|
||||||
|
//
|
||||||
// DecoderConfig has a field that changes the behavior of mapstructure
|
// DecoderConfig has a field that changes the behavior of mapstructure
|
||||||
// to always squash embedded structs.
|
// to always squash embedded structs.
|
||||||
//
|
//
|
||||||
|
@ -161,10 +172,11 @@ import (
|
||||||
// data transformations. See "DecodeHook" in the DecoderConfig
|
// data transformations. See "DecodeHook" in the DecoderConfig
|
||||||
// struct.
|
// struct.
|
||||||
//
|
//
|
||||||
// The type should be DecodeHookFuncType or DecodeHookFuncKind.
|
// The type must be one of DecodeHookFuncType, DecodeHookFuncKind, or
|
||||||
// Either is accepted. Types are a superset of Kinds (Types can return
|
// DecodeHookFuncValue.
|
||||||
// Kinds) and are generally a richer thing to use, but Kinds are simpler
|
// Values are a superset of Types (Values can return types), and Types are a
|
||||||
// if you only need those.
|
// superset of Kinds (Types can return Kinds) and are generally a richer thing
|
||||||
|
// to use, but Kinds are simpler if you only need those.
|
||||||
//
|
//
|
||||||
// The reason DecodeHookFunc is multi-typed is for backwards compatibility:
|
// The reason DecodeHookFunc is multi-typed is for backwards compatibility:
|
||||||
// we started with Kinds and then realized Types were the better solution,
|
// we started with Kinds and then realized Types were the better solution,
|
||||||
|
@ -180,15 +192,22 @@ type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface
|
||||||
// source and target types.
|
// source and target types.
|
||||||
type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
|
type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
|
||||||
|
|
||||||
|
// DecodeHookFuncRaw is a DecodeHookFunc which has complete access to both the source and target
|
||||||
|
// values.
|
||||||
|
type DecodeHookFuncValue func(from reflect.Value, to reflect.Value) (interface{}, error)
|
||||||
|
|
||||||
// DecoderConfig is the configuration that is used to create a new decoder
|
// DecoderConfig is the configuration that is used to create a new decoder
|
||||||
// and allows customization of various aspects of decoding.
|
// and allows customization of various aspects of decoding.
|
||||||
type DecoderConfig struct {
|
type DecoderConfig struct {
|
||||||
// DecodeHook, if set, will be called before any decoding and any
|
// DecodeHook, if set, will be called before any decoding and any
|
||||||
// type conversion (if WeaklyTypedInput is on). This lets you modify
|
// type conversion (if WeaklyTypedInput is on). This lets you modify
|
||||||
// the values before they're set down onto the resulting struct.
|
// the values before they're set down onto the resulting struct. The
|
||||||
|
// DecodeHook is called for every map and value in the input. This means
|
||||||
|
// that if a struct has embedded fields with squash tags the decode hook
|
||||||
|
// is called only once with all of the input data, not once for each
|
||||||
|
// embedded struct.
|
||||||
//
|
//
|
||||||
// If an error is returned, the entire decode will fail with that
|
// If an error is returned, the entire decode will fail with that error.
|
||||||
// error.
|
|
||||||
DecodeHook DecodeHookFunc
|
DecodeHook DecodeHookFunc
|
||||||
|
|
||||||
// If ErrorUnused is true, then it is an error for there to exist
|
// If ErrorUnused is true, then it is an error for there to exist
|
||||||
|
@ -409,9 +428,7 @@ func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) e
|
||||||
if d.config.DecodeHook != nil {
|
if d.config.DecodeHook != nil {
|
||||||
// We have a DecodeHook, so let's pre-process the input.
|
// We have a DecodeHook, so let's pre-process the input.
|
||||||
var err error
|
var err error
|
||||||
input, err = DecodeHookExec(
|
input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal)
|
||||||
d.config.DecodeHook,
|
|
||||||
inputVal.Type(), outVal.Type(), input)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error decoding '%s': %s", name, err)
|
return fmt.Errorf("error decoding '%s': %s", name, err)
|
||||||
}
|
}
|
||||||
|
@ -562,8 +579,8 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
|
||||||
|
|
||||||
if !converted {
|
if !converted {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -588,7 +605,12 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er
|
||||||
val.SetInt(0)
|
val.SetInt(0)
|
||||||
}
|
}
|
||||||
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
||||||
i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits())
|
str := dataVal.String()
|
||||||
|
if str == "" {
|
||||||
|
str = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
i, err := strconv.ParseInt(str, 0, val.Type().Bits())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
val.SetInt(i)
|
val.SetInt(i)
|
||||||
} else {
|
} else {
|
||||||
|
@ -604,8 +626,8 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er
|
||||||
val.SetInt(i)
|
val.SetInt(i)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -640,7 +662,12 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
|
||||||
val.SetUint(0)
|
val.SetUint(0)
|
||||||
}
|
}
|
||||||
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
||||||
i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
|
str := dataVal.String()
|
||||||
|
if str == "" {
|
||||||
|
str = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
i, err := strconv.ParseUint(str, 0, val.Type().Bits())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
val.SetUint(i)
|
val.SetUint(i)
|
||||||
} else {
|
} else {
|
||||||
|
@ -660,8 +687,8 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
|
||||||
val.SetUint(uint64(i))
|
val.SetUint(uint64(i))
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -691,8 +718,8 @@ func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) e
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -717,7 +744,12 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value)
|
||||||
val.SetFloat(0)
|
val.SetFloat(0)
|
||||||
}
|
}
|
||||||
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
case dataKind == reflect.String && d.config.WeaklyTypedInput:
|
||||||
f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits())
|
str := dataVal.String()
|
||||||
|
if str == "" {
|
||||||
|
str = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := strconv.ParseFloat(str, val.Type().Bits())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
val.SetFloat(f)
|
val.SetFloat(f)
|
||||||
} else {
|
} else {
|
||||||
|
@ -733,8 +765,8 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value)
|
||||||
val.SetFloat(i)
|
val.SetFloat(i)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -785,7 +817,7 @@ func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val ref
|
||||||
|
|
||||||
for i := 0; i < dataVal.Len(); i++ {
|
for i := 0; i < dataVal.Len(); i++ {
|
||||||
err := d.decode(
|
err := d.decode(
|
||||||
fmt.Sprintf("%s[%d]", name, i),
|
name+"["+strconv.Itoa(i)+"]",
|
||||||
dataVal.Index(i).Interface(), val)
|
dataVal.Index(i).Interface(), val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -818,7 +850,7 @@ func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val refle
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range dataVal.MapKeys() {
|
for _, k := range dataVal.MapKeys() {
|
||||||
fieldName := fmt.Sprintf("%s[%s]", name, k)
|
fieldName := name + "[" + k.String() + "]"
|
||||||
|
|
||||||
// First decode the key into the proper type
|
// First decode the key into the proper type
|
||||||
currentKey := reflect.Indirect(reflect.New(valKeyType))
|
currentKey := reflect.Indirect(reflect.New(valKeyType))
|
||||||
|
@ -871,6 +903,7 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
|
||||||
|
|
||||||
// If Squash is set in the config, we squash the field down.
|
// If Squash is set in the config, we squash the field down.
|
||||||
squash := d.config.Squash && v.Kind() == reflect.Struct && f.Anonymous
|
squash := d.config.Squash && v.Kind() == reflect.Struct && f.Anonymous
|
||||||
|
|
||||||
// Determine the name of the key in the map
|
// Determine the name of the key in the map
|
||||||
if index := strings.Index(tagValue, ","); index != -1 {
|
if index := strings.Index(tagValue, ","); index != -1 {
|
||||||
if tagValue[:index] == "-" {
|
if tagValue[:index] == "-" {
|
||||||
|
@ -883,9 +916,17 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
|
||||||
|
|
||||||
// If "squash" is specified in the tag, we squash the field down.
|
// If "squash" is specified in the tag, we squash the field down.
|
||||||
squash = !squash && strings.Index(tagValue[index+1:], "squash") != -1
|
squash = !squash && strings.Index(tagValue[index+1:], "squash") != -1
|
||||||
if squash && v.Kind() != reflect.Struct {
|
if squash {
|
||||||
|
// When squashing, the embedded type can be a pointer to a struct.
|
||||||
|
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The final type must be a struct
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
|
return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
keyName = tagValue[:index]
|
keyName = tagValue[:index]
|
||||||
} else if len(tagValue) > 0 {
|
} else if len(tagValue) > 0 {
|
||||||
if tagValue == "-" {
|
if tagValue == "-" {
|
||||||
|
@ -995,8 +1036,8 @@ func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) e
|
||||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||||
if val.Type() != dataVal.Type() {
|
if val.Type() != dataVal.Type() {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
|
||||||
name, val.Type(), dataVal.Type())
|
name, val.Type(), dataVal.Type(), data)
|
||||||
}
|
}
|
||||||
val.Set(dataVal)
|
val.Set(dataVal)
|
||||||
return nil
|
return nil
|
||||||
|
@ -1062,7 +1103,7 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
|
||||||
}
|
}
|
||||||
currentField := valSlice.Index(i)
|
currentField := valSlice.Index(i)
|
||||||
|
|
||||||
fieldName := fmt.Sprintf("%s[%d]", name, i)
|
fieldName := name + "[" + strconv.Itoa(i) + "]"
|
||||||
if err := d.decode(fieldName, currentData, currentField); err != nil {
|
if err := d.decode(fieldName, currentData, currentField); err != nil {
|
||||||
errors = appendErrors(errors, err)
|
errors = appendErrors(errors, err)
|
||||||
}
|
}
|
||||||
|
@ -1129,7 +1170,7 @@ func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value)
|
||||||
currentData := dataVal.Index(i).Interface()
|
currentData := dataVal.Index(i).Interface()
|
||||||
currentField := valArray.Index(i)
|
currentField := valArray.Index(i)
|
||||||
|
|
||||||
fieldName := fmt.Sprintf("%s[%d]", name, i)
|
fieldName := name + "[" + strconv.Itoa(i) + "]"
|
||||||
if err := d.decode(fieldName, currentData, currentField); err != nil {
|
if err := d.decode(fieldName, currentData, currentField); err != nil {
|
||||||
errors = appendErrors(errors, err)
|
errors = appendErrors(errors, err)
|
||||||
}
|
}
|
||||||
|
@ -1232,10 +1273,14 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
||||||
|
|
||||||
for i := 0; i < structType.NumField(); i++ {
|
for i := 0; i < structType.NumField(); i++ {
|
||||||
fieldType := structType.Field(i)
|
fieldType := structType.Field(i)
|
||||||
fieldKind := fieldType.Type.Kind()
|
fieldVal := structVal.Field(i)
|
||||||
|
if fieldVal.Kind() == reflect.Ptr && fieldVal.Elem().Kind() == reflect.Struct {
|
||||||
|
// Handle embedded struct pointers as embedded structs.
|
||||||
|
fieldVal = fieldVal.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
// If "squash" is specified in the tag, we squash the field down.
|
// If "squash" is specified in the tag, we squash the field down.
|
||||||
squash := d.config.Squash && fieldKind == reflect.Struct && fieldType.Anonymous
|
squash := d.config.Squash && fieldVal.Kind() == reflect.Struct && fieldType.Anonymous
|
||||||
remain := false
|
remain := false
|
||||||
|
|
||||||
// We always parse the tags cause we're looking for other tags too
|
// We always parse the tags cause we're looking for other tags too
|
||||||
|
@ -1253,21 +1298,21 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
||||||
}
|
}
|
||||||
|
|
||||||
if squash {
|
if squash {
|
||||||
if fieldKind != reflect.Struct {
|
if fieldVal.Kind() != reflect.Struct {
|
||||||
errors = appendErrors(errors,
|
errors = appendErrors(errors,
|
||||||
fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
|
fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldVal.Kind()))
|
||||||
} else {
|
} else {
|
||||||
structs = append(structs, structVal.FieldByName(fieldType.Name))
|
structs = append(structs, fieldVal)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build our field
|
// Build our field
|
||||||
if remain {
|
if remain {
|
||||||
remainField = &field{fieldType, structVal.Field(i)}
|
remainField = &field{fieldType, fieldVal}
|
||||||
} else {
|
} else {
|
||||||
// Normal struct field, store it away
|
// Normal struct field, store it away
|
||||||
fields = append(fields, field{fieldType, structVal.Field(i)})
|
fields = append(fields, field{fieldType, fieldVal})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1326,7 +1371,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
||||||
// If the name is empty string, then we're at the root, and we
|
// If the name is empty string, then we're at the root, and we
|
||||||
// don't dot-join the fields.
|
// don't dot-join the fields.
|
||||||
if name != "" {
|
if name != "" {
|
||||||
fieldName = fmt.Sprintf("%s.%s", name, fieldName)
|
fieldName = name + "." + fieldName
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
|
if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
|
||||||
|
@ -1373,7 +1418,7 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
||||||
for rawKey := range dataValKeysUnused {
|
for rawKey := range dataValKeysUnused {
|
||||||
key := rawKey.(string)
|
key := rawKey.(string)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
key = fmt.Sprintf("%s.%s", name, key)
|
key = name + "." + key
|
||||||
}
|
}
|
||||||
|
|
||||||
d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
|
d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
|
||||||
|
|
|
@ -336,7 +336,7 @@ github.com/mitchellh/go-homedir
|
||||||
github.com/mitchellh/go-testing-interface
|
github.com/mitchellh/go-testing-interface
|
||||||
# github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452
|
# github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452
|
||||||
github.com/mitchellh/hashstructure
|
github.com/mitchellh/hashstructure
|
||||||
# github.com/mitchellh/mapstructure v1.3.3
|
# github.com/mitchellh/mapstructure v1.4.1-0.20210112042008-8ebf2d61a8b4
|
||||||
github.com/mitchellh/mapstructure
|
github.com/mitchellh/mapstructure
|
||||||
# github.com/mitchellh/pointerstructure v1.0.0
|
# github.com/mitchellh/pointerstructure v1.0.0
|
||||||
github.com/mitchellh/pointerstructure
|
github.com/mitchellh/pointerstructure
|
||||||
|
|
Loading…
Reference in New Issue