diff --git a/bindparam_test.go b/bindparam_test.go index eec3d3a..079332f 100644 --- a/bindparam_test.go +++ b/bindparam_test.go @@ -294,12 +294,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" @@ -308,16 +316,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) diff --git a/deepobject_test.go b/deepobject_test.go index 7490203..2646ad4 100644 --- a/deepobject_test.go +++ b/deepobject_test.go @@ -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) { @@ -47,6 +65,8 @@ 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, @@ -54,12 +74,24 @@ func TestDeepObject(t *testing.T) { 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, @@ -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, "&")