]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - vendor/github.com/mitchellh/mapstructure/mapstructure.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / mitchellh / mapstructure / mapstructure.go
index 6dee0ef0a25d0a9e2a21bd93dadb8ccb89b175ac..256ee63fbf3eebacee7e5404c94310c8b099f4b9 100644 (file)
@@ -1,5 +1,5 @@
-// The mapstructure package exposes functionality to convert an
-// arbitrary map[string]interface{} into a native Go structure.
+// Package mapstructure exposes functionality to convert an arbitrary
+// map[string]interface{} into a native Go structure.
 //
 // The Go structure can be arbitrarily complex, containing slices,
 // other structs, etc. and the decoder will properly decode nested
@@ -32,7 +32,12 @@ import (
 // both.
 type DecodeHookFunc interface{}
 
+// DecodeHookFuncType is a DecodeHookFunc which has complete information about
+// the source and target types.
 type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)
+
+// DecodeHookFuncKind is a DecodeHookFunc which knows only the Kinds of the
+// source and target types.
 type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
 
 // DecoderConfig is the configuration that is used to create a new decoder
@@ -109,12 +114,12 @@ type Metadata struct {
        Unused []string
 }
 
-// Decode takes a map and uses reflection to convert it into the
-// given Go native structure. val must be a pointer to a struct.
-func Decode(m interface{}, rawVal interface{}) error {
+// Decode takes an input structure and uses reflection to translate it to
+// the output structure. output must be a pointer to a map or struct.
+func Decode(input interface{}, output interface{}) error {
        config := &DecoderConfig{
                Metadata: nil,
-               Result:   rawVal,
+               Result:   output,
        }
 
        decoder, err := NewDecoder(config)
@@ -122,7 +127,7 @@ func Decode(m interface{}, rawVal interface{}) error {
                return err
        }
 
-       return decoder.Decode(m)
+       return decoder.Decode(input)
 }
 
 // WeakDecode is the same as Decode but is shorthand to enable
@@ -142,6 +147,40 @@ func WeakDecode(input, output interface{}) error {
        return decoder.Decode(input)
 }
 
+// DecodeMetadata is the same as Decode, but is shorthand to
+// enable metadata collection. See DecoderConfig for more info.
+func DecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
+       config := &DecoderConfig{
+               Metadata: metadata,
+               Result:   output,
+       }
+
+       decoder, err := NewDecoder(config)
+       if err != nil {
+               return err
+       }
+
+       return decoder.Decode(input)
+}
+
+// WeakDecodeMetadata is the same as Decode, but is shorthand to
+// enable both WeaklyTypedInput and metadata collection. See
+// DecoderConfig for more info.
+func WeakDecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
+       config := &DecoderConfig{
+               Metadata:         metadata,
+               Result:           output,
+               WeaklyTypedInput: true,
+       }
+
+       decoder, err := NewDecoder(config)
+       if err != nil {
+               return err
+       }
+
+       return decoder.Decode(input)
+}
+
 // NewDecoder returns a new decoder for the given configuration. Once
 // a decoder has been returned, the same configuration must not be used
 // again.
