Skip to content

Commit

Permalink
Add eval.TooManyArgError() (#3512)
Browse files Browse the repository at this point in the history
* Add a test case for Files

* Support functions nested twice by eval.caller()

* Add eval.TooManyArgError()
  • Loading branch information
tchssk committed Apr 22, 2024
1 parent b1d8205 commit 026621f
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions dsl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func Example(args ...any) {
return
}
if len(args) > 2 {
eval.ReportError("too many arguments")
eval.TooManyArgError()
return
}
var (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dsl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dsl/http_file_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions dsl/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dsl/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dsl/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dsl/result_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions dsl/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion dsl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions dsl/user_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
8 changes: 7 additions & 1 deletion eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions expr/http_file_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions expr/testdata/http_file_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {})
})
}

0 comments on commit 026621f

Please sign in to comment.