From c5f4a5e8887f6cef7a800e2f775873c95478f58e Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Tue, 16 Nov 2021 11:39:16 +0000 Subject: [PATCH] Add support for comma string - used in examples like severity=low,high --- decoder.go | 14 ++++++++++++++ decoder_test.go | 18 ++++++++++-------- structs_test.go | 37 +++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/decoder.go b/decoder.go index 5f7ddb6..7340ca6 100644 --- a/decoder.go +++ b/decoder.go @@ -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) + } } } diff --git a/decoder_test.go b/decoder_test.go index 87fa487..b6355bf 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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"}, @@ -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") diff --git a/structs_test.go b/structs_test.go index c4e7985..b2cde5d 100644 --- a/structs_test.go +++ b/structs_test.go @@ -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 {