@@ -179,68 +218,91 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) {
 
 // Decode decodes the given raw interface to the target pointer specified
 // by the configuration.
-func (d *Decoder) Decode(raw interface{}) error {
-       return d.decode("", raw, reflect.ValueOf(d.config.Result).Elem())
+func (d *Decoder) Decode(input interface{}) error {
+       return d.decode("", input, reflect.ValueOf(d.config.Result).Elem())
 }
 
 // Decodes an unknown data type into a specific reflection value.
-func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error {
-       if data == nil {
-               // If the data is nil, then we don't set anything.
+func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {
+       var inputVal reflect.Value
+       if input != nil {
+               inputVal = reflect.ValueOf(input)
+
+               // We need to check here if input is a typed nil. Typed nils won't
+               // match the "input == nil" below so we check that here.
+               if inputVal.Kind() == reflect.Ptr && inputVal.IsNil() {
+                       input = nil
+               }
+       }
+
+       if input == nil {
+               // If the data is nil, then we don't set anything, unless ZeroFields is set
+               // to true.
+               if d.config.ZeroFields {
+                       outVal.Set(reflect.Zero(outVal.Type()))
+
+                       if d.config.Metadata != nil && name != "" {
+                               d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
+                       }
+               }
                return nil
        }
 
-       dataVal := reflect.ValueOf(data)
-       if !dataVal.IsValid() {
-               // If the data value is invalid, then we just set the value
+       if !inputVal.IsValid() {
+               // If the input value is invalid, then we just set the value
                // to be the zero value.
-               val.Set(reflect.Zero(val.Type()))
+               outVal.Set(reflect.Zero(outVal.Type()))
+               if d.config.Metadata != nil && name != "" {
+                       d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
+               }
                return nil
        }
 
        if d.config.DecodeHook != nil {
-               // We have a DecodeHook, so let's pre-process the data.
+               // We have a DecodeHook, so let's pre-process the input.
                var err error
-               data, err = DecodeHookExec(
+               input, err = DecodeHookExec(
                        d.config.DecodeHook,
-                       dataVal.Type(), val.Type(), data)
+                       inputVal.Type(), outVal.Type(), input)
                if err != nil {
                        return fmt.Errorf("error decoding '%s': %s", name, err)
                }
        }
 
        var err error
-       dataKind := getKind(val)
-       switch dataKind {
+       outputKind := getKind(outVal)
+       switch outputKind {
        case reflect.Bool:
-               err = d.decodeBool(name, data, val)
+               err = d.decodeBool(name, input, outVal)
        case reflect.Interface:
-               err = d.decodeBasic(name, data, val)
+               err = d.decodeBasic(name, input, outVal)
        case reflect.String:
-               err = d.decodeString(name, data, val)
+               err = d.decodeString(name, input, outVal)
        case reflect.Int:
-               err = d.decodeInt(name, data, val)
+               err = d.decodeInt(name, input, outVal)
        case reflect.Uint:
-               err = d.decodeUint(name, data, val)
+               err = d.decodeUint(name, input, outVal)
        case reflect.Float32:
-               err = d.decodeFloat(name, data, val)
+               err = d.decodeFloat(name, input, outVal)
        case reflect.Struct:
-               err = d.decodeStruct(name, data, val)
+               err = d.decodeStruct(name, input, outVal)
        case reflect.Map:
-               err = d.decodeMap(name, data, val)
+               err = d.decodeMap(name, input, outVal)
        case reflect.Ptr:
-               err = d.decodePtr(name, data, val)
+               err = d.decodePtr(name, input, outVal)
        case reflect.Slice:
-               err = d.decodeSlice(name, data, val)
+               err = d.decodeSlice(name, input, outVal)
+       case reflect.Array:
+               err = d.decodeArray(name, input, outVal)
        case reflect.Func:
-               err = d.decodeFunc(name, data, val)
+               err = d.decodeFunc(name, input, outVal)
        default:
                // If we reached this point then we weren't able to decode it
-               return fmt.Errorf("%s: unsupported type: %s", name, dataKind)
+               return fmt.Errorf("%s: unsupported type: %s", name, outputKind)
        }
 
        // If we reached here, then we successfully decoded SOMETHING, so
-       // mark the key as used if we're tracking metadata.
+       // mark the key as used if we're tracking metainput.
        if d.config.Metadata != nil && name != "" {
                d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
        }
@@ -251,7 +313,19 @@ func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error
 // This decodes a basic type (bool, int, string, etc.) and sets the
 // value to "data" of that type.
 func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
+       if val.IsValid() && val.Elem().IsValid() {
+               return d.decode(name, data, val.Elem())
+       }
+
        dataVal := reflect.ValueOf(data)
+
+       // If the input data is a pointer, and the assigned type is the dereference
+       // of that exact pointer, then indirect it so that we can assign it.
+       // Example: *string to string
+       if dataVal.Kind() == reflect.Ptr && dataVal.Type().Elem() == val.Type() {
+               dataVal = reflect.Indirect(dataVal)
+       }
+
        if !dataVal.IsValid() {
                dataVal = reflect.Zero(val.Type())
        }
@@ -268,7 +342,7 @@ func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value)
 }
 
 func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
-       dataVal := reflect.ValueOf(data)
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
        dataKind := getKind(dataVal)
 
        converted := true
@@ -287,12 +361,22 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
                val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
        case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
                val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
-       case dataKind == reflect.Slice && d.config.WeaklyTypedInput:
+       case dataKind == reflect.Slice && d.config.WeaklyTypedInput,
+               dataKind == reflect.Array && d.config.WeaklyTypedInput:
                dataType := dataVal.Type()
                elemKind := dataType.Elem().Kind()
-               switch {
-               case elemKind == reflect.Uint8:
-                       val.SetString(string(dataVal.Interface().([]uint8)))
+               switch elemKind {
+               case reflect.Uint8:
+                       var uints []uint8
+                       if dataKind == reflect.Array {
+                               uints = make([]uint8, dataVal.Len(), dataVal.Len())
+                               for i := range uints {
+                                       uints[i] = dataVal.Index(i).Interface().(uint8)
+                               }
+                       } else {
+                               uints = dataVal.Interface().([]uint8)
+                       }
+                       val.SetString(string(uints))
                default:
                        converted = false
                }
@@ -310,7 +394,7 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
 }
 
 func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
-       dataVal := reflect.ValueOf(data)
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
        dataKind := getKind(dataVal)
        dataType := dataVal.Type()
 
@@ -352,7 +436,7 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er
 }
 
 func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
-       dataVal := reflect.ValueOf(data)
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
        dataKind := getKind(dataVal)
 
        switch {
@@ -395,7 +479,7 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
 }
 
 func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
-       dataVal := reflect.ValueOf(data)
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
        dataKind := getKind(dataVal)
 
        switch {
@@ -426,7 +510,7 @@ func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) e
 }
 
 func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
-       dataVal := reflect.ValueOf(data)
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
        dataKind := getKind(dataVal)
        dataType := dataVal.Type()
 
@@ -436,7 +520,7 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value)
        case dataKind == reflect.Uint:
                val.SetFloat(float64(dataVal.Uint()))
        case dataKind == reflect.Float32:
-               val.SetFloat(float64(dataVal.Float()))
+               val.SetFloat(dataVal.Float())
        case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
                if dataVal.Bool() {
                        val.SetFloat(1)
@@ -482,38 +566,68 @@ func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) er
                valMap = reflect.MakeMap(mapType)
        }
 
-       // Check input type
+       // Check input type and based on the input type jump to the proper func
        dataVal := reflect.Indirect(reflect.ValueOf(data))
-       if dataVal.Kind() != reflect.Map {
-               // In weak mode, we accept a slice of maps as an input...
-               if d.config.WeaklyTypedInput {
-                       switch dataVal.Kind() {
-                       case reflect.Array, reflect.Slice:
-                               // Special case for BC reasons (covered by tests)
-                               if dataVal.Len() == 0 {
-                                       val.Set(valMap)
-                                       return nil
-                               }
+       switch dataVal.Kind() {
+       case reflect.Map:
+               return d.decodeMapFromMap(name, dataVal, val, valMap)
 
-                               for i := 0; i < dataVal.Len(); i++ {
-                                       err := d.decode(
-                                               fmt.Sprintf("%s[%d]", name, i),
-                                               dataVal.Index(i).Interface(), val)
-                                       if err != nil {
-                                               return err
-                                       }
-                               }
+       case reflect.Struct:
+               return d.decodeMapFromStruct(name, dataVal, val, valMap)
 
-                               return nil
-                       }
+       case reflect.Array, reflect.Slice:
+               if d.config.WeaklyTypedInput {
+                       return d.decodeMapFromSlice(name, dataVal, val, valMap)
                }
 
+               fallthrough
+
+       default:
                return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
        }
+}
+
+func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
+       // Special case for BC reasons (covered by tests)
+       if dataVal.Len() == 0 {
+               val.Set(valMap)
+               return nil
+       }
+
+       for i := 0; i < dataVal.Len(); i++ {
+               err := d.decode(
+                       fmt.Sprintf("%s[%d]", name, i),
+                       dataVal.Index(i).Interface(), val)
+               if err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
+func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
+       valType := val.Type()
+       valKeyType := valType.Key()
+       valElemType := valType.Elem()
 
        // Accumulate errors
        errors := make([]string, 0)
 
+       // If the input data is empty, then we just match what the input data is.
+       if dataVal.Len() == 0 {
+               if dataVal.IsNil() {
+                       if !val.IsNil() {
+                               val.Set(dataVal)
+                       }
+               } else {
+                       // Set to empty allocated value
+                       val.Set(valMap)
+               }
+
+               return nil
+       }
+
        for _, k := range dataVal.MapKeys() {
                fieldName := fmt.Sprintf("%s[%s]", name, k)
 
@@ -546,22 +660,128 @@ func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) er
        return nil
 }
 
+func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
+       typ := dataVal.Type()
+       for i := 0; i < typ.NumField(); i++ {
+               // Get the StructField first since this is a cheap operation. If the
+               // field is unexported, then ignore it.
+               f := typ.Field(i)
+               if f.PkgPath != "" {
+                       continue
+               }
+
+               // Next get the actual value of this field and verify it is assignable
+               // to the map value.
+               v := dataVal.Field(i)
+               if !v.Type().AssignableTo(valMap.Type().Elem()) {
+                       return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
+               }
+
+               tagValue := f.Tag.Get(d.config.TagName)
+               tagParts := strings.Split(tagValue, ",")
+
+               // Determine the name of the key in the map
+               keyName := f.Name
+               if tagParts[0] != "" {
+                       if tagParts[0] == "-" {
+                               continue
+                       }
+                       keyName = tagParts[0]
+               }
+
+               // If "squash" is specified in the tag, we squash the field down.
+               squash := false
+               for _, tag := range tagParts[1:] {
+                       if tag == "squash" {
+                               squash = true
+                               break
+                       }
+               }
+               if squash && v.Kind() != reflect.Struct {
+                       return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
+               }
+
+               switch v.Kind() {
+               // this is an embedded struct, so handle it differently
+               case reflect.Struct:
+                       x := reflect.New(v.Type())
+                       x.Elem().Set(v)
+
+                       vType := valMap.Type()
+                       vKeyType := vType.Key()
+                       vElemType := vType.Elem()
+                       mType := reflect.MapOf(vKeyType, vElemType)
+                       vMap := reflect.MakeMap(mType)
+
+                       err := d.decode(keyName, x.Interface(), vMap)
+                       if err != nil {
+                               return err
+                       }
+
+                       if squash {
+                               for _, k := range vMap.MapKeys() {
+                                       valMap.SetMapIndex(k, vMap.MapIndex(k))
+                               }
+                       } else {
+                               valMap.SetMapIndex(reflect.ValueOf(keyName), vMap)
+                       }
+
+               default:
+                       valMap.SetMapIndex(reflect.ValueOf(keyName), v)
+               }
+       }
+
+       if val.CanAddr() {
+               val.Set(valMap)
+       }
+
+       return nil
+}
+
 func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error {
+       // If the input data is nil, then we want to just set the output
+       // pointer to be nil as well.
+       isNil := data == nil
+       if !isNil {
+               switch v := reflect.Indirect(reflect.ValueOf(data)); v.Kind() {
+               case reflect.Chan,
+                       reflect.Func,
+                       reflect.Interface,
+                       reflect.Map,
+                       reflect.Ptr,
+                       reflect.Slice:
+                       isNil = v.IsNil()
+               }
+       }
+       if isNil {
+               if !val.IsNil() && val.CanSet() {
+                       nilValue := reflect.New(val.Type()).Elem()
+                       val.Set(nilValue)
+               }
+
+               return nil
+       }
+
        // Create an element of the concrete (non pointer) type and decode
        // into that. Then set the value of the pointer to this type.
        valType := val.Type()
        valElemType := valType.Elem()
+       if val.CanSet() {
+               realVal := val
+               if realVal.IsNil() || d.config.ZeroFields {
+                       realVal = reflect.New(valElemType)
+               }
 
-       realVal := val
-       if realVal.IsNil() || d.config.ZeroFields {
-               realVal = reflect.New(valElemType)
-       }
+               if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
+                       return err
+               }
 
-       if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
-               return err
+               val.Set(realVal)
+       } else {
+               if err := d.decode(name, data, reflect.Indirect(val)); err != nil {
+                       return err
+               }
        }
-
-       val.Set(realVal)
        return nil
 }
 
