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)
| 126 | // Parse data into the map |
| 127 | // thanks to https://github.com/gin-gonic/gin/blob/master/binding/binding.go |
| 128 | func 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 | |
| 180 | func parseParamSquareBrackets(k string) (string, error) { |
| 181 | bb := bytebufferpool.Get() |