encoding/protobuf: make map field as not required by default

in proto3 all fields are optional, thus making the map field optional as well.

Change-Id: I5397adf6bfc990d6af248acef5bc4dcdf89c5cdd
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2981
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/protobuf/examples/basic/basic.proto b/encoding/protobuf/examples/basic/basic.proto
index d66aab9..50696ad 100644
--- a/encoding/protobuf/examples/basic/basic.proto
+++ b/encoding/protobuf/examples/basic/basic.proto
@@ -13,4 +13,6 @@
 
     // A method must start with a capital letter.
     repeated string method = 2 [(cue.val) = '[...=~"^[A-Z]"]'];
+
+    map<string, string> exmaple_map = 3;
 }
\ No newline at end of file
diff --git a/encoding/protobuf/examples_test.go b/encoding/protobuf/examples_test.go
index 08b3e68..7ce0ffa 100644
--- a/encoding/protobuf/examples_test.go
+++ b/encoding/protobuf/examples_test.go
@@ -52,5 +52,8 @@
 	// 	// A method must start with a capital letter.
 	// 	method?: [...string] @protobuf(2)
 	// 	method?: [...=~"^[A-Z]"]
+	// 	exmapleMap?: {
+	// 		<_>: string
+	// 	} @protobuf(3,type=map<string,string>,exmaple_map)
 	// }
 }
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index 6432fce..dc1a230 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -545,6 +545,10 @@
 		o.parse(x.Options)
 		p.addTag(f, o.tags)
 
+		if !o.required {
+			f.Optional = token.NoSpace.Pos()
+		}
+
 	case *proto.Enum:
 		p.enum(x)
 