@@ -587,22 +807,101 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
 
        valSlice := val
        if valSlice.IsNil() || d.config.ZeroFields {
+               if d.config.WeaklyTypedInput {
+                       switch {
+                       // Slice and array we use the normal logic
+                       case dataValKind == reflect.Slice, dataValKind == reflect.Array:
+                               break
+
+                       // Empty maps turn into empty slices
+                       case dataValKind == reflect.Map:
+                               if dataVal.Len() == 0 {
+                                       val.Set(reflect.MakeSlice(sliceType, 0, 0))
+                                       return nil
+                               }
+                               // Create slice of maps of other sizes
+                               return d.decodeSlice(name, []interface{}{data}, val)
+
+                       case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8:
+                               return d.decodeSlice(name, []byte(dataVal.String()), val)
+
+                       // All other types we try to convert to the slice type
+                       // and "lift" it into it. i.e. a string becomes a string slice.
+                       default:
+                               // Just re-try this function with data as a slice.
+                               return d.decodeSlice(name, []interface{}{data}, val)
+                       }
+               }
+
+               // Check input type
+               if dataValKind != reflect.Array && dataValKind != reflect.Slice {
+                       return fmt.Errorf(
+                               "'%s': source data must be an array or slice, got %s", name, dataValKind)
+
+               }
+
+               // If the input value is empty, then don't allocate since non-nil != nil
+               if dataVal.Len() == 0 {
+                       return nil
+               }
+
+               // Make a new slice to hold our result, same size as the original data.
+               valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
+       }
+
+       // Accumulate any errors
+       errors := make([]string, 0)
+
+       for i := 0; i < dataVal.Len(); i++ {
+               currentData := dataVal.Index(i).Interface()
+               for valSlice.Len() <= i {
+                       valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
+               }
+               currentField := valSlice.Index(i)
+
+               fieldName := fmt.Sprintf("%s[%d]", name, i)
+               if err := d.decode(fieldName, currentData, currentField); err != nil {
+                       errors = appendErrors(errors, err)
+               }
+       }
+
+       // Finally, set the value to the slice we built up
+       val.Set(valSlice)
+
+       // If there were errors, we return those
+       if len(errors) > 0 {
+               return &Error{errors}
+       }
+
+       return nil
+}
+
+func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value) error {
+       dataVal := reflect.Indirect(reflect.ValueOf(data))
+       dataValKind := dataVal.Kind()
+       valType := val.Type()
+       valElemType := valType.Elem()
+       arrayType := reflect.ArrayOf(valType.Len(), valElemType)
+
+       valArray := val
+
+       if valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
                // Check input type
                if dataValKind != reflect.Array && dataValKind != reflect.Slice {
                        if d.config.WeaklyTypedInput {
                                switch {
-                               // Empty maps turn into empty slices
+                               // Empty maps turn into empty arrays
                                case dataValKind == reflect.Map:
                                        if dataVal.Len() == 0 {
-                                               val.Set(reflect.MakeSlice(sliceType, 0, 0))
+                                               val.Set(reflect.Zero(arrayType))
                                                return nil
                                        }
 
-                               // All other types we try to convert to the slice type
-                               // and "lift" it into it. i.e. a string becomes a string slice.
+                               // All other types we try to convert to the array type
+                               // and "lift" it into it. i.e. a string becomes a string array.
                                default:
                                        // Just re-try this function with data as a slice.
-                                       return d.decodeSlice(name, []interface{}{data}, val)
+                                       return d.decodeArray(name, []interface{}{data}, val)
                                }
                        }
 
@@ -610,9 +909,14 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
                                "'%s': source data must be an array or slice, got %s", name, dataValKind)
 
                }
