Repository: goadesign/goa
Pull request #3044 (2022-04-07 18:35:43)

Handle gRPC struct alias primitive type
When initializing fields with default values


Patch:
diff --git a/grpc/codegen/protobuf_transform.go b/grpc/codegen/protobuf_transform.go
index 2aea8b2e..53a59147 100644
--- a/grpc/codegen/protobuf_transform.go
+++ b/grpc/codegen/protobuf_transform.go
@@ -336,14 +336,16 @@ func transformObject(source, target *expr.AttributeExpr, sourceVar, targetVar st
 				// their default values if the corresponding source fields have zero
 				// values.
 				// We don't set default values for booleans since by default, protocol
-				// buffer sets boolean fields as false. Changing them to the default
-				// value is counter-intuitive.
+				// buffer sets boolean fields as false.
 				if !srcMatt.IsRequired(n) && srcc.Type != expr.Boolean {
 					if typeName, _ := codegen.GetMetaType(tgtc); typeName != "" {
 						code += fmt.Sprintf("var zero %s\n\t", typeName)
 						code += fmt.Sprintf("if %s == zero {\n\t", srcVar)
 					} else {
-						code += fmt.Sprintf("if %s {\n\t", checkZeroValue(srcc.Type, srcVar, false))
+						check := checkZeroValue(srcc.Type, srcVar, false)
+						if check != "" {
+							code += fmt.Sprintf("if %s {\n\t", check)
+						}
 					}
 					if ta.TargetCtx.IsPrimitivePointer(n, tgtMatt.AttributeExpr) && expr.IsPrimitive(tgtc.Type) {
 						code += fmt.Sprintf("var tmp %s = %#v\n\t%s = &tmp\n", codegen.GoNativeTypeName(tgtc.Type), tdef, tgtVar)
@@ -614,12 +616,16 @@ func convertType(src, tgt *expr.AttributeExpr, srcVar string, ta *transformAttrs
 
 // zeroValure returns the zero value for the given primitive type.
 func checkZeroValue(dt expr.DataType, target string, negate bool) string {
+	kind := dt.Kind()
+	if p := getPrimitive(&expr.AttributeExpr{Type: dt}); p != nil {
+		kind = p.Type.Kind()
+	}
 	eq := "=="
 	if negate {
 		eq = "!="
 	}
-	switch dt.Kind() {
 	// don't check for BooleanKind since by default boolean is set to false
+	switch kind {
 	case expr.IntKind, expr.Int32Kind, expr.Int64Kind,
 		expr.UIntKind, expr.UInt32Kind, expr.UInt64Kind,
 		expr.Float32Kind, expr.Float64Kind:


Test patch:
diff --git a/grpc/codegen/testdata/client_type_code.go b/grpc/codegen/testdata/client_type_code.go
index fbe6a01e..04aee07b 100644
--- a/grpc/codegen/testdata/client_type_code.go
+++ b/grpc/codegen/testdata/client_type_code.go
@@ -132,7 +132,7 @@ func NewMethodMessageUserTypeWithAliasResult(message *service_message_user_type_
 	result := &servicemessageusertypewithalias.PayloadAliasT{
 		IntAliasField: servicemessageusertypewithalias.IntAlias(message.IntAliasField),
 	}
-	if message.OptionalIntAliasField != nil {
+	if message.OptionalIntAliasField != 0 {
 		optionalIntAliasFieldptr := servicemessageusertypewithalias.IntAlias(message.OptionalIntAliasField)
 		result.OptionalIntAliasField = &optionalIntAliasFieldptr
 	}
diff --git a/grpc/codegen/testdata/server_type_code.go b/grpc/codegen/testdata/server_type_code.go
index 27f38449..da85f3fd 100644
--- a/grpc/codegen/testdata/server_type_code.go
+++ b/grpc/codegen/testdata/server_type_code.go
@@ -150,7 +150,7 @@ func NewMethodMessageUserTypeWithAliasPayload(message *service_message_user_type
 	v := &servicemessageusertypewithalias.PayloadAliasT{
 		IntAliasField: servicemessageusertypewithalias.IntAlias(message.IntAliasField),
 	}
-	if message.OptionalIntAliasField != nil {
+	if message.OptionalIntAliasField != 0 {
 		optionalIntAliasFieldptr := servicemessageusertypewithalias.IntAlias(message.OptionalIntAliasField)
 		v.OptionalIntAliasField = &optionalIntAliasFieldptr
 	}