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 <mpvl@golang.org>
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index 8593aa3..c918fd1 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -582,6 +582,13 @@
 	}
 	p.file.Decls = append(p.file.Decls, enum, d)
 
+	numEnums := 0
+	for _, v := range x.Elements {
+		if _, ok := v.(*proto.EnumField); ok {
+			numEnums++
+		}
+	}
+
 	// The line comments for an enum field need to attach after the '|', which
 	// is only known at the next iteration.
 	var lastComment *proto.Comment
@@ -601,7 +608,10 @@
 			var e ast.Expr = value
 			// Make the first value the default value.
 			if i == 0 {
-				e = &ast.UnaryExpr{OpPos: newline, Op: token.MUL, X: value}
+				e = value
+				if numEnums > 1 {
+					e = &ast.UnaryExpr{OpPos: newline, Op: token.MUL, X: value}
+				}
 			} else {
 				value.ValuePos = newline
 			}
diff --git a/encoding/protobuf/testdata/client_config.proto.out.cue b/encoding/protobuf/testdata/client_config.proto.out.cue
index efa1f40..66ec87a 100644
--- a/encoding/protobuf/testdata/client_config.proto.out.cue
+++ b/encoding/protobuf/testdata/client_config.proto.out.cue
@@ -42,19 +42,13 @@
 	maxRetryWait?: time.Duration @protobuf(4,type=google.protobuf.Duration,name=max_retry_wait)
 }
 
-//  Describes the policy.
+//  Example of single-value enum.
 NetworkFailPolicy_FailPolicy:
 	//  If network connection fails, request is allowed and delivered to the
 	//  service.
-	*"FAIL_OPEN" |
+	"FAIL_OPEN"
 
-	//  If network connection fails, request is rejected.
-	"FAIL_CLOSE"
-
-NetworkFailPolicy_FailPolicy_value: {
-	FAIL_OPEN:  0
-	FAIL_CLOSE: 1
-}
+NetworkFailPolicy_FailPolicy_value FAIL_OPEN: 0
 
 //  Defines the per-service client configuration.
 ServiceConfig: {
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config.proto b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config.proto
index a229699..c540745 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config.proto
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config.proto
@@ -37,14 +37,12 @@
 
 // Specifies the behavior when the client is unable to connect to Mixer.
 message NetworkFailPolicy {
-  // Describes the policy.
+
+  // Example of single-value enum.
   enum FailPolicy {
     // If network connection fails, request is allowed and delivered to the
     // service.
     FAIL_OPEN = 0;
-
-    // If network connection fails, request is rejected.
-    FAIL_CLOSE = 1;
   }
 
   // Specifies the behavior when the client is unable to connect to Mixer.
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue
index 2f4bf64..8471bc5 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/client_config_proto_gen.cue
@@ -41,19 +41,13 @@
 	maxRetryWait?: time.Duration @protobuf(4,type=google.protobuf.Duration,name=max_retry_wait)
 }
 
-//  Describes the policy.
+//  Example of single-value enum.
 NetworkFailPolicy_FailPolicy:
 	//  If network connection fails, request is allowed and delivered to the
 	//  service.
-	*"FAIL_OPEN" |
+	"FAIL_OPEN"
 
-	//  If network connection fails, request is rejected.
-	"FAIL_CLOSE"
-
-NetworkFailPolicy_FailPolicy_value: {
-	"FAIL_OPEN":  0
-	"FAIL_CLOSE": 1
-}
+NetworkFailPolicy_FailPolicy_value "FAIL_OPEN": 0
 
 //  Defines the per-service client configuration.
 ServiceConfig: {