Skip to content

Commit

Permalink
Merge pull request #9 from owenrumney/owenr-add-support-for-comma-sep…
Browse files Browse the repository at this point in the history
…erated-string

Add support for comma string
  • Loading branch information
oranmoshai authored Nov 16, 2021
2 parents f5d02b2 + c5f4a5e commit d06f933
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
14 changes: 14 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ func unmarshalField(
}

valueField.Set(slice)
} else {
str, ok := params[param]
if ok {
stringParts := strings.Split(str, ",")
slice := reflect.MakeSlice(typeField, len(stringParts), len(stringParts))

for i, p := range stringParts {
inVal := reflect.ValueOf(p)
asVal := inVal.Convert(typeField.Elem())
slice.Index(i).Set(asVal)
}

valueField.Set(slice)
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ func Test_UnmarshalRequest(t *testing.T) {
"id": "fake-scan-id",
},
QueryStringParameters: map[string]string{
"page": "2",
"page_size": "30",
"const": "two",
"bool": "true",
"pbool1": "0",
"time": "2021-11-01T11:11:11.000Z",
"alias": "hello",
"alias_ptr": "world",
"page": "2",
"page_size": "30",
"const": "two",
"bool": "true",
"pbool1": "0",
"time": "2021-11-01T11:11:11.000Z",
"alias": "hello",
"alias_ptr": "world",
"commaSplit": "one,two,three",
},
MultiValueQueryStringParameters: map[string][]string{
"terms": []string{"one", "two"},
Expand Down Expand Up @@ -60,6 +61,7 @@ func Test_UnmarshalRequest(t *testing.T) {
assert.Equal(t, input.Alias, stringAliasExample("hello"))
assert.NotNil(t, input.AliasPtr)
assert.Equal(t, *input.AliasPtr, aliasExample)
assert.DeepEqual(t, []Number{numberOne, numberTwo, numberThree}, input.CommaSplit, "CommaSplit must have 2 items")
assert.Equal(t, (*bool)(nil), input.PBoolTwo, "PBoolTwo must be nil")
assert.DeepEqual(t, []string{"one", "two"}, input.Terms, "Terms must be parsed from multiple query params")
assert.DeepEqual(t, []float64{1.2, 3.5, 666.666}, input.Numbers, "Numbers must be parsed from multiple query params")
Expand Down
37 changes: 23 additions & 14 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ const (
mockConstTwo mockConst = "two"
)

type Number string

const (
numberOne Number = "one"
numberTwo Number = "two"
numberThree Number = "three"
)

type mockListRequest struct {
ID string `lambda:"path.id"`
Page int64 `lambda:"query.page"`
PageSize int64 `lambda:"query.page_size"`
Terms []string `lambda:"query.terms"`
Numbers []float64 `lambda:"query.numbers"`
Const mockConst `lambda:"query.const"`
Bool bool `lambda:"query.bool"`
PBoolOne *bool `lambda:"query.pbool1"`
PBoolTwo *bool `lambda:"query.pbool2"`
Time *time.Time `lambda:"query.time"`
Alias stringAliasExample `lambda:"query.alias"`
AliasPtr *stringAliasExample `lambda:"query.alias_ptr"`
Language string `lambda:"header.Accept-Language"`
Encoding []string `lambda:"header.Accept-Encoding"`
ID string `lambda:"path.id"`
Page int64 `lambda:"query.page"`
PageSize int64 `lambda:"query.page_size"`
Terms []string `lambda:"query.terms"`
Numbers []float64 `lambda:"query.numbers"`
Const mockConst `lambda:"query.const"`
Bool bool `lambda:"query.bool"`
PBoolOne *bool `lambda:"query.pbool1"`
PBoolTwo *bool `lambda:"query.pbool2"`
Time *time.Time `lambda:"query.time"`
Alias stringAliasExample `lambda:"query.alias"`
AliasPtr *stringAliasExample `lambda:"query.alias_ptr"`
CommaSplit []Number `lambda:"query.commaSplit"`
Language string `lambda:"header.Accept-Language"`
Encoding []string `lambda:"header.Accept-Encoding"`
}

type mockGetRequest struct {
Expand Down

0 comments on commit d06f933

Please sign in to comment.