Skip to content

Commit eae20e0

Browse files
committed
encoding/protobuf: don't mark a single enum value as default
Issue #5 Change-Id: Icb6e8b5c6c490ea7123e554a9fb86a2b8d8ba624 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2373 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent ffe4ced commit eae20e0

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

encoding/protobuf/parse.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,13 @@ func (p *protoConverter) enum(x *proto.Enum) {
582582
}
583583
p.file.Decls = append(p.file.Decls, enum, d)
584584

585+
numEnums := 0
586+
for _, v := range x.Elements {
587+
if _, ok := v.(*proto.EnumField); ok {
588+
numEnums++
589+
}
590+
}
591+
585592
// The line comments for an enum field need to attach after the '|', which
586593
// is only known at the next iteration.
587594
var lastComment *proto.Comment
@@ -601,7 +608,10 @@ func (p *protoConverter) enum(x *proto.Enum) {
601608
var e ast.Expr = value
602609
// Make the first value the default value.
603610
if i == 0 {
604-
e = &ast.UnaryExpr{OpPos: newline, Op: token.MUL, X: value}
611+
e = value
612+
if numEnums > 1 {
613+
e = &ast.UnaryExpr{OpPos: newline, Op: token.MUL, X: value}
614+
}
605615
} else {
606616
value.ValuePos = newline
607617
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,13 @@ NetworkFailPolicy: {
4242
maxRetryWait?: time.Duration @protobuf(4,type=google.protobuf.Duration,name=max_retry_wait)
4343
}
4444

45-
// Describes the policy.
45+
// Example of single-value enum.
4646
NetworkFailPolicy_FailPolicy:
4747
// If network connection fails, request is allowed and delivered to the
4848
// service.
49-
*"FAIL_OPEN" |
49+
"FAIL_OPEN"
5050

51-
// If network connection fails, request is rejected.
52-
"FAIL_CLOSE"
53-
54-
NetworkFailPolicy_FailPolicy_value: {
55-
FAIL_OPEN: 0
56-
FAIL_CLOSE: 1
57-
}
51+
NetworkFailPolicy_FailPolicy_value FAIL_OPEN: 0
5852

5953
// Defines the per-service client configuration.
6054
ServiceConfig: {

encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config.proto

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ option (gogoproto.stable_marshaler_all) = true;
3737

3838
// Specifies the behavior when the client is unable to connect to Mixer.
3939
message NetworkFailPolicy {
40-
// Describes the policy.
40+
41+
// Example of single-value enum.
4142
enum FailPolicy {
4243
// If network connection fails, request is allowed and delivered to the
4344
// service.
4445
FAIL_OPEN = 0;
45-
46-
// If network connection fails, request is rejected.
47-
FAIL_CLOSE = 1;
4846
}
4947

5048
// Specifies the behavior when the client is unable to connect to Mixer.

encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,13 @@ NetworkFailPolicy: {
4141
maxRetryWait?: time.Duration @protobuf(4,type=google.protobuf.Duration,name=max_retry_wait)
4242
}
4343

44-
// Describes the policy.
44+
// Example of single-value enum.
4545
NetworkFailPolicy_FailPolicy:
4646
// If network connection fails, request is allowed and delivered to the
4747
// service.
48-
*"FAIL_OPEN" |
48+
"FAIL_OPEN"
4949

50-
// If network connection fails, request is rejected.
51-
"FAIL_CLOSE"
52-
53-
NetworkFailPolicy_FailPolicy_value: {
54-
"FAIL_OPEN": 0
55-
"FAIL_CLOSE": 1
56-
}
50+
NetworkFailPolicy_FailPolicy_value "FAIL_OPEN": 0
5751

5852
// Defines the per-service client configuration.
5953
ServiceConfig: {

0 commit comments

Comments
 (0)