Skip to content

Commit

Permalink
Support optional boolean query parameters (i.e. pointers)
Browse files Browse the repository at this point in the history
This commit adds support for boolean query parameters that are optional,
meaning their type is `*bool` rather than `bool`. This is useful in
cases where an API must know whether the parameter was actually
provided, even if it was false, and not just received a zero value.
  • Loading branch information
ido50 committed Sep 1, 2020
1 parent c8d9cd1 commit 067c168
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func unmarshalField(
valueField.SetFloat(value)
case reflect.Bool:
valueField.SetBool(boolRegex.MatchString(strings.ToLower(params[param])))
case reflect.Ptr:
if typeField.Elem().Kind() == reflect.Bool {
if val, ok := params[param]; ok {
b := boolRegex.MatchString(strings.ToLower(val))
valueField.Set(reflect.ValueOf(&b))
}
}
case reflect.Slice:
// we'll be extracting values from multiParam, generating a slice and
// putting it in valueField
Expand Down
6 changes: 6 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func Test_UnmarshalRequest(t *testing.T) {
"page": "2",
"page_size": "30",
"const": "two",
"bool": "true",
"pbool1": "0",
},
MultiValueQueryStringParameters: map[string][]string{
"terms": []string{"one", "two"},
Expand All @@ -43,6 +45,10 @@ func Test_UnmarshalRequest(t *testing.T) {
assert.Equal(t, int64(30), input.PageSize, "PageSize must be parsed from query")
assert.Equal(t, "en-us", input.Language, "Language must be parsed from headers")
assert.Equal(t, mockConstTwo, input.Const, "Const must be parsed from query")
assert.True(t, input.Bool, "Bool must be true")
assert.NotNil(t, input.PBoolOne, "PBoolOne must not be nil")
assert.False(t, *input.PBoolOne, "PBoolOne must be *false")
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")
assert.DeepEqual(t, []string{"gzip", "deflate"}, input.Encoding, "Encoding must be parsed from multiple header params")
Expand Down
3 changes: 3 additions & 0 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type mockListRequest struct {
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"`
Language string `lambda:"header.Accept-Language"`
Encoding []string `lambda:"header.Accept-Encoding"`
}
Expand Down

0 comments on commit 067c168

Please sign in to comment.