Repository: goadesign/goa
Pull request #3066 (2022-05-14 23:45:41)

Remove unnecessary nil check
in generated CLI payload validation code


Patch:
diff --git a/codegen/cli/cli.go b/codegen/cli/cli.go
index bf69b9ec..9976f852 100644
--- a/codegen/cli/cli.go
+++ b/codegen/cli/cli.go
@@ -367,6 +367,25 @@ func FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultVa
 			}
 		}
 		if validate != "" {
+			nilCheck := "if " + argName + " != nil {"
+			if strings.HasPrefix(validate, nilCheck) {
+				// hackety hack... the validation code is generated for the client and needs to
+				// account for the fact that the field could be nil in this case. We are reusing
+				// that code to validate a CLI flag which can never be nil.  Lint tools complain
+				// about that so remove the if statements. Ideally we'd have a better way to do
+				// this but that requires a lot of changes and the added complexity might not be
+				// worth it.
+				var lines []string
+				ls := strings.Split(validate, "\n")
+				for i := 1; i < len(ls)-1; i++ {
+					if ls[i+1] == nilCheck {
+						i++ // skip both closing brace on previous line and check
+						continue
+					}
+					lines = append(lines, ls[i])
+				}
+				validate = strings.Join(lines, "\n")
+			}
 			code += "\n" + validate + "\n"
 			nilVal := "nil"
 			if expr.IsPrimitive(payload) {


Test patch:
diff --git a/grpc/codegen/testdata/client_cli_code.go b/grpc/codegen/testdata/client_cli_code.go
index 53319bdf..e971d715 100644
--- a/grpc/codegen/testdata/client_cli_code.go
+++ b/grpc/codegen/testdata/client_cli_code.go
@@ -30,15 +30,11 @@ func BuildMethodAPayload(payloadWithValidationMethodAMetadataInt string, payload
 			if err != nil {
 				return nil, fmt.Errorf("invalid value for metadataInt, must be INT")
 			}
-			if metadataInt != nil {
-				if *metadataInt < 0 {
-					err = goa.MergeErrors(err, goa.InvalidRangeError("metadataInt", *metadataInt, 0, true))
-				}
+			if *metadataInt < 0 {
+				err = goa.MergeErrors(err, goa.InvalidRangeError("metadataInt", *metadataInt, 0, true))
 			}
-			if metadataInt != nil {
-				if *metadataInt > 100 {
-					err = goa.MergeErrors(err, goa.InvalidRangeError("metadataInt", *metadataInt, 100, false))
-				}
+			if *metadataInt > 100 {
+				err = goa.MergeErrors(err, goa.InvalidRangeError("metadataInt", *metadataInt, 100, false))
 			}
 			if err != nil {
 				return nil, err
@@ -49,15 +45,11 @@ func BuildMethodAPayload(payloadWithValidationMethodAMetadataInt string, payload
 	{
 		if payloadWithValidationMethodAMetadataString != "" {
 			metadataString = &payloadWithValidationMethodAMetadataString
-			if metadataString != nil {
-				if utf8.RuneCountInString(*metadataString) < 5 {
-					err = goa.MergeErrors(err, goa.InvalidLengthError("metadataString", *metadataString, utf8.RuneCountInString(*metadataString), 5, true))
-				}
+			if utf8.RuneCountInString(*metadataString) < 5 {
+				err = goa.MergeErrors(err, goa.InvalidLengthError("metadataString", *metadataString, utf8.RuneCountInString(*metadataString), 5, true))
 			}
-			if metadataString != nil {
-				if utf8.RuneCountInString(*metadataString) > 10 {
-					err = goa.MergeErrors(err, goa.InvalidLengthError("metadataString", *metadataString, utf8.RuneCountInString(*metadataString), 10, false))
-				}
+			if utf8.RuneCountInString(*metadataString) > 10 {
+				err = goa.MergeErrors(err, goa.InvalidLengthError("metadataString", *metadataString, utf8.RuneCountInString(*metadataString), 10, false))
 			}
 			if err != nil {
 				return nil, err
diff --git a/http/codegen/testdata/parse_endpoint_functions.go b/http/codegen/testdata/parse_endpoint_functions.go
index 861f2144..727315b9 100644
--- a/http/codegen/testdata/parse_endpoint_functions.go
+++ b/http/codegen/testdata/parse_endpoint_functions.go
@@ -746,10 +746,8 @@ func BuildMethodParamValidatePayload(serviceParamValidateMethodParamValidateA st
 			if err != nil {
 				return nil, fmt.Errorf("invalid value for a, must be INT")
 			}
-			if a != nil {
-				if *a < 1 {
-					err = goa.MergeErrors(err, goa.InvalidRangeError("a", *a, 1, true))
-				}
+			if *a < 1 {
+				err = goa.MergeErrors(err, goa.InvalidRangeError("a", *a, 1, true))
 			}
 			if err != nil {
 				return nil, err