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
26 changes: 20 additions & 6 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,20 @@ func TestSplitParameter(t *testing.T) {

func TestBindQueryParameter(t *testing.T) {
t.Run("deepObject", func(t *testing.T) {
type Object struct {
Count int `json:"count"`
}
type Nested struct {
Object Object `json:"object"`
Objects []Object `json:"objects"`
}
type ID struct {
FirstName *string `json:"firstName"`
LastName *string `json:"lastName"`
Role string `json:"role"`
Birthday *types.Date `json:"birthday"`
Married *MockBinder `json:"married"`
Nested Nested `json:"nested"`
}

expectedName := "Alex"
Expand All @@ -307,16 +315,23 @@ func TestBindQueryParameter(t *testing.T) {
Role: "admin",
Birthday: &types.Date{Time: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)},
Married: &MockBinder{time.Date(2020, 2, 2, 0, 0, 0, 0, time.UTC)},
Nested: Nested{
Object: Object{Count: 123},
Objects: []Object{{Count: 1}, {Count: 2}},
},
}

actual := new(ID)
paramName := "id"
queryParams := url.Values{
"id[firstName]": {"Alex"},
"id[role]": {"admin"},
"foo": {"bar"},
"id[birthday]": {"2020-01-01"},
"id[married]": {"2020-02-02"},
"id[firstName]": {"Alex"},
"id[role]": {"admin"},
"foo": {"bar"},
"id[birthday]": {"2020-01-01"},
"id[married]": {"2020-02-02"},
"id[nested][object][count]": {"123"},
"id[nested][objects][0][count]": {"1"},
"id[nested][objects][1][count]": {"2"},
}

err := BindQueryParameter("deepObject", true, false, paramName, queryParams, &actual)
Expand Down Expand Up @@ -356,7 +371,6 @@ func TestBindQueryParameter(t *testing.T) {
assert.Error(t, err)
err = BindQueryParameter("form", true, true, "notfound", queryParams, &optionalNumber)
assert.Error(t, err)

})
}

Expand Down
12 changes: 3 additions & 9 deletions deepobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (f *fieldOrValue) appendPathValue(path []string, value string) {
}

func makeFieldOrValue(paths [][]string, values []string) fieldOrValue {

f := fieldOrValue{
fields: make(map[string]fieldOrValue),
}
Expand Down Expand Up @@ -193,7 +192,7 @@ func fieldIndicesByJSONTag(i interface{}) (map[string]int, error) {
}

func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
//t := reflect.TypeOf(dst)
// t := reflect.TypeOf(dst)
v := reflect.ValueOf(dst)

iv := reflect.Indirect(v)
Expand Down Expand Up @@ -337,22 +336,17 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
func assignSlice(dst reflect.Value, pathValues fieldOrValue) error {
// Gather up the values
nValues := len(pathValues.fields)
values := make([]string, nValues)

// We expect to have consecutive array indices in the map
for i := 0; i < nValues; i++ {
indexStr := strconv.Itoa(i)
fv, found := pathValues.fields[indexStr]
if !found {
return errors.New("array deepObjects must have consecutive indices")
}
values[i] = fv.value
}

// This could be cleaner, but we can call into assignPathValues to
// avoid recreating this logic.
for i := 0; i < nValues; i++ {
dstElem := dst.Index(i).Addr()
err := assignPathValues(dstElem.Interface(), fieldOrValue{value: values[i]})
err := assignPathValues(dstElem.Interface(), fv)
if err != nil {
return fmt.Errorf("error binding array: %w", err)
}
Expand Down
62 changes: 47 additions & 15 deletions deepobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,45 @@ import (
"github.com/stretchr/testify/require"
)

type InnerArrayObject struct {
Names []string `json:"names"`
}

type InnerObject struct {
Name string
ID int
}

type InnerObject2 struct {
Foo string
Is bool
}

type InnerObject3 struct {
Foo string
Count *int `json:"count,omitempty"`
}

// These are all possible field types, mandatory and optional.
type AllFields struct {
I int `json:"i"`
Oi *int `json:"oi,omitempty"`
F float32 `json:"f"`
Of *float32 `json:"of,omitempty"`
B bool `json:"b"`
Ob *bool `json:"ob,omitempty"`
As []string `json:"as"`
Oas *[]string `json:"oas,omitempty"`
O InnerObject `json:"o"`
Oo *InnerObject `json:"oo,omitempty"`
D MockBinder `json:"d"`
Od *MockBinder `json:"od,omitempty"`
M map[string]int `json:"m"`
Om *map[string]int `json:"om,omitempty"`
I int `json:"i"`
Oi *int `json:"oi,omitempty"`
Ab *[]bool `json:"ab,omitempty"`
F float32 `json:"f"`
Of *float32 `json:"of,omitempty"`
B bool `json:"b"`
Ob *bool `json:"ob,omitempty"`
As []string `json:"as"`
Oas *[]string `json:"oas,omitempty"`
O InnerObject `json:"o"`
Ao []InnerObject2 `json:"ao"`
Aop *[]InnerObject3 `json:"aop"`
Onas InnerArrayObject `json:"onas"`
Oo *InnerObject `json:"oo,omitempty"`
D MockBinder `json:"d"`
Od *MockBinder `json:"od,omitempty"`
M map[string]int `json:"m"`
Om *map[string]int `json:"om,omitempty"`
}

func TestDeepObject(t *testing.T) {
Expand All @@ -47,19 +65,33 @@ func TestDeepObject(t *testing.T) {
}
d := MockBinder{Time: time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)}

two := 2

srcObj := AllFields{
I: 12,
Oi: &oi,
F: 4.2,
Of: &of,
B: true,
Ob: &ob,
Ab: &[]bool{true},
As: []string{"hello", "world"},
Oas: &oas,
O: InnerObject{
Name: "Joe Schmoe",
ID: 456,
},
Ao: []InnerObject2{
{Foo: "bar", Is: true},
{Foo: "baz"},
},
Aop: &[]InnerObject3{
{Foo: "a"},
{Foo: "b", Count: &two},
},
Onas: InnerArrayObject{
Names: []string{"Bill", "Frank"},
},
Oo: &oo,
D: d,
Od: &d,
Expand All @@ -69,7 +101,7 @@ func TestDeepObject(t *testing.T) {

marshaled, err := MarshalDeepObject(srcObj, "p")
require.NoError(t, err)
t.Log(marshaled)
require.EqualValues(t, "p[ab][0]=true&p[ao][0][Foo]=bar&p[ao][0][Is]=true&p[ao][1][Foo]=baz&p[ao][1][Is]=false&p[aop][0][Foo]=a&p[aop][1][Foo]=b&p[aop][1][count]=2&p[as][0]=hello&p[as][1]=world&p[b]=true&p[d]=2020-02-01&p[f]=4.2&p[i]=12&p[m][additional]=1&p[o][ID]=456&p[o][Name]=Joe Schmoe&p[oas][0]=foo&p[oas][1]=bar&p[ob]=true&p[od]=2020-02-01&p[of]=3.7&p[oi]=5&p[om][additional]=1&p[onas][names][0]=Bill&p[onas][names][1]=Frank&p[oo][ID]=123&p[oo][Name]=Marcin Romaszewicz", marshaled)

params := make(url.Values)
marshaledParts := strings.Split(marshaled, "&")
Expand Down