+               if dataVal.Len() > arrayType.Len() {
+                       return fmt.Errorf(
+                               "'%s': expected source data to have length less or equal to %d, got %d", name, arrayType.Len(), dataVal.Len())
 
-               // Make a new slice to hold our result, same size as the original data.
-               valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
+               }
+
+               // Make a new array to hold our result, same size as the original data.
+               valArray = reflect.New(arrayType).Elem()
        }
 
        // Accumulate any errors
@@ -620,10 +924,7 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
 
        for i := 0; i < dataVal.Len(); i++ {
                currentData := dataVal.Index(i).Interface()
-               for valSlice.Len() <= i {
-                       valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
-               }
-               currentField := valSlice.Index(i)
+               currentField := valArray.Index(i)
 
                fieldName := fmt.Sprintf("%s[%d]", name, i)
                if err := d.decode(fieldName, currentData, currentField); err != nil {
@@ -631,8 +932,8 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
                }
        }
 
-       // Finally, set the value to the slice we built up
-       val.Set(valSlice)
+       // Finally, set the value to the array we built up
+       val.Set(valArray)
 
        // If there were errors, we return those
        if len(errors) > 0 {
@@ -653,10 +954,29 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
        }
 
        dataValKind := dataVal.Kind()
-       if dataValKind != reflect.Map {
-               return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind)
+       switch dataValKind {
+       case reflect.Map:
+               return d.decodeStructFromMap(name, dataVal, val)
+
+       case reflect.Struct:
+               // Not the most efficient way to do this but we can optimize later if
+               // we want to. To convert from struct to struct we go to map first
+               // as an intermediary.
+               m := make(map[string]interface{})
+               mval := reflect.Indirect(reflect.ValueOf(&m))
+               if err := d.decodeMapFromStruct(name, dataVal, mval, mval); err != nil {
+                       return err
+               }
+
+               result := d.decodeStructFromMap(name, mval, val)
+               return result
+
+       default:
+               return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
        }
