Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eval.TooFewArgError() #3557

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions dsl/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,27 @@ import (
// })
func Attribute(name string, args ...any) {
var parent *expr.AttributeExpr
{
switch def := eval.Current().(type) {
case *expr.AttributeExpr:
parent = def
case expr.CompositeExpr:
parent = def.Attribute()
default:
eval.IncompatibleDSL()
return
}
if parent == nil {
eval.ReportError("invalid syntax, attribute %#v has no parent", name)
switch def := eval.Current().(type) {
case *expr.AttributeExpr:
parent = def
case expr.CompositeExpr:
parent = def.Attribute()
default:
eval.IncompatibleDSL()
return
}
if parent == nil {
eval.ReportError("invalid syntax, attribute %#v has no parent", name)
return
}
if parent.Type == nil {
parent.Type = &expr.Object{}
}
if _, ok := parent.Type.(*expr.Object); !ok {
if _, ok := parent.Type.(*expr.Union); !ok {
eval.ReportError("can't define child attribute %#v on attribute of type %s %T", name, parent.Type.Name(), parent.Type)
return
}
if parent.Type == nil {
parent.Type = &expr.Object{}
}
if _, ok := parent.Type.(*expr.Object); !ok {
if _, ok := parent.Type.(*expr.Union); !ok {
eval.ReportError("can't define child attribute %#v on attribute of type %s %T", name, parent.Type.Name(), parent.Type)
return
}
}
}

var attr *expr.AttributeExpr
Expand Down Expand Up @@ -298,7 +296,7 @@ func Default(def any) {
// })
func Example(args ...any) {
if len(args) == 0 {
eval.ReportError("not enough arguments")
eval.TooFewArgError()
return
}
if len(args) > 2 {
Expand Down
6 changes: 3 additions & 3 deletions dsl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func Error(name string, args ...any) {
// }
func ErrorName(args ...any) {
if len(args) == 0 {
eval.IncompatibleDSL()
eval.TooFewArgError()
return
}
dsl, ok := args[len(args)-1].(func())
Expand All @@ -191,8 +191,8 @@ func ErrorName(args ...any) {
case string:
Attribute(actual, args[1:]...)
case int:
if len(args) == 1 {
eval.IncompatibleDSL()
if len(args) == 2 {
eval.TooFewArgError()
return
}
name, ok := args[1].(string)
Expand Down
2 changes: 1 addition & 1 deletion dsl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ func SkipResponseBodyEncodeDecode() {
// })
func Body(args ...any) {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Body(name), Body(type), Body(func()) or Body(type, func())")
eval.TooFewArgError()
return
}

Expand Down
4 changes: 2 additions & 2 deletions dsl/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func Code(code int) {

func grpcError(n string, p eval.Expression, args ...any) *expr.GRPCErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
eval.TooFewArgError()
return nil
}
var (
Expand Down Expand Up @@ -260,7 +260,7 @@ func parseResponseArgs(val any, args ...any) (code int, fn func()) {

func httpError(n string, p eval.Expression, args ...any) *expr.HTTPErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
eval.TooFewArgError()
return nil
}
var (
Expand Down
6 changes: 6 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func InvalidArgError(expected string, actual any) {
ReportError("cannot use %#v (type %s) as type %s", actual, reflect.TypeOf(actual), expected)
}

// TooFewArgError records a too few arguments error. It is used by DSL
// functions that take dynamic arguments.
func TooFewArgError() {
ReportError("too few arguments given to %s", caller())
}

// TooManyArgError records a too many arguments error. It is used by DSL
// functions that take dynamic arguments.
func TooManyArgError() {
Expand Down
18 changes: 18 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ func TestInvalidArgError(t *testing.T) {
}
}

func TestTooFewArgError(t *testing.T) {
dsls := map[string]func(){
"Body": func() { Body() },
"ErrorName": func() { ErrorName() },
"ErrorName (int)": func() { Type("name", func() { ErrorName(1) }) },
"Example": func() { Example() },
"Response (grpc)": func() { Service("s", func() { GRPC(func() { Response("name") }) }) },
"Response (http)": func() { Service("s", func() { HTTP(func() { Response("name") }) }) },
}
for name, dsl := range dsls {
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 few arguments given to "+strings.Split(name, " ")[0])
})
}
}

func TestTooManyArgError(t *testing.T) {
dsls := map[string]func(){
"APIKey": func() { Type("name", func() { APIKey("scheme", "name", 1, 2, 3) }) },
Expand Down
2 changes: 1 addition & 1 deletion expr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestExample(t *testing.T) {
{"overriding-example", testdata.OverridingExampleDSL, map[string]any{"name": "overridden"}, ""},
{"with-extend", testdata.WithExtendExampleDSL, map[string]any{"name": "example"}, ""},
{"invalid-example-type", testdata.InvalidExampleTypeDSL, nil, "example value map[int]int{1:1} is incompatible with attribute of type map in attribute"},
{"empty-example", testdata.EmptyExampleDSL, nil, "not enough arguments in attribute"},
{"empty-example", testdata.EmptyExampleDSL, nil, "too few arguments given to Example in attribute"},
{"hiding-example", testdata.HidingExampleDSL, nil, ""},
{"overriding-hidden-examples", testdata.OverridingHiddenExamplesDSL, "example", ""},
}
Expand Down