Skip to content

Commit 448c833

Browse files
authored
Enable deeply nested message validation options (#17)
* Enable deeply nested message validation options * Use latest gentool as base for testing
1 parent 038d568 commit 448c833

File tree

8 files changed

+192
-108
lines changed

8 files changed

+192
-108
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.10.0 AS builder
1+
FROM golang:1.13.0-alpine AS builder
22

33
LABEL stage=server-intermediate
44

Makefile

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
GOPATH ?= $(HOME)/go
22
SRCPATH := $(patsubst %/,%,$(GOPATH))/src
33

4+
# configuration for the protobuf gentool
5+
PROJECT_ROOT := github.com/infobloxopen/protoc-gen-atlas-query-validate
6+
SRCROOT_ON_HOST := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
7+
SRCROOT_IN_CONTAINER := /go/src/$(PROJECT_ROOT)
8+
DOCKERPATH := /go/src
9+
DOCKER_RUNNER := docker run --rm
10+
DOCKER_RUNNER += -v $(SRCROOT_ON_HOST):$(SRCROOT_IN_CONTAINER)
11+
DOCKER_GENERATOR := infoblox/atlas-gentool:v19.1
12+
GENERATOR := $(DOCKER_RUNNER) $(DOCKER_GENERATOR)
13+
414
default: install
515

616
.PHONY: options
717
options:
8-
protoc -I. -I$(SRCPATH) -I./vendor \
9-
--gogo_out="Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor:$(SRCPATH)" \
18+
@$(GENERATOR) \
19+
--gogo_out="Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor:$(DOCKERPATH)" \
1020
options/query_validate.proto
1121

1222
.PHONY: install
1323
install:
1424
go install
1525

1626
.PHONY: example
17-
example: default
18-
protoc -I. -I${SRCPATH} -I./vendor -I./vendor/github.com/grpc-ecosystem/grpc-gateway --atlas-query-validate_out=. example/example.proto
27+
example: gentool
28+
$(DOCKER_RUNNER) infoblox/atlas-gentool:atlas-validate-query-dev \
29+
--atlas-query-validate_out=":$(DOCKERPATH)" example/example.proto
1930

2031
test: example
2132
go test ./...
22-
33+
2334
.PHONY: vendor
2435
vendor:
2536
dep ensure -vendor-only

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Purpose
44

5-
A [protobuf](https://developers.google.com/protocol-buffers/) compiler plugin
5+
A [protobuf](https://developers.google.com/protocol-buffers/) compiler plugin
66
designed to simplify validation of [Atlas](https://github.com/infobloxopen/atlas-app-toolkit)
77
[gRPC](https://grpc.io/) List [query](https://github.com/infobloxopen/atlas-app-toolkit/blob/master/query/collection_operators.proto) parameters
88
by generating .pb.atlas.query.validate.go files with validation rules and functions.
@@ -58,7 +58,7 @@ The following table shows what is allowed for each *value_type*:
5858

5959
| | STRING | NUMBER |
6060
|-------------------------------------|--------|--------|
61-
| **Filtering operators** | EQ, MATCH, GT, GE, LT, LE, IEQ, IN | EQ, GT, GE, LT, LE, IN |
61+
| **Filtering operators** | EQ, MATCH, GT, GE, LT, LE, IEQ, IN | EQ, GT, GE, LT, LE, IN |
6262
| **Filtering value type/condition type** | String, null/StringCondition, NullCondition, StringArray(only for IN)| Number, null/NumberCondition, NullCondition, NumberArray(only for IN)|
6363

6464
The next table shows how *value_type* is computed from a proto field type:
@@ -118,7 +118,7 @@ field-level options in order to support different validation rules for List meth
118118
bool on_vacation = 3 [(atlas.query.validate).sorting.disable = true];
119119
```
120120

121-
* In order to customize the list of allowed filtering operators pass either a set of `(atlas.query.validate).filtering.allow` or
121+
* In order to customize the list of allowed filtering operators pass either a set of `(atlas.query.validate).filtering.allow` or
122122
a set of `(atlas.query.validate).filtering.deny` options.
123123
- In case of using `(atlas.query.validate).filtering.allow` only specified filtering operators are allowed:
124124
```golang
@@ -133,9 +133,17 @@ a set of `(atlas.query.validate).filtering.deny` options.
133133
CustomType custom_type_string = 10 [(atlas.query.validate) = {value_type: STRING}];
134134
```
135135
* In order to enable filtering/sorting by nested fields set `(atlas.query.validate).enable_nested_fields` option to true
136-
on the field of a message type.
136+
on the field of a message type or as message option.
137+
Note that this overrides any field level settings.
137138

138139
```golang
140+
message User {
141+
option (atlas.query.message) = {
142+
enable_nested_fields: true;
143+
};
144+
...
145+
}
146+
139147
message User {
140148
Address home_address = 11 [(atlas.query.validate) = {enable_nested_fields: true}];
141149
Address work_address = 12;
@@ -147,6 +155,27 @@ message Address {
147155
}
148156
```
149157

158+
* You can also specify the maximum nesting depth using the `nested_field_depth_limit`
159+
option at the message level (the default is second level fields only).
160+
161+
```golang
162+
message User {
163+
option (atlas.query.message) = {
164+
enable_nested_fields: true;
165+
nested_field_depth_limit: 3;
166+
};
167+
...
168+
}
169+
```
170+
171+
* Nesting and nested field depth can also be set for the entire code generation
172+
process as parameters on the protoc command
173+
174+
```sh
175+
protoc ... \
176+
--atlas-query-validate_out="nested_field_depth_limit=3,enable_nested_fields=true:." \
177+
example/example.proto
178+
```
150179

151180
### Examples
152181

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

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

example/example.proto

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import "google/protobuf/wrappers.proto";
66
import "github.com/infobloxopen/protoc-gen-atlas-query-validate/options/query_validate.proto";
77
import "github.com/infobloxopen/atlas-app-toolkit/query/collection_operators.proto";
88

9+
option go_package = "github.com/infobloxopen/protoc-gen-atlas-query-validate/example;example";
10+
911
message User {
1012
option (atlas.query.message) = {
13+
// enable_nested_fields: true;
1114
validate: {name: "custom_search", value: {value_type_url: ".example.Address", nested_fields: ["country"]}};
1215
validate: {name: "custom_search_2", value: {value_type: STRING, filtering: {allow: MATCH, allow: EQ}}};
1316
validate: {name: "list_of_addresses", value: {value_type_url: ".example.Address", enable_nested_fields: true, field_selection: {disable: false}}};
@@ -22,7 +25,7 @@ message User {
2225
google.protobuf.StringValue last_name = 6;
2326
string id = 7 [(atlas.query.validate).filtering.deny = ALL];
2427
repeated string array = 8;
25-
CustomType custom_type = 9;
28+
CustomType custom_type = 9 [(atlas.query.validate) = {enable_nested_fields: true}];
2629
CustomType custom_type_string = 10 [(atlas.query.validate) = {value_type: STRING}];
2730
Address home_address = 11 [(atlas.query.validate) = {enable_nested_fields: true}];
2831
Address work_address = 12;
@@ -32,6 +35,8 @@ message User {
3235
}
3336

3437
message CustomType {
38+
CustomType recur = 1;
39+
string name = 2;
3540
}
3641

3742
message Address {
@@ -67,4 +72,3 @@ service TestService {
6772
rpc Read (ReadRequest) returns (ReadUserResponse) {
6873
}
6974
}
70-

options/query_validate.pb.go

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

0 commit comments

Comments
 (0)