Skip to content

Commit 026621f

Browse files
authored
Add eval.TooManyArgError() (#3512)
* Add a test case for Files * Support functions nested twice by eval.caller() * Add eval.TooManyArgError()
1 parent b1d8205 commit 026621f

13 files changed

+30
-17
lines changed

dsl/attribute.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func Example(args ...any) {
301301
return
302302
}
303303
if len(args) > 2 {
304-
eval.ReportError("too many arguments")
304+
eval.TooManyArgError()
305305
return
306306
}
307307
var (
@@ -400,7 +400,7 @@ func parseAttributeArgs(baseAttr *expr.AttributeExpr, args ...any) (expr.DataTyp
400400
parseDescription("string", 1)
401401
parseDSL(2, success, func() { eval.InvalidArgError("func()", args[2]) })
402402
default:
403-
eval.ReportError("too many arguments in call to Attribute")
403+
eval.TooManyArgError()
404404
}
405405

406406
return dataType, description, fn

dsl/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func Param(name string, args ...any) {
743743
// })
744744
func MapParams(args ...any) {
745745
if len(args) > 1 {
746-
eval.ReportError("too many arguments")
746+
eval.TooManyArgError()
747747
}
748748
e, ok := eval.Current().(*expr.HTTPEndpointExpr)
749749
if !ok {

dsl/http_file_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
// })
5050
func Files(path, filename string, fns ...func()) {
5151
if len(fns) > 1 {
52-
eval.ReportError("too many arguments given to Files")
52+
eval.TooManyArgError()
5353
return
5454
}
5555
// Make sure request path starts with a "/" so codegen can rely on it.

dsl/payload.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import (
7070
// })
7171
func Payload(val any, args ...any) {
7272
if len(args) > 2 {
73-
eval.ReportError("too many arguments")
73+
eval.TooManyArgError()
7474
}
7575
e, ok := eval.Current().(*expr.MethodExpr)
7676
if !ok {
@@ -124,7 +124,7 @@ func Payload(val any, args ...any) {
124124
// })
125125
func StreamingPayload(val any, args ...any) {
126126
if len(args) > 2 {
127-
eval.ReportError("too many arguments")
127+
eval.TooManyArgError()
128128
}
129129
e, ok := eval.Current().(*expr.MethodExpr)
130130
if !ok {

dsl/response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) {
234234
case int:
235235
code = t
236236
if len(args) > 1 {
237-
eval.ReportError("too many arguments given to Response (%d)", len(args)+1)
237+
eval.TooManyArgError()
238238
return
239239
}
240240
if len(args) == 1 {

dsl/result.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import (
7272
// })
7373
func Result(val any, args ...any) {
7474
if len(args) > 2 {
75-
eval.ReportError("too many arguments")
75+
eval.TooManyArgError()
7676
return
7777
}
7878
e, ok := eval.Current().(*expr.MethodExpr)
@@ -130,7 +130,7 @@ func Result(val any, args ...any) {
130130
// })
131131
func StreamingResult(val any, args ...any) {
132132
if len(args) > 2 {
133-
eval.ReportError("too many arguments")
133+
eval.TooManyArgError()
134134
return
135135
}
136136
e, ok := eval.Current().(*expr.MethodExpr)

dsl/result_type.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func ResultType(identifier string, args ...any) *expr.ResultTypeExpr {
9898
eval.InvalidArgError("function", args[1])
9999
}
100100
if len(args) > 2 {
101-
eval.ReportError("too many arguments")
101+
eval.TooManyArgError()
102102
}
103103
}
104104
}

dsl/security.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,13 @@ func Scope(name string, desc ...string) {
524524
switch current := eval.Current().(type) {
525525
case *expr.SecurityExpr:
526526
if len(desc) >= 1 {
527-
eval.ReportError("too many arguments")
527+
eval.TooManyArgError()
528528
return
529529
}
530530
current.Scopes = append(current.Scopes, name)
531531
case *expr.SchemeExpr:
532532
if len(desc) > 1 {
533-
eval.ReportError("too many arguments")
533+
eval.TooManyArgError()
534534
return
535535
}
536536
d := "no description"

dsl/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import (
5858
// })
5959
func Server(name string, fn ...func()) *expr.ServerExpr {
6060
if len(fn) > 1 {
61-
eval.ReportError("too many arguments given to Server")
61+
eval.TooManyArgError()
6262
}
6363
api, ok := eval.Current().(*expr.APIExpr)
6464
if !ok {

dsl/user_type.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var (
5858
// })
5959
func Type(name string, args ...any) expr.UserType {
6060
if len(args) > 2 {
61-
eval.ReportError("too many arguments")
61+
eval.TooManyArgError()
6262
return nil
6363
}
6464
if t := expr.Root.UserType(name); t != nil {
@@ -149,7 +149,7 @@ func ArrayOf(v any, fn ...func()) *expr.Array {
149149
return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}}
150150
}
151151
if len(fn) > 1 {
152-
eval.ReportError("ArrayOf: too many arguments")
152+
eval.TooManyArgError()
153153
return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}}
154154
}
155155
at := expr.AttributeExpr{Type: t}
@@ -203,7 +203,7 @@ func MapOf(k, v any, fn ...func()) *expr.Map {
203203
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
204204
}
205205
if len(fn) > 1 {
206-
eval.ReportError("MapOf: too many arguments")
206+
eval.TooManyArgError()
207207
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
208208
}
209209
kat := expr.AttributeExpr{Type: tk}

eval/eval.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ func InvalidArgError(expected string, actual any) {
125125
ReportError("cannot use %#v (type %s) as type %s", actual, reflect.TypeOf(actual), expected)
126126
}
127127

128+
// TooManyArgError records a too many arguments error. It is used by DSL
129+
// functions that take dynamic arguments.
130+
func TooManyArgError() {
131+
ReportError("too many arguments given to %s", caller())
132+
}
133+
128134
// ValidationErrors records the errors encountered when running Validate.
129135
type ValidationErrors struct {
130136
Errors []error
@@ -234,7 +240,7 @@ func finalizeSet(set ExpressionSet) {
234240

235241
// caller returns the name of calling function.
236242
func caller() string {
237-
for skip := 2; skip <= 3; skip++ {
243+
for skip := 2; skip <= 4; skip++ {
238244
pc, _, _, ok := runtime.Caller(skip)
239245
if !ok {
240246
break

expr/http_file_server_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestFilesDSL(t *testing.T) {
1616
}{
1717
{Name: "valid", DSL: testdata.FilesValidDSL},
1818
{Name: "incompatible", DSL: testdata.FilesIncompatibleDSL, Error: "invalid use of Files in API files-incompatile"},
19+
{Name: "too many arg error", DSL: testdata.FilesTooManyArgErrorDSL, Error: "too many arguments given to Files in API files-too-many-arg-error"},
1920
}
2021
for _, c := range cases {
2122
t.Run(c.Name, func(t *testing.T) {

expr/testdata/http_file_server.go

+6
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ var FilesIncompatibleDSL = func() {
1515
Files("path", "filename")
1616
})
1717
}
18+
19+
var FilesTooManyArgErrorDSL = func() {
20+
API("files-too-many-arg-error", func() {
21+
Files("path", "filename", func() {}, func() {})
22+
})
23+
}

0 commit comments

Comments
 (0)