Skip to content

Commit 1b61163

Browse files
committed
Change styleMap to iterate via reflection
1 parent d741dd7 commit 1b61163

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

styleparam.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"time"
2828

2929
"github.com/google/uuid"
30+
3031
"github.com/oapi-codegen/runtime/types"
3132
)
3233

@@ -97,12 +98,7 @@ func StyleParamWithLocation(style string, explode bool, paramName string, paramL
9798
case reflect.Struct:
9899
return styleStruct(style, explode, paramName, paramLocation, value)
99100
case reflect.Map:
100-
dict := make(map[string]any, v.Len())
101-
for _, key := range v.MapKeys() {
102-
// the key is guaranteed to be a string
103-
dict[key.String()] = v.MapIndex(key).Interface()
104-
}
105-
return styleMap(style, explode, paramName, paramLocation, dict)
101+
return styleMap(style, explode, paramName, paramLocation, value)
106102
default:
107103
return stylePrimitive(style, explode, paramName, paramLocation, value)
108104
}
@@ -293,19 +289,15 @@ func styleMap(style string, explode bool, paramName string, paramLocation ParamL
293289
}
294290
return MarshalDeepObject(value, paramName)
295291
}
296-
297-
dict, ok := value.(map[string]interface{})
298-
if !ok {
299-
return "", errors.New("map not of type map[string]interface{}")
300-
}
292+
v := reflect.ValueOf(value)
301293

302294
fieldDict := make(map[string]string)
303-
for fieldName, value := range dict {
304-
str, err := primitiveToString(value)
295+
for _, fieldName := range v.MapKeys() {
296+
str, err := primitiveToString(v.MapIndex(fieldName).Interface())
305297
if err != nil {
306298
return "", fmt.Errorf("error formatting '%s': %s", paramName, err)
307299
}
308-
fieldDict[fieldName] = str
300+
fieldDict[fieldName.String()] = str
309301
}
310302
return processFieldDict(style, explode, paramName, paramLocation, fieldDict)
311303
}

0 commit comments

Comments
 (0)