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

Method decodePtr

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

Source from the content-addressed store, hash-verified

1016}
1017
1018func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) (bool, error) {
1019 // If the input data is nil, then we want to just set the output
1020 // pointer to be nil as well.
1021 isNil := data == nil
1022 if !isNil {
1023 switch v := reflect.Indirect(reflect.ValueOf(data)); v.Kind() {
1024 case reflect.Chan,
1025 reflect.Func,
1026 reflect.Interface,
1027 reflect.Map,
1028 reflect.Ptr,
1029 reflect.Slice:
1030 isNil = v.IsNil()
1031 }
1032 }
1033 if isNil {
1034 if !val.IsNil() && val.CanSet() {
1035 nilValue := reflect.New(val.Type()).Elem()
1036 val.Set(nilValue)
1037 }
1038
1039 return true, nil
1040 }
1041
1042 // Create an element of the concrete (non pointer) type and decode
1043 // into that. Then set the value of the pointer to this type.
1044 valType := val.Type()
1045 valElemType := valType.Elem()
1046 if val.CanSet() {
1047 realVal := val
1048 if realVal.IsNil() || d.config.ZeroFields {
1049 realVal = reflect.New(valElemType)
1050 }
1051
1052 if err := d.decode(name, data, reflect.Indirect(realVal)); err != nil {
1053 return false, err
1054 }
1055
1056 val.Set(realVal)
1057 } else {
1058 if err := d.decode(name, data, reflect.Indirect(val)); err != nil {
1059 return false, err
1060 }
1061 }
1062 return false, nil
1063}
1064
1065func (d *Decoder) decodeFunc(name string, data interface{}, val reflect.Value) error {
1066 // Create an element of the concrete (non pointer) type and decode

Callers 1

decodeMethod · 0.95

Calls 1

decodeMethod · 0.95

Tested by

no test coverage detected