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
12 changes: 10 additions & 2 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,16 @@ func BindStyledParameterWithOptions(style string, paramName string, value string
return bindSplitPartsToDestinationArray(parts, dest)
}

// Try to bind the remaining types as a base type.
return BindStringToObject(value, dest)
// For primitive types, we still need to strip style prefixes (e.g. label's
// leading "." or matrix's ";paramName=") before binding.
parts, err := splitStyledParameter(style, opts.Explode, false, paramName, value)
if err != nil {
return fmt.Errorf("error splitting parameter '%s': %w", paramName, err)
}
if len(parts) != 1 {
return fmt.Errorf("parameter '%s': expected single value, got %d parts", paramName, len(parts))
}
return BindStringToObject(parts[0], dest)
}

// This is a complex set of operations, but each given parameter style can be
Expand Down
48 changes: 48 additions & 0 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,54 @@ func TestRoundTripQueryParameter(t *testing.T) {
})
}

func TestBindStyledParameterWithOptions_LabelPrimitive(t *testing.T) {
tests := []struct {
name string
explode bool
value string
want int32
}{
{"non-exploded", false, ".5", 5},
{"exploded", true, ".5", 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var dest int32
err := BindStyledParameterWithOptions("label", "param", tt.value, &dest, BindStyledParameterOptions{
ParamLocation: ParamLocationPath,
Explode: tt.explode,
Required: true,
})
require.NoError(t, err)
assert.Equal(t, tt.want, dest)
})
}
}

func TestBindStyledParameterWithOptions_MatrixPrimitive(t *testing.T) {
tests := []struct {
name string
explode bool
value string
want int32
}{
{"non-exploded", false, ";param=5", 5},
{"exploded", true, ";param=5", 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var dest int32
err := BindStyledParameterWithOptions("matrix", "param", tt.value, &dest, BindStyledParameterOptions{
ParamLocation: ParamLocationPath,
Explode: tt.explode,
Required: true,
})
require.NoError(t, err)
assert.Equal(t, tt.want, dest)
})
}
}

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