Repository: goadesign/goa
Pull request #3526 (2024-06-04 15:12:00)

Use eval.TooManyArgError() more
Related to
* https://github.com/goadesign/goa/pull/3512
* https://github.com/goadesign/goa/pull/3520

raphael: This is great, thank you!

Patch:
diff --git a/dsl/attribute.go b/dsl/attribute.go
index a6bc780f..6ee52ca6 100644
--- a/dsl/attribute.go
+++ b/dsl/attribute.go
@@ -222,7 +222,8 @@ func Field(tag any, name string, args ...any) {
 //	})
 func OneOf(name string, args ...any) {
 	if len(args) > 2 {
-		eval.ReportError("OneOf: wrong number of arguments")
+		eval.TooManyArgError()
+		return
 	}
 	fn, ok := args[len(args)-1].(func())
 	if !ok {
diff --git a/dsl/http.go b/dsl/http.go
index 0ddd4403..5c4f441a 100644
--- a/dsl/http.go
+++ b/dsl/http.go
@@ -1,7 +1,6 @@
 package dsl
 
 import (
-	"fmt"
 	"strconv"
 	"strings"
 
@@ -146,7 +145,7 @@ const (
 //	})
 func HTTP(fns ...func()) {
 	if len(fns) > 1 {
-		eval.InvalidArgError("zero or one function", fmt.Sprintf("%d functions", len(fns)))
+		eval.TooManyArgError()
 		return
 	}
 	fn := func() {}
diff --git a/dsl/response.go b/dsl/response.go
index fc00f266..61527055 100644
--- a/dsl/response.go
+++ b/dsl/response.go
@@ -247,7 +247,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) {
 		}
 	case func():
 		if len(args) > 0 {
-			eval.InvalidArgError("int (HTTP status code)", val)
+			eval.TooManyArgError()
 			return
 		}
 		fn = t
diff --git a/dsl/user_type.go b/dsl/user_type.go
index 7659ea7e..8f18474a 100644
--- a/dsl/user_type.go
+++ b/dsl/user_type.go
@@ -94,7 +94,7 @@ func Type(name string, args ...any) expr.UserType {
 		base = &expr.Object{}
 		fn = a
 		if len(args) == 2 {
-			eval.ReportError("only one argument allowed when it is a function")
+			eval.TooManyArgError()
 			return nil
 		}
 	default:


Test patch:
diff --git a/eval/eval_test.go b/eval/eval_test.go
index 7e4fee00..c316f52a 100644
--- a/eval/eval_test.go
+++ b/eval/eval_test.go
@@ -23,14 +23,17 @@ func TestTooManyArgError(t *testing.T) {
 		"Example":          func() { Example(1, 2, 3) },
 		"Field":            func() { Type("name", func() { Field("tag", "name", 1, 2, 3, 4) }) },
 		"Files":            func() { Files("path", "filename", func() {}, func() {}) },
+		"HTTP":             func() { API("name", func() { HTTP(func() {}, func() {}) }) },
 		"Header":           func() { API("name", func() { HTTP(func() { Header("name", 1, 2, 3, 4) }) }) },
 		"MapOf":            func() { MapOf(String, String, func() {}, func() {}) },
 		"MapParams":        func() { MapParams(1, 2) },
+		"OneOf":            func() { OneOf("name", 1, 2, 3) },
 		"Param":            func() { API("name", func() { HTTP(func() { Param("name", 1, 2, 3, 4) }) }) },
 		"Password":         func() { Type("name", func() { Password("name", 1, 2, 3) }) },
 		"PasswordField":    func() { Type("name", func() { PasswordField("tag", "name", 1, 2, 3) }) },
 		"Payload":          func() { Payload(String, 1, 2, 3) },
-		"Response":         func() { API("name", func() { HTTP(func() { Response(StatusOK, "name", 1, 2) }) }) },
+		"Response (int)":   func() { API("name", func() { HTTP(func() { Response(StatusOK, "name", 1, 2) }) }) },
+		"Response (func)":  func() { API("name", func() { HTTP(func() { Response("name", func() {}, func() {}) }) }) },
 		"Result":           func() { Result(String, 1, 2, 3) },
 		"ResultType":       func() { ResultType("identifier", "name", func() {}, func() {}) },
 		"Scope":            func() { BasicAuthSecurity("name", func() { Scope("name", "1", "2") }) },
@@ -40,6 +43,7 @@ func TestTooManyArgError(t *testing.T) {
 		"Token":            func() { Type("name", func() { Token("name", 1, 2, 3) }) },
 		"TokenField":       func() { Type("name", func() { TokenField("tag", "name", 1, 2, 3) }) },
 		"Type":             func() { Type("name", 1, 2, 3) },
+		"Type (func)":      func() { Type("name", func() {}, func() {}) },
 		"Username":         func() { Type("name", func() { Username("name", 1, 2, 3) }) },
 		"UsernameField":    func() { Type("name", func() { UsernameField("tag", "name", 1, 2, 3) }) },
 		"Variable":         func() { API("a", func() { Server("s", func() { Host("h", func() { Variable("v", 1, 2, 3, 4) }) }) }) },
@@ -48,7 +52,7 @@ func TestTooManyArgError(t *testing.T) {
 		t.Run(name, func(t *testing.T) {
 			err := expr.RunInvalidDSL(t, dsl)
 			assert.Len(t, strings.Split(err.Error(), "\n"), 1)
-			assert.Contains(t, err.Error(), "too many arguments given to "+name)
+			assert.Contains(t, err.Error(), "too many arguments given to "+strings.Split(name, " ")[0])
 		})
 	}
 }