MCPcopy
hub / github.com/gin-gonic/gin / mapping

Function mapping

binding/form_mapping.go:84–136  ·  view source on GitHub ↗
(value reflect.Value, field reflect.StructField, setter setter, tag string)

Source from the content-addressed store, hash-verified

82}
83
84func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
85 if field.Tag.Get(tag) == "-" { // just ignoring this field
86 return false, nil
87 }
88
89 vKind := value.Kind()
90
91 if vKind == reflect.Ptr {
92 var isNew bool
93 vPtr := value
94 if value.IsNil() {
95 isNew = true
96 vPtr = reflect.New(value.Type().Elem())
97 }
98 isSet, err := mapping(vPtr.Elem(), field, setter, tag)
99 if err != nil {
100 return false, err
101 }
102 if isNew && isSet {
103 value.Set(vPtr)
104 }
105 return isSet, nil
106 }
107
108 if vKind != reflect.Struct || !field.Anonymous {
109 ok, err := tryToSetValue(value, field, setter, tag)
110 if err != nil {
111 return false, err
112 }
113 if ok {
114 return true, nil
115 }
116 }
117
118 if vKind == reflect.Struct {
119 tValue := value.Type()
120
121 var isSet bool
122 for i := range value.NumField() {
123 sf := tValue.Field(i)
124 if sf.PkgPath != "" && !sf.Anonymous { // unexported
125 continue
126 }
127 ok, err := mapping(value.Field(i), sf, setter, tag)
128 if err != nil {
129 return false, err
130 }
131 isSet = isSet || ok
132 }
133 return isSet, nil
134 }
135 return false, nil
136}
137
138type setOptions struct {
139 isDefaultExists bool

Callers 2

mappingByPtrFunction · 0.85
TestMappingBaseTypesFunction · 0.85

Calls 4

tryToSetValueFunction · 0.85
TypeMethod · 0.80
SetMethod · 0.80
GetMethod · 0.45

Tested by 1

TestMappingBaseTypesFunction · 0.68