Skip to content

Commit 179c5e9

Browse files
JoZieJohannes Ziegenbalg
andauthored
feat: Allow BindStyledParameterWithOptions to fill maps (#72)
Co-authored-by: Johannes Ziegenbalg <Johannes.Ziegenbalg@wandelbots.com>
1 parent cfb0873 commit 179c5e9

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

bindparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
109109
// This is the basic type of the destination object.
110110
t := v.Type()
111111

112-
if t.Kind() == reflect.Struct {
112+
if t.Kind() == reflect.Struct || t.Kind() == reflect.Map {
113113
// We've got a destination object, we'll create a JSON representation
114114
// of the input value, and let the json library deal with the unmarshaling
115115
parts, err := splitStyledParameter(style, opts.Explode, true, paramName, value)

bindparam_test.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,15 +509,52 @@ func TestBindParamsToExplodedObject(t *testing.T) {
509509
}
510510

511511
func TestBindStyledParameterWithLocation(t *testing.T) {
512-
expectedBig := big.NewInt(12345678910)
512+
t.Run("bigNumber", func(t *testing.T) {
513+
expectedBig := big.NewInt(12345678910)
514+
var dstBigNumber big.Int
515+
516+
err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
517+
ParamLocation: ParamLocationUndefined,
518+
Explode: false,
519+
Required: false,
520+
})
521+
assert.NoError(t, err)
522+
assert.Equal(t, *expectedBig, dstBigNumber)
523+
})
513524

514-
var dstBigNumber big.Int
525+
t.Run("object", func(t *testing.T) {
526+
type Object struct {
527+
Key1 string `json:"key1"`
528+
Key2 string `json:"key2"`
529+
}
530+
expectedObject := Object{
531+
Key1: "value1",
532+
Key2: "42",
533+
}
534+
var dstObject Object
515535

516-
err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
517-
ParamLocation: ParamLocationUndefined,
518-
Explode: false,
519-
Required: false,
536+
err := BindStyledParameterWithOptions("simple", "map", "key1,value1,key2,42", &dstObject, BindStyledParameterOptions{
537+
ParamLocation: ParamLocationUndefined,
538+
Explode: false,
539+
Required: false,
540+
})
541+
assert.NoError(t, err)
542+
assert.Equal(t, *&expectedObject, dstObject)
543+
})
544+
545+
t.Run("map", func(t *testing.T) {
546+
expectedMap := map[string]any{
547+
"key1": "value1",
548+
"key2": "42",
549+
}
550+
var dstMap map[string]any
551+
552+
err := BindStyledParameterWithOptions("simple", "map", "key1,value1,key2,42", &dstMap, BindStyledParameterOptions{
553+
ParamLocation: ParamLocationUndefined,
554+
Explode: false,
555+
Required: false,
556+
})
557+
assert.NoError(t, err)
558+
assert.Equal(t, *&expectedMap, dstMap)
520559
})
521-
assert.NoError(t, err)
522-
assert.Equal(t, *expectedBig, dstBigNumber)
523560
}

0 commit comments

Comments
 (0)