Skip to content

Commit 80b0eee

Browse files
howardjohnmvdan
authored andcommitted
encoding/protobuf: support field_behavior "required" option
See https://google.aip.dev/203. Currently, there is support for the 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 existing field behavior mechanism can be used, which is the standard way to declare a required field in proto, like: RecognitionAudio audio = 2 [(google.api.field_behavior) = REQUIRED]; Closes #2622 as merged as of commit 6974630. Signed-off-by: John Howard <[email protected]> Change-Id: Ia8079a8b148c9abe9b6fe26d090c86cb4489359a Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172314 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent d97b0c3 commit 80b0eee

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)