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

Method decodeMap

mapstructure.go:799–833  ·  view source on GitHub ↗
(name string, data interface{}, val reflect.Value)

Source from the content-addressed store, hash-verified

797}
798
799func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
800 valType := val.Type()
801 valKeyType := valType.Key()
802 valElemType := valType.Elem()
803
804 // By default we overwrite keys in the current map
805 valMap := val
806
807 // If the map is nil or we're purposely zeroing fields, make a new map
808 if valMap.IsNil() || d.config.ZeroFields {
809 // Make a new map to hold our result
810 mapType := reflect.MapOf(valKeyType, valElemType)
811 valMap = reflect.MakeMap(mapType)
812 }
813
814 // Check input type and based on the input type jump to the proper func
815 dataVal := reflect.Indirect(reflect.ValueOf(data))
816 switch dataVal.Kind() {
817 case reflect.Map:
818 return d.decodeMapFromMap(name, dataVal, val, valMap)
819
820 case reflect.Struct:
821 return d.decodeMapFromStruct(name, dataVal, val, valMap)
822
823 case reflect.Array, reflect.Slice:
824 if d.config.WeaklyTypedInput {
825 return d.decodeMapFromSlice(name, dataVal, val, valMap)
826 }
827
828 fallthrough
829
830 default:
831 return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
832 }
833}
834
835func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
836 // Special case for BC reasons (covered by tests)

Callers 2

decodeMethod · 0.95
decodeStructFromMapMethod · 0.95

Calls 3

decodeMapFromMapMethod · 0.95
decodeMapFromStructMethod · 0.95
decodeMapFromSliceMethod · 0.95

Tested by

no test coverage detected