diff --git a/dsl/attribute.go b/dsl/attribute.go index 2f904b59d8..a6bc780fc4 100644 --- a/dsl/attribute.go +++ b/dsl/attribute.go @@ -301,7 +301,7 @@ func Example(args ...any) { return } if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return } var ( @@ -400,7 +400,7 @@ func parseAttributeArgs(baseAttr *expr.AttributeExpr, args ...any) (expr.DataTyp parseDescription("string", 1) parseDSL(2, success, func() { eval.InvalidArgError("func()", args[2]) }) default: - eval.ReportError("too many arguments in call to Attribute") + eval.TooManyArgError() } return dataType, description, fn diff --git a/dsl/http.go b/dsl/http.go index e2b53c91a6..2266e4b4e2 100644 --- a/dsl/http.go +++ b/dsl/http.go @@ -743,7 +743,7 @@ func Param(name string, args ...any) { // }) func MapParams(args ...any) { if len(args) > 1 { - eval.ReportError("too many arguments") + eval.TooManyArgError() } e, ok := eval.Current().(*expr.HTTPEndpointExpr) if !ok { diff --git a/dsl/http_file_server.go b/dsl/http_file_server.go index e360104765..611bbf0057 100644 --- a/dsl/http_file_server.go +++ b/dsl/http_file_server.go @@ -49,7 +49,7 @@ import ( // }) func Files(path, filename string, fns ...func()) { if len(fns) > 1 { - eval.ReportError("too many arguments given to Files") + eval.TooManyArgError() return } // Make sure request path starts with a "/" so codegen can rely on it. diff --git a/dsl/payload.go b/dsl/payload.go index a4adf2682c..dded16e9d4 100644 --- a/dsl/payload.go +++ b/dsl/payload.go @@ -70,7 +70,7 @@ import ( // }) func Payload(val any, args ...any) { if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() } e, ok := eval.Current().(*expr.MethodExpr) if !ok { @@ -124,7 +124,7 @@ func Payload(val any, args ...any) { // }) func StreamingPayload(val any, args ...any) { if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() } e, ok := eval.Current().(*expr.MethodExpr) if !ok { diff --git a/dsl/response.go b/dsl/response.go index fcd85cd3d6..fc00f266ca 100644 --- a/dsl/response.go +++ b/dsl/response.go @@ -234,7 +234,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) { case int: code = t if len(args) > 1 { - eval.ReportError("too many arguments given to Response (%d)", len(args)+1) + eval.TooManyArgError() return } if len(args) == 1 { diff --git a/dsl/result.go b/dsl/result.go index 3c987cbf3d..845e40f758 100644 --- a/dsl/result.go +++ b/dsl/result.go @@ -72,7 +72,7 @@ import ( // }) func Result(val any, args ...any) { if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return } e, ok := eval.Current().(*expr.MethodExpr) @@ -130,7 +130,7 @@ func Result(val any, args ...any) { // }) func StreamingResult(val any, args ...any) { if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return } e, ok := eval.Current().(*expr.MethodExpr) diff --git a/dsl/result_type.go b/dsl/result_type.go index 5fab7eabab..de883d2864 100644 --- a/dsl/result_type.go +++ b/dsl/result_type.go @@ -98,7 +98,7 @@ func ResultType(identifier string, args ...any) *expr.ResultTypeExpr { eval.InvalidArgError("function", args[1]) } if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() } } } diff --git a/dsl/security.go b/dsl/security.go index 0a31738e79..224f2f31e4 100644 --- a/dsl/security.go +++ b/dsl/security.go @@ -524,13 +524,13 @@ func Scope(name string, desc ...string) { switch current := eval.Current().(type) { case *expr.SecurityExpr: if len(desc) >= 1 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return } current.Scopes = append(current.Scopes, name) case *expr.SchemeExpr: if len(desc) > 1 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return } d := "no description" diff --git a/dsl/server.go b/dsl/server.go index d529717874..f230c71af0 100644 --- a/dsl/server.go +++ b/dsl/server.go @@ -58,7 +58,7 @@ import ( // }) func Server(name string, fn ...func()) *expr.ServerExpr { if len(fn) > 1 { - eval.ReportError("too many arguments given to Server") + eval.TooManyArgError() } api, ok := eval.Current().(*expr.APIExpr) if !ok { diff --git a/dsl/user_type.go b/dsl/user_type.go index 2aa733c4cd..7659ea7e4b 100644 --- a/dsl/user_type.go +++ b/dsl/user_type.go @@ -58,7 +58,7 @@ var ( // }) func Type(name string, args ...any) expr.UserType { if len(args) > 2 { - eval.ReportError("too many arguments") + eval.TooManyArgError() return nil } if t := expr.Root.UserType(name); t != nil { @@ -149,7 +149,7 @@ func ArrayOf(v any, fn ...func()) *expr.Array { return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}} } if len(fn) > 1 { - eval.ReportError("ArrayOf: too many arguments") + eval.TooManyArgError() return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}} } at := expr.AttributeExpr{Type: t} @@ -203,7 +203,7 @@ func MapOf(k, v any, fn ...func()) *expr.Map { return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}} } if len(fn) > 1 { - eval.ReportError("MapOf: too many arguments") + eval.TooManyArgError() return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}} } kat := expr.AttributeExpr{Type: tk} diff --git a/eval/eval.go b/eval/eval.go index 34cb8f03cd..a3e622baca 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -125,6 +125,12 @@ func InvalidArgError(expected string, actual any) { ReportError("cannot use %#v (type %s) as type %s", actual, reflect.TypeOf(actual), expected) } +// TooManyArgError records a too many arguments error. It is used by DSL +// functions that take dynamic arguments. +func TooManyArgError() { + ReportError("too many arguments given to %s", caller()) +} + // ValidationErrors records the errors encountered when running Validate. type ValidationErrors struct { Errors []error @@ -234,7 +240,7 @@ func finalizeSet(set ExpressionSet) { // caller returns the name of calling function. func caller() string { - for skip := 2; skip <= 3; skip++ { + for skip := 2; skip <= 4; skip++ { pc, _, _, ok := runtime.Caller(skip) if !ok { break diff --git a/expr/http_file_server_test.go b/expr/http_file_server_test.go index fdee36c0de..15e289df34 100644 --- a/expr/http_file_server_test.go +++ b/expr/http_file_server_test.go @@ -16,6 +16,7 @@ func TestFilesDSL(t *testing.T) { }{ {Name: "valid", DSL: testdata.FilesValidDSL}, {Name: "incompatible", DSL: testdata.FilesIncompatibleDSL, Error: "invalid use of Files in API files-incompatile"}, + {Name: "too many arg error", DSL: testdata.FilesTooManyArgErrorDSL, Error: "too many arguments given to Files in API files-too-many-arg-error"}, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { diff --git a/expr/testdata/http_file_server.go b/expr/testdata/http_file_server.go index 1e3950d5c1..f822d43dae 100644 --- a/expr/testdata/http_file_server.go +++ b/expr/testdata/http_file_server.go @@ -15,3 +15,9 @@ var FilesIncompatibleDSL = func() { Files("path", "filename") }) } + +var FilesTooManyArgErrorDSL = func() { + API("files-too-many-arg-error", func() { + Files("path", "filename", func() {}, func() {}) + }) +}