Skip to content

Commit 79a3be6

Browse files
committed
boolean support
1 parent fcc0044 commit 79a3be6

File tree

8 files changed

+167
-95
lines changed

8 files changed

+167
-95
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ vendor:
2626

2727
.PHONY: gentool
2828
gentool:
29-
docker build -t infoblox/atlas-gentool:test-query .
29+
docker build -t infoblox/atlas-gentool:atlas-validate-query-dev .
3030
docker image prune -f --filter label=stage=server-intermediate

example/example.pb.atlas.query.validate.go

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ message User {
2828
Address work_address = 12;
2929
string company = 13 [(atlas.query.validate).filtering.deny = IEQ];
3030
string nationality = 14 [(atlas.query.validate).filtering.deny = IN];
31+
bool boolean_field = 15;
3132
}
3233

3334
message CustomType {

example/example_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func TestValidateFiltering(t *testing.T) {
3838
{`nationality in ["American", "Сhinese"]`, true},
3939
{`weight in [10, 20, 30]`, false},
4040
{`comment in [10, 20, 30]`, true},
41+
{`boolean_field in ["true", "1", "t", "True", "TRUE", "false", "0", "f", "False", "FALSE"]`, false},
42+
{`boolean_field in ["tRuE"]`, true},
43+
{`boolean_field=="True"`, false},
44+
{`boolean_field=="Blah"`, true},
4145
}
4246

4347
for _, test := range tests {

options/query_validate.pb.go

Lines changed: 44 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

options/query_validate.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ message QueryValidate {
3939
DEFAULT = 0;
4040
STRING = 1;
4141
NUMBER = 2;
42+
BOOL = 3;
4243
}
4344
ValueType value_type = 4;
4445
string value_type_url = 5;

options/validate.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package options
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/infobloxopen/atlas-app-toolkit/query"
@@ -53,9 +54,16 @@ func ValidateFiltering(f *query.Filtering, messageInfo map[string]FilteringOptio
5354

5455
switch x := f.(type) {
5556
case *query.StringCondition:
56-
if fieldInfo.ValueType != QueryValidate_STRING {
57+
if fieldInfo.ValueType != QueryValidate_STRING && fieldInfo.ValueType != QueryValidate_BOOL {
5758
return fmt.Errorf("Got invalid literal type for %s, expect %s", fieldTag, fieldInfo.ValueType)
5859
}
60+
61+
if fieldInfo.ValueType == QueryValidate_BOOL {
62+
if _, err := strconv.ParseBool(x.Value); err != nil {
63+
return fmt.Errorf("Got invalid literal for field %q of type %s, expect 'true' or 'false'", fieldTag, fieldInfo.ValueType)
64+
}
65+
}
66+
5967
sc := &query.Filtering_StringCondition{x}
6068
tp = query.StringCondition_Type_name[int32(sc.StringCondition.Type)]
6169
case *query.NumberCondition:
@@ -65,9 +73,18 @@ func ValidateFiltering(f *query.Filtering, messageInfo map[string]FilteringOptio
6573
nc := &query.Filtering_NumberCondition{x}
6674
tp = query.NumberCondition_Type_name[int32(nc.NumberCondition.Type)]
6775
case *query.StringArrayCondition:
68-
if fieldInfo.ValueType != QueryValidate_STRING {
76+
if fieldInfo.ValueType != QueryValidate_STRING && fieldInfo.ValueType != QueryValidate_BOOL {
6977
return fmt.Errorf("Got invalid literal type for %s, expect %s", fieldTag, fieldInfo.ValueType)
7078
}
79+
80+
if fieldInfo.ValueType == QueryValidate_BOOL {
81+
for i, xv := range x.Values {
82+
if _, err := strconv.ParseBool(xv); err != nil {
83+
return fmt.Errorf("Got invalid literal for field %q of type %s at position %d, expect 'true' or 'false'", fieldTag, fieldInfo.ValueType, i)
84+
}
85+
}
86+
}
87+
7188
nc := &query.Filtering_StringArrayCondition{x}
7289
tp = query.StringArrayCondition_Type_name[int32(nc.StringArrayCondition.Type)]
7390
case *query.NumberArrayCondition:

0 commit comments

Comments
 (0)