1 // Package mapstructure exposes functionality to convert an arbitrary
2 // map[string]interface{} into a native Go structure.
4 // The Go structure can be arbitrarily complex, containing slices,
5 // other structs, etc. and the decoder will properly decode nested
6 // maps and so on into the proper structures in the native Go struct.
7 // See the examples to see what the decoder is capable of.
20 // DecodeHookFunc is the callback function that can be used for
21 // data transformations. See "DecodeHook" in the DecoderConfig
24 // The type should be DecodeHookFuncType or DecodeHookFuncKind.
25 // Either is accepted. Types are a superset of Kinds (Types can return
26 // Kinds) and are generally a richer thing to use, but Kinds are simpler
27 // if you only need those.
29 // The reason DecodeHookFunc is multi-typed is for backwards compatibility:
30 // we started with Kinds and then realized Types were the better solution,
31 // but have a promise to not break backwards compat so we now support
33 type DecodeHookFunc interface{}
35 // DecodeHookFuncType is a DecodeHookFunc which has complete information about
36 // the source and target types.
37 type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)
39 // DecodeHookFuncKind is a DecodeHookFunc which knows only the Kinds of the
40 // source and target types.
41 type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)
43 // DecoderConfig is the configuration that is used to create a new decoder
44 // and allows customization of various aspects of decoding.
45 type DecoderConfig struct {
46 // DecodeHook, if set, will be called before any decoding and any
47 // type conversion (if WeaklyTypedInput is on). This lets you modify
48 // the values before they're set down onto the resulting struct.
50 // If an error is returned, the entire decode will fail with that
52 DecodeHook DecodeHookFunc
54 // If ErrorUnused is true, then it is an error for there to exist
55 // keys in the original map that were unused in the decoding process
59 // ZeroFields, if set to true, will zero fields before writing them.
60 // For example, a map will be emptied before decoded values are put in
61 // it. If this is false, a map will be merged.
64 // If WeaklyTypedInput is true, the decoder will make the following
65 // "weak" conversions:
67 // - bools to string (true = "1", false = "0")
68 // - numbers to string (base 10)
69 // - bools to int/uint (true = 1, false = 0)
70 // - strings to int/uint (base implied by prefix)
71 // - int to bool (true if value != 0)
72 // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F,
73 // FALSE, false, False. Anything else is an error)
74 // - empty array = empty map and vice versa
75 // - negative numbers to overflowed uint values (base 10)
76 // - slice of maps to a merged map
77 // - single values are converted to slices if required. Each
78 // element is weakly decoded. For example: "4" can become []int{4}
79 // if the target type is an int slice.
83 // Metadata is the struct that will contain extra metadata about
84 // the decoding. If this is nil, then no metadata will be tracked.
87 // Result is a pointer to the struct that will contain the decoded
91 // The tag name that mapstructure reads for field names. This
92 // defaults to "mapstructure"
96 // A Decoder takes a raw interface value and turns it into structured
97 // data, keeping track of rich error information along the way in case
98 // anything goes wrong. Unlike the basic top-level Decode method, you can
99 // more finely control how the Decoder behaves using the DecoderConfig
100 // structure. The top-level Decode method is just a convenience that sets
101 // up the most basic Decoder.
102 type Decoder struct {
103 config *DecoderConfig
106 // Metadata contains information about decoding a structure that
107 // is tedious or difficult to get otherwise.
108 type Metadata struct {
109 // Keys are the keys of the structure which were successfully decoded
112 // Unused is a slice of keys that were found in the raw value but
113 // weren't decoded since there was no matching field in the result interface
117 // Decode takes a map and uses reflection to convert it into the
118 // given Go native structure. val must be a pointer to a struct.
119 func Decode(m interface{}, rawVal interface{}) error {
120 config := &DecoderConfig{
125 decoder, err := NewDecoder(config)
130 return decoder.Decode(m)
133 // WeakDecode is the same as Decode but is shorthand to enable
134 // WeaklyTypedInput. See DecoderConfig for more info.
135 func WeakDecode(input, output interface{}) error {
136 config := &DecoderConfig{
139 WeaklyTypedInput: true,
142 decoder, err := NewDecoder(config)
147 return decoder.Decode(input)
150 // NewDecoder returns a new decoder for the given configuration. Once
151 // a decoder has been returned, the same configuration must not be used
153 func NewDecoder(config *DecoderConfig) (*Decoder, error) {
154 val := reflect.ValueOf(config.Result)
155 if val.Kind() != reflect.Ptr {
156 return nil, errors.New("result must be a pointer")
161 return nil, errors.New("result must be addressable (a pointer)")
164 if config.Metadata != nil {
165 if config.Metadata.Keys == nil {
166 config.Metadata.Keys = make([]string, 0)
169 if config.Metadata.Unused == nil {
170 config.Metadata.Unused = make([]string, 0)
174 if config.TagName == "" {
175 config.TagName = "mapstructure"
185 // Decode decodes the given raw interface to the target pointer specified
186 // by the configuration.
187 func (d *Decoder) Decode(raw interface{}) error {
188 return d.decode("", raw, reflect.ValueOf(d.config.Result).Elem())
191 // Decodes an unknown data type into a specific reflection value.
192 func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error {
194 // If the data is nil, then we don't set anything.
198 dataVal := reflect.ValueOf(data)
199 if !dataVal.IsValid() {
200 // If the data value is invalid, then we just set the value
201 // to be the zero value.
202 val.Set(reflect.Zero(val.Type()))
206 if d.config.DecodeHook != nil {
207 // We have a DecodeHook, so let's pre-process the data.
209 data, err = DecodeHookExec(
211 dataVal.Type(), val.Type(), data)
213 return fmt.Errorf("error decoding '%s': %s", name, err)
218 dataKind := getKind(val)
221 err = d.decodeBool(name, data, val)
222 case reflect.Interface:
223 err = d.decodeBasic(name, data, val)
225 err = d.decodeString(name, data, val)
227 err = d.decodeInt(name, data, val)
229 err = d.decodeUint(name, data, val)
230 case reflect.Float32:
231 err = d.decodeFloat(name, data, val)
233 err = d.decodeStruct(name, data, val)
235 err = d.decodeMap(name, data, val)
237 err = d.decodePtr(name, data, val)
239 err = d.decodeSlice(name, data, val)
241 err = d.decodeFunc(name, data, val)
243 // If we reached this point then we weren't able to decode it
244 return fmt.Errorf("%s: unsupported type: %s", name, dataKind)
247 // If we reached here, then we successfully decoded SOMETHING, so
248 // mark the key as used if we're tracking metadata.
249 if d.config.Metadata != nil && name != "" {
250 d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
256 // This decodes a basic type (bool, int, string, etc.) and sets the
257 // value to "data" of that type.
258 func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
259 dataVal := reflect.ValueOf(data)
260 if !dataVal.IsValid() {
261 dataVal = reflect.Zero(val.Type())
264 dataValType := dataVal.Type()
265 if !dataValType.AssignableTo(val.Type()) {
267 "'%s' expected type '%s', got '%s'",
268 name, val.Type(), dataValType)
275 func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
276 dataVal := reflect.ValueOf(data)
277 dataKind := getKind(dataVal)
281 case dataKind == reflect.String:
282 val.SetString(dataVal.String())
283 case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
289 case dataKind == reflect.Int && d.config.WeaklyTypedInput:
290 val.SetString(strconv.FormatInt(dataVal.Int(), 10))
291 case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
292 val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
293 case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
294 val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
295 case dataKind == reflect.Slice && d.config.WeaklyTypedInput:
296 dataType := dataVal.Type()
297 elemKind := dataType.Elem().Kind()
299 case elemKind == reflect.Uint8:
300 val.SetString(string(dataVal.Interface().([]uint8)))
310 "'%s' expected type '%s', got unconvertible type '%s'",
311 name, val.Type(), dataVal.Type())
317 func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
318 dataVal := reflect.ValueOf(data)
319 dataKind := getKind(dataVal)
320 dataType := dataVal.Type()
323 case dataKind == reflect.Int:
324 val.SetInt(dataVal.Int())
325 case dataKind == reflect.Uint:
326 val.SetInt(int64(dataVal.Uint()))
327 case dataKind == reflect.Float32:
328 val.SetInt(int64(dataVal.Float()))
329 case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
335 case dataKind == reflect.String && d.config.WeaklyTypedInput:
336 i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits())
340 return fmt.Errorf("cannot parse '%s' as int: %s", name, err)
342 case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
343 jn := data.(json.Number)
347 "error decoding json.Number into %s: %s", name, err)
352 "'%s' expected type '%s', got unconvertible type '%s'",
353 name, val.Type(), dataVal.Type())
359 func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
360 dataVal := reflect.ValueOf(data)
361 dataKind := getKind(dataVal)
364 case dataKind == reflect.Int:
366 if i < 0 && !d.config.WeaklyTypedInput {
367 return fmt.Errorf("cannot parse '%s', %d overflows uint",
370 val.SetUint(uint64(i))
371 case dataKind == reflect.Uint:
372 val.SetUint(dataVal.Uint())
373 case dataKind == reflect.Float32:
375 if f < 0 && !d.config.WeaklyTypedInput {
376 return fmt.Errorf("cannot parse '%s', %f overflows uint",
379 val.SetUint(uint64(f))
380 case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
386 case dataKind == reflect.String && d.config.WeaklyTypedInput:
387 i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
391 return fmt.Errorf("cannot parse '%s' as uint: %s", name, err)
395 "'%s' expected type '%s', got unconvertible type '%s'",
396 name, val.Type(), dataVal.Type())
402 func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
403 dataVal := reflect.ValueOf(data)
404 dataKind := getKind(dataVal)
407 case dataKind == reflect.Bool:
408 val.SetBool(dataVal.Bool())
409 case dataKind == reflect.Int && d.config.WeaklyTypedInput:
410 val.SetBool(dataVal.Int() != 0)
411 case dataKind == reflect.Uint && d.config.WeaklyTypedInput:
412 val.SetBool(dataVal.Uint() != 0)
413 case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
414 val.SetBool(dataVal.Float() != 0)
415 case dataKind == reflect.String && d.config.WeaklyTypedInput:
416 b, err := strconv.ParseBool(dataVal.String())
419 } else if dataVal.String() == "" {
422 return fmt.Errorf("cannot parse '%s' as bool: %s", name, err)
426 "'%s' expected type '%s', got unconvertible type '%s'",
427 name, val.Type(), dataVal.Type())
433 func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
434 dataVal := reflect.ValueOf(data)
435 dataKind := getKind(dataVal)
436 dataType := dataVal.Type()
439 case dataKind == reflect.Int:
440 val.SetFloat(float64(dataVal.Int()))
441 case dataKind == reflect.Uint:
442 val.SetFloat(float64(dataVal.Uint()))
443 case dataKind == reflect.Float32:
444 val.SetFloat(dataVal.Float())
445 case dataKind == reflect.Bool && d.config.WeaklyTypedInput:
451 case dataKind == reflect.String && d.config.WeaklyTypedInput:
452 f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits())
456 return fmt.Errorf("cannot parse '%s' as float: %s", name, err)
458 case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
459 jn := data.(json.Number)
460 i, err := jn.Float64()
463 "error decoding json.Number into %s: %s", name, err)
468 "'%s' expected type '%s', got unconvertible type '%s'",
469 name, val.Type(), dataVal.Type())
475 func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
476 valType := val.Type()
477 valKeyType := valType.Key()
478 valElemType := valType.Elem()
480 // By default we overwrite keys in the current map
483 // If the map is nil or we're purposely zeroing fields, make a new map
484 if valMap.IsNil() || d.config.ZeroFields {
485 // Make a new map to hold our result
486 mapType := reflect.MapOf(valKeyType, valElemType)
487 valMap = reflect.MakeMap(mapType)
491 dataVal := reflect.Indirect(reflect.ValueOf(data))
492 if dataVal.Kind() != reflect.Map {
493 // In weak mode, we accept a slice of maps as an input...
494 if d.config.WeaklyTypedInput {
495 switch dataVal.Kind() {
496 case reflect.Array, reflect.Slice:
497 // Special case for BC reasons (covered by tests)
498 if dataVal.Len() == 0 {
503 for i := 0; i < dataVal.Len(); i++ {
505 fmt.Sprintf("%s[%d]", name, i),
506 dataVal.Index(i).Interface(), val)
516 return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
520 errors := make([]string, 0)
522 for _, k := range dataVal.MapKeys() {
523 fieldName := fmt.Sprintf("%s[%s]", name, k)
525 // First decode the key into the proper type
526 currentKey := reflect.Indirect(reflect.New(valKeyType))
527 if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
528 errors = appendErrors(errors, err)
532 // Next decode the data into the proper type
533 v := dataVal.MapIndex(k).Interface()
534 currentVal := reflect.Indirect(reflect.New(valElemType))
535 if err := d.decode(fieldName, v, currentVal); err != nil {
536 errors = appendErrors(errors, err)
540 valMap.SetMapIndex(currentKey, currentVal)
543 // Set the built up map to the value
546 // If we had errors, return those
548 return &Error{errors}
554 func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error {
555 // Create an element of the concrete (non pointer) type and decode
556 // into that. Then set the value of the pointer to this type.
557 valType := val.Type()
558 valElemType := valType.Elem()
561 if realVal.IsNil() || d.config.ZeroFields {
562 realVal = reflect.New(valElemType)
565 if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
573 func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) error {
574 // Create an element of the concrete (non pointer) type and decode
575 // into that. Then set the value of the pointer to this type.
576 dataVal := reflect.Indirect(reflect.ValueOf(data))
577 if val.Type() != dataVal.Type() {
579 "'%s' expected type '%s', got unconvertible type '%s'",
580 name, val.Type(), dataVal.Type())
586 func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error {
587 dataVal := reflect.Indirect(reflect.ValueOf(data))
588 dataValKind := dataVal.Kind()
589 valType := val.Type()
590 valElemType := valType.Elem()
591 sliceType := reflect.SliceOf(valElemType)
594 if valSlice.IsNil() || d.config.ZeroFields {
596 if dataValKind != reflect.Array && dataValKind != reflect.Slice {
597 if d.config.WeaklyTypedInput {
599 // Empty maps turn into empty slices
600 case dataValKind == reflect.Map:
601 if dataVal.Len() == 0 {
602 val.Set(reflect.MakeSlice(sliceType, 0, 0))
606 // All other types we try to convert to the slice type
607 // and "lift" it into it. i.e. a string becomes a string slice.
609 // Just re-try this function with data as a slice.
610 return d.decodeSlice(name, []interface{}{data}, val)
615 "'%s': source data must be an array or slice, got %s", name, dataValKind)
619 // Make a new slice to hold our result, same size as the original data.
620 valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
623 // Accumulate any errors
624 errors := make([]string, 0)
626 for i := 0; i < dataVal.Len(); i++ {
627 currentData := dataVal.Index(i).Interface()
628 for valSlice.Len() <= i {
629 valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
631 currentField := valSlice.Index(i)
633 fieldName := fmt.Sprintf("%s[%d]", name, i)
634 if err := d.decode(fieldName, currentData, currentField); err != nil {
635 errors = appendErrors(errors, err)
639 // Finally, set the value to the slice we built up
642 // If there were errors, we return those
644 return &Error{errors}
650 func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error {
651 dataVal := reflect.Indirect(reflect.ValueOf(data))
653 // If the type of the value to write to and the data match directly,
654 // then we just set it directly instead of recursing into the structure.
655 if dataVal.Type() == val.Type() {
660 dataValKind := dataVal.Kind()
661 if dataValKind != reflect.Map {
662 return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind)
665 dataValType := dataVal.Type()
666 if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
668 "'%s' needs a map with string keys, has '%s' keys",
669 name, dataValType.Key().Kind())
672 dataValKeys := make(map[reflect.Value]struct{})
673 dataValKeysUnused := make(map[interface{}]struct{})
674 for _, dataValKey := range dataVal.MapKeys() {
675 dataValKeys[dataValKey] = struct{}{}
676 dataValKeysUnused[dataValKey.Interface()] = struct{}{}
679 errors := make([]string, 0)
681 // This slice will keep track of all the structs we'll be decoding.
682 // There can be more than one struct if there are embedded structs
683 // that are squashed.
684 structs := make([]reflect.Value, 1, 5)
687 // Compile the list of all the fields that we're going to be decoding
688 // from all the structs.
690 field reflect.StructField
694 for len(structs) > 0 {
695 structVal := structs[0]
696 structs = structs[1:]
698 structType := structVal.Type()
700 for i := 0; i < structType.NumField(); i++ {
701 fieldType := structType.Field(i)
702 fieldKind := fieldType.Type.Kind()
704 // If "squash" is specified in the tag, we squash the field down.
706 tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",")
707 for _, tag := range tagParts[1:] {
715 if fieldKind != reflect.Struct {
716 errors = appendErrors(errors,
717 fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
719 structs = append(structs, val.FieldByName(fieldType.Name))
724 // Normal struct field, store it away
725 fields = append(fields, field{fieldType, structVal.Field(i)})
729 // for fieldType, field := range fields {
730 for _, f := range fields {
731 field, fieldValue := f.field, f.val
732 fieldName := field.Name
734 tagValue := field.Tag.Get(d.config.TagName)
735 tagValue = strings.SplitN(tagValue, ",", 2)[0]
740 rawMapKey := reflect.ValueOf(fieldName)
741 rawMapVal := dataVal.MapIndex(rawMapKey)
742 if !rawMapVal.IsValid() {
743 // Do a slower search by iterating over each key and
744 // doing case-insensitive search.
745 for dataValKey := range dataValKeys {
746 mK, ok := dataValKey.Interface().(string)
752 if strings.EqualFold(mK, fieldName) {
753 rawMapKey = dataValKey
754 rawMapVal = dataVal.MapIndex(dataValKey)
759 if !rawMapVal.IsValid() {
760 // There was no matching key in the map for the value in
761 // the struct. Just ignore.
766 // Delete the key we're using from the unused map so we stop tracking
767 delete(dataValKeysUnused, rawMapKey.Interface())
769 if !fieldValue.IsValid() {
770 // This should never happen
771 panic("field is not valid")
774 // If we can't set the field, then it is unexported or something,
775 // and we just continue onwards.
776 if !fieldValue.CanSet() {
780 // If the name is empty string, then we're at the root, and we
781 // don't dot-join the fields.
783 fieldName = fmt.Sprintf("%s.%s", name, fieldName)
786 if err := d.decode(fieldName, rawMapVal.Interface(), fieldValue); err != nil {
787 errors = appendErrors(errors, err)
791 if d.config.ErrorUnused && len(dataValKeysUnused) > 0 {
792 keys := make([]string, 0, len(dataValKeysUnused))
793 for rawKey := range dataValKeysUnused {
794 keys = append(keys, rawKey.(string))
798 err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", "))
799 errors = appendErrors(errors, err)
803 return &Error{errors}
806 // Add the unused keys to the list of unused keys if we're tracking metadata
807 if d.config.Metadata != nil {
808 for rawKey := range dataValKeysUnused {
809 key := rawKey.(string)
811 key = fmt.Sprintf("%s.%s", name, key)
814 d.config.Metadata.Unused = append(d.config.Metadata.Unused, key)
821 func getKind(val reflect.Value) reflect.Kind {
825 case kind >= reflect.Int && kind <= reflect.Int64:
827 case kind >= reflect.Uint && kind <= reflect.Uint64:
829 case kind >= reflect.Float32 && kind <= reflect.Float64:
830 return reflect.Float32