+}
 
+func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) error {
        dataValType := dataVal.Type()
        if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
                return fmt.Errorf(
@@ -681,7 +1001,11 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
 
        // Compile the list of all the fields that we're going to be decoding
        // from all the structs.
-       fields := make(map[*reflect.StructField]reflect.Value)
+       type field struct {
+               field reflect.StructField
+               val   reflect.Value
+       }
+       fields := []field{}
        for len(structs) > 0 {
                structVal := structs[0]
                structs = structs[1:]
@@ -707,20 +1031,22 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
                                        errors = appendErrors(errors,
                                                fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
                                } else {
-                                       structs = append(structs, val.FieldByName(fieldType.Name))
+                                       structs = append(structs, structVal.FieldByName(fieldType.Name))
                                }
                                continue
                        }
 
                        // Normal struct field, store it away
-                       fields[&fieldType] = structVal.Field(i)
+                       fields = append(fields, field{fieldType, structVal.Field(i)})
                }
        }
 
-       for fieldType, field := range fields {
-               fieldName := fieldType.Name
+       // for fieldType, field := range fields {
+       for _, f := range fields {
+               field, fieldValue := f.field, f.val
+               fieldName := field.Name
 
-               tagValue := fieldType.Tag.Get(d.config.TagName)
+               tagValue := field.Tag.Get(d.config.TagName)
                tagValue = strings.SplitN(tagValue, ",", 2)[0]
                if tagValue != "" {
                        fieldName = tagValue
@@ -755,14 +1081,14 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
                // Delete the key we're using from the unused map so we stop tracking
                delete(dataValKeysUnused, rawMapKey.Interface())
 
-               if !field.IsValid() {
+               if !fieldValue.IsValid() {
                        // This should never happen
                        panic("field is not valid")
                }
 
                // If we can't set the field, then it is unexported or something,
                // and we just continue onwards.
-               if !field.CanSet() {
+               if !fieldValue.CanSet() {
                        continue
                }
 
@@ -772,7 +1098,7 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
                        fieldName = fmt.Sprintf("%s.%s", name, fieldName)
                }
 
-               if err := d.decode(fieldName, rawMapVal.Interface(), field); err != nil {
+               if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
                        errors = appendErrors(errors, err)
                }
        }