MCPcopy
hub / github.com/gofiber/fiber / parseToMap

Function parseToMap

binder/mapping.go:128–178  ·  view source on GitHub ↗

Parse data into the map thanks to https://github.com/gin-gonic/gin/blob/master/binding/binding.go

(target reflect.Value, data map[string][]string)

Source from the content-addressed store, hash-verified

126// Parse data into the map
127// thanks to https://github.com/gin-gonic/gin/blob/master/binding/binding.go
128func parseToMap(target reflect.Value, data map[string][]string) error {
129 if !target.IsValid() {
130 return ErrInvalidDestinationValue
131 }
132
133 if target.Kind() == reflect.Interface && !target.IsNil() {
134 target = target.Elem()
135 }
136
137 if target.Kind() != reflect.Map || target.Type().Key().Kind() != reflect.String {
138 return nil // nothing to do for non-map destinations
139 }
140
141 if target.IsNil() {
142 if !target.CanSet() {
143 return ErrMapNilDestination
144 }
145 target.Set(reflect.MakeMap(target.Type()))
146 }
147
148 switch target.Type().Elem().Kind() {
149 case reflect.Slice:
150 newMap, ok := target.Interface().(map[string][]string)
151 if !ok {
152 return ErrMapNotConvertible
153 }
154
155 maps.Copy(newMap, data)
156 case reflect.String:
157 newMap, ok := target.Interface().(map[string]string)
158 if !ok {
159 return ErrMapNotConvertible
160 }
161
162 for k, v := range data {
163 if len(v) == 0 {
164 newMap[k] = ""
165 continue
166 }
167 newMap[k] = v[len(v)-1]
168 }
169 default:
170 // Interface element maps (e.g. map[string]any) are left untouched because
171 // the binder cannot safely infer element conversions without mutating
172 // caller-provided values. These destinations therefore see a successful
173 // no-op parse.
174 return nil // it's not necessary to check all types
175 }
176
177 return nil
178}
179
180func parseParamSquareBrackets(k string) (string, error) {
181 bb := bytebufferpool.Get()

Callers 4

parseFunction · 0.85
Test_parseToMapFunction · 0.85
Test_parseToMap_ExtendedFunction · 0.85

Calls 2

TypeMethod · 0.65
SetMethod · 0.65

Tested by 3

Test_parseToMapFunction · 0.68
Test_parseToMap_ExtendedFunction · 0.68