Skip to content

Commit

Permalink
encoding/protobuf: support field_behavior "required" option
Browse files Browse the repository at this point in the history
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 <howardjohn@google.com>
Change-Id: Ia8079a8b148c9abe9b6fe26d090c86cb4489359a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172314
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
  • Loading branch information
howardjohn authored and mvdan committed Nov 20, 2023
1 parent d97b0c3 commit 80b0eee
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion encoding/protobuf/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,10 @@ func (p *optionParser) parse(options []*proto.Option) {
if !p.required {
constraint.Optional = token.NoSpace.Pos()
}

case "(google.api.field_behavior)":
if o.Constant.Source == "REQUIRED" {
p.required = true
}
default:
// TODO: dropping comments. Maybe add dummy tag?

Expand Down
2 changes: 1 addition & 1 deletion encoding/protobuf/testdata/gateway.proto.out.cue
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ package v1alpha3

#Gateway: {
// REQUIRED: A list of server specifications.
servers?: [...#Server] @protobuf(1,Server)
servers: [...#Server] @protobuf(1,Server)

// REQUIRED: One or more labels that indicate a specific set of pods/VMs
// on which this gateway configuration should be applied. The scope of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ option go_package = "istio.io/api/networking/v1alpha3"; // INLINE

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

// REQUIRED: One or more labels that indicate a specific set of pods/VMs
// on which this gateway configuration should be applied. The scope of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ package v1alpha3

#Gateway: {
// REQUIRED: A list of server specifications.
servers?: [...#Server] @protobuf(1,Server)
servers: [...#Server] @protobuf(1,Server)

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

0 comments on commit 80b0eee

Please sign in to comment.