MCPcopy
hub / github.com/mitchellh/mapstructure / decodeMapFromMap

Method decodeMapFromMap

mapstructure.go:854–906  ·  view source on GitHub ↗
(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value)

Source from the content-addressed store, hash-verified

852}
853
854func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
855 valType := val.Type()
856 valKeyType := valType.Key()
857 valElemType := valType.Elem()
858
859 // Accumulate errors
860 errors := make([]string, 0)
861
862 // If the input data is empty, then we just match what the input data is.
863 if dataVal.Len() == 0 {
864 if dataVal.IsNil() {
865 if !val.IsNil() {
866 val.Set(dataVal)
867 }
868 } else {
869 // Set to empty allocated value
870 val.Set(valMap)
871 }
872
873 return nil
874 }
875
876 for _, k := range dataVal.MapKeys() {
877 fieldName := name + "[" + k.String() + "]"
878
879 // First decode the key into the proper type
880 currentKey := reflect.Indirect(reflect.New(valKeyType))
881 if err := d.decode(fieldName, k.Interface(), currentKey); err != nil {
882 errors = appendErrors(errors, err)
883 continue
884 }
885
886 // Next decode the data into the proper type
887 v := dataVal.MapIndex(k).Interface()
888 currentVal := reflect.Indirect(reflect.New(valElemType))
889 if err := d.decode(fieldName, v, currentVal); err != nil {
890 errors = appendErrors(errors, err)
891 continue
892 }
893
894 valMap.SetMapIndex(currentKey, currentVal)
895 }
896
897 // Set the built up map to the value
898 val.Set(valMap)
899
900 // If we had errors, return those
901 if len(errors) > 0 {
902 return &Error{errors}
903 }
904
905 return nil
906}
907
908func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
909 typ := dataVal.Type()

Callers 1

decodeMapMethod · 0.95

Calls 2

decodeMethod · 0.95
appendErrorsFunction · 0.85

Tested by

no test coverage detected