Skip to content

Commit 6974630

Browse files
committed
protobuf: enable protobuf standard required mechanism
See https://google.aip.dev/203. Currently, there is support for an option `(cue.opt).required`. This is fine, but requires importing the `cue.proto`, and exposes cue to the public facing API. With this approach, the built-in field behavior mechanism can be used, which is the standard way to declare a required field in proto. Signed-off-by: John Howard <[email protected]>
1 parent 91d9766 commit 6974630

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

encoding/protobuf/parse.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,10 @@ func (p *optionParser) parse(options []*proto.Option) {
796796
if !p.required {
797797
constraint.Optional = token.NoSpace.Pos()
798798
}
799-
799+
case "(google.api.field_behavior)":
800+
if o.Constant.Source == "REQUIRED" {
801+
p.required = true
802+
}
800803
default:
801804
// TODO: dropping comments. Maybe add dummy tag?
802805

encoding/protobuf/testdata/gateway.proto.out.cue

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ package v1alpha3
205205

206206
#Gateway: {
207207
// REQUIRED: A list of server specifications.
208-
servers?: [...#Server] @protobuf(1,Server)
208+
servers: [...#Server] @protobuf(1,Server)
209209

210210
// REQUIRED: One or more labels that indicate a specific set of pods/VMs
211211
// on which this gateway configuration should be applied. The scope of

encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ option go_package = "istio.io/api/networking/v1alpha3"; // INLINE
213213

214214
message Gateway {
215215
// REQUIRED: A list of server specifications.
216-
repeated Server servers = 1;
216+
repeated Server servers = 1 [(google.api.field_behavior) = REQUIRED];
217217

218218
// REQUIRED: One or more labels that indicate a specific set of pods/VMs
219219
// on which this gateway configuration should be applied. The scope of

encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway_proto_gen.cue

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ package v1alpha3
205205

206206
#Gateway: {
207207
// REQUIRED: A list of server specifications.
208-
servers?: [...#Server] @protobuf(1,Server)
208+
servers: [...#Server] @protobuf(1,Server)
209209

210210
// REQUIRED: One or more labels that indicate a specific set of pods/VMs
211211
// on which this gateway configuration should be applied. The scope of

0 commit comments

Comments
 (0)