diff --git a/encoding/protobuf/testdata/attributes.proto.out.cue b/encoding/protobuf/testdata/attributes.proto.out.cue
index e75dd01..4ac3479 100644
--- a/encoding/protobuf/testdata/attributes.proto.out.cue
+++ b/encoding/protobuf/testdata/attributes.proto.out.cue
@@ -48,7 +48,7 @@
 //
 Attributes: {
 	// A map of attribute name to its value.
-	attributes: {
+	attributes?: {
 		<_>: Attributes_AttributeValue
 	} @protobuf(1,type=map<string,AttributeValue>)
 }
@@ -90,7 +90,7 @@
 // Defines a string map.
 Attributes_StringMap: {
 	// Holds a set of name/value pairs.
-	entries: {
+	entries?: {
 		<_>: string
 	} @protobuf(1,type=map<string,string>)
 }
@@ -107,42 +107,42 @@
 	words?: [...string] @protobuf(1)
 
 	// Holds attributes of type STRING, DNS_NAME, EMAIL_ADDRESS, URI
-	strings: {
+	strings?: {
 		<_>: int32
 	} @protobuf(2,type=map<sint32,sint32>)
 
 	// Holds attributes of type INT64
-	int64s: {
+	int64s?: {
 		<_>: int64
 	} @protobuf(3,type=map<sint32,int64>)
 
 	// Holds attributes of type DOUBLE
-	doubles: {
+	doubles?: {
 		<_>: float64
 	} @protobuf(4,type=map<sint32,double>)
 
 	// Holds attributes of type BOOL
-	bools: {
+	bools?: {
 		<_>: bool
 	} @protobuf(5,type=map<sint32,bool>)
 
 	// Holds attributes of type TIMESTAMP
-	time: {
+	time?: {
 		<_>: __time.Time
 	} @protobuf(6,type=map<sint32,google.protobuf.Timestamp>,"(gogoproto.nullable)=false","(gogoproto.stdtime)")
 
 	// Holds attributes of type DURATION
-	durations: {
+	durations?: {
 		<_>: __time.Duration
 	} @protobuf(7,type=map<sint32,google.protobuf.Duration>,"(gogoproto.nullable)=false","(gogoproto.stdduration)")
 
 	// Holds attributes of type BYTES
-	bytes: {
+	bytes?: {
 		<_>: __bytes
 	} @protobuf(8,type=map<sint32,bytes>)
 
 	// Holds attributes of type STRING_MAP
-	stringMaps: {
+	stringMaps?: {
 		<_>: StringMap
 	} @protobuf(9,type=map<sint32,StringMap>,string_maps,"(gogoproto.nullable)=false")
 }
@@ -153,7 +153,7 @@
 // indices (see the [Attributes][istio.mixer.v1.CompressedAttributes] message for an explanation)
 StringMap: {
 	// Holds a set of name/value pairs.
-	entries: {
+	entries?: {
 		<_>: int32
 	} @protobuf(1,type=map<sint32,sint32>)
 }
diff --git a/encoding/protobuf/testdata/client_config.proto.out.cue b/encoding/protobuf/testdata/client_config.proto.out.cue
index 527eead..50d61d5 100644
--- a/encoding/protobuf/testdata/client_config.proto.out.cue
+++ b/encoding/protobuf/testdata/client_config.proto.out.cue
@@ -144,7 +144,7 @@
 	// Map of control configuration indexed by destination.service. This
 	// is used to support per-service configuration for cases where a
 	// mixerclient serves multiple services.
-	serviceConfigs: {
+	serviceConfigs?: {
 		<_>: ServiceConfig
 	} @protobuf(2,type=map<string,ServiceConfig>,service_configs)
 
diff --git a/encoding/protobuf/testdata/gateway.proto.out.cue b/encoding/protobuf/testdata/gateway.proto.out.cue
index b8061e5..86bae1e 100644
--- a/encoding/protobuf/testdata/gateway.proto.out.cue
+++ b/encoding/protobuf/testdata/gateway.proto.out.cue
@@ -213,7 +213,7 @@
 	// label search is restricted to the configuration namespace in which the
 	// the resource is present. In other words, the Gateway resource must
 	// reside in the same namespace as the gateway workload instance.
-	selector: {
+	selector?: {
 		<_>: string
 	} @protobuf(2,type=map<string,string>)
 	selector? <name>: name
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
index 3211820..97fa441 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
@@ -48,7 +48,7 @@
 //
 Attributes: {
 	// A map of attribute name to its value.
-	attributes: {
+	attributes?: {
 		<_>: Attributes_AttributeValue
 	} @protobuf(1,type=map<string,AttributeValue>)
 }
@@ -90,7 +90,7 @@
 // Defines a string map.
 Attributes_StringMap: {
 	// Holds a set of name/value pairs.
-	entries: {
+	entries?: {
 		<_>: string
 	} @protobuf(1,type=map<string,string>)
 }
@@ -107,42 +107,42 @@
 	words?: [...string] @protobuf(1)
 
 	// Holds attributes of type STRING, DNS_NAME, EMAIL_ADDRESS, URI
-	strings: {
+	strings?: {
 		<_>: int32
 	} @protobuf(2,type=map<sint32,sint32>)
 
 	// Holds attributes of type INT64
-	int64s: {
+	int64s?: {
 		<_>: int64
 	} @protobuf(3,type=map<sint32,int64>)
 
 	// Holds attributes of type DOUBLE
-	doubles: {
+	doubles?: {
 		<_>: float64
 	} @protobuf(4,type=map<sint32,double>)
 
 	// Holds attributes of type BOOL
-	bools: {
+	bools?: {
 		<_>: bool
 	} @protobuf(5,type=map<sint32,bool>)
 
 	// Holds attributes of type TIMESTAMP
-	time: {
+	time?: {
 		<_>: __time.Time
 	} @protobuf(6,type=map<sint32,google.protobuf.Timestamp>,"(gogoproto.nullable)=false","(gogoproto.stdtime)")
 
 	// Holds attributes of type DURATION
-	durations: {
+	durations?: {
 		<_>: __time.Duration
 	} @protobuf(7,type=map<sint32,google.protobuf.Duration>,"(gogoproto.nullable)=false","(gogoproto.stdduration)")
 
 	// Holds attributes of type BYTES
-	bytes: {
+	bytes?: {
 		<_>: __bytes
 	} @protobuf(8,type=map<sint32,bytes>)
 
 	// Holds attributes of type STRING_MAP
-	stringMaps: {
+	stringMaps?: {
 		<_>: StringMap
 	} @protobuf(9,type=map<sint32,StringMap>,string_maps,"(gogoproto.nullable)=false")
 }
@@ -153,7 +153,7 @@
 // indices (see the [Attributes][istio.mixer.v1.CompressedAttributes] message for an explanation)
 StringMap: {
 	// Holds a set of name/value pairs.
-	entries: {
+	entries?: {
 		<_>: int32
 	} @protobuf(1,type=map<sint32,sint32>)
 }
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 03a0025..bf28bfb 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
@@ -143,7 +143,7 @@
 	// Map of control configuration indexed by destination.service. This
 	// is used to support per-service configuration for cases where a
 	// mixerclient serves multiple services.
-	serviceConfigs: {
+	serviceConfigs?: {
 		<_>: ServiceConfig
 	} @protobuf(2,type=map<string,ServiceConfig>,service_configs)
 
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/quota_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/quota_proto_gen.cue
index 394d5e3..d94fe8e 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/quota_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/quota_proto_gen.cue
@@ -97,7 +97,7 @@
 	//       exact: SOURCE_UID
 	//     request.http_method:
 	//       exact: POST
-	clause: {
+	clause?: {
 		<_>: StringMatch
 	} @protobuf(1,type=map<string,StringMatch>)
 }
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/service_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/service_proto_gen.cue
index 03e034e..1940810 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/service_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/config/client/service_proto_gen.cue
@@ -42,7 +42,7 @@
 	//
 	// *Note:* When used for a VirtualService destination, labels MUST be empty.
 	//
-	labels: {
+	labels?: {
 		<_>: string
 	} @protobuf(5,type=map<string,string>)
 }
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue
index ded5e39..1e34cff 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue
@@ -40,7 +40,7 @@
 	deduplicationId?: string @protobuf(3,name=deduplication_id)
 
 	// The individual quotas to allocate
-	quotas: {
+	quotas?: {
 		<_>: CheckRequest_QuotaParams
 	} @protobuf(4,type=map<string,QuotaParams>,"(gogoproto.nullable)=false")
 }
@@ -61,7 +61,7 @@
 	precondition?: CheckResponse_PreconditionResult @protobuf(2,type=PreconditionResult,"(gogoproto.nullable)=false")
 
 	// The resulting quota, one entry per requested quota.
-	quotas: {
+	quotas?: {
 		<_>: CheckResponse_QuotaResult
 	} @protobuf(3,type=map<string,QuotaResult>,"(gogoproto.nullable)=false")
 }
diff --git a/encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway_proto_gen.cue
index 024937e..1a2a293 100644
--- a/encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/networking/v1alpha3/gateway_proto_gen.cue
@@ -212,7 +212,7 @@
 	// label search is restricted to the configuration namespace in which the
 	// the resource is present. In other words, the Gateway resource must
 	// reside in the same namespace as the gateway workload instance.
-	selector: {
+	selector?: {
 		<_>: string
 	} @protobuf(2,type=map<string,string>)
 	selector?: {<name>: name}