Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
// This is the basic type of the destination object.
t := v.Type()

if t.Kind() == reflect.Struct {
if t.Kind() == reflect.Struct || t.Kind() == reflect.Map {
// We've got a destination object, we'll create a JSON representation
// of the input value, and let the json library deal with the unmarshaling
parts, err := splitStyledParameter(style, opts.Explode, true, paramName, value)
Expand Down
53 changes: 45 additions & 8 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,52 @@ func TestBindParamsToExplodedObject(t *testing.T) {
}

func TestBindStyledParameterWithLocation(t *testing.T) {
expectedBig := big.NewInt(12345678910)
t.Run("bigNumber", func(t *testing.T) {
expectedBig := big.NewInt(12345678910)
var dstBigNumber big.Int

err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: false,
Required: false,
})
assert.NoError(t, err)
assert.Equal(t, *expectedBig, dstBigNumber)
})

var dstBigNumber big.Int
t.Run("object", func(t *testing.T) {
type Object struct {
Key1 string `json:"key1"`
Key2 string `json:"key2"`
}
expectedObject := Object{
Key1: "value1",
Key2: "42",
}
var dstObject Object

err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: false,
Required: false,
err := BindStyledParameterWithOptions("simple", "map", "key1,value1,key2,42", &dstObject, BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: false,
Required: false,
})
assert.NoError(t, err)
assert.Equal(t, *&expectedObject, dstObject)
})

t.Run("map", func(t *testing.T) {
expectedMap := map[string]any{
"key1": "value1",
"key2": "42",
}
var dstMap map[string]any

err := BindStyledParameterWithOptions("simple", "map", "key1,value1,key2,42", &dstMap, BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: false,
Required: false,
})
assert.NoError(t, err)
assert.Equal(t, *&expectedMap, dstMap)
})
assert.NoError(t, err)
assert.Equal(t, *expectedBig, dstBigNumber)
}