Skip to content

Commit bc2aac0

Browse files
tchsskraphael
andauthored
Add eval.TooFewArgError() (#3557)
* Add eval.TooFewArgError() * Fix a condition in dsl.ErrorName * Fix a test case in expr.TestExample * Remove an unnecessary block --------- Co-authored-by: Raphael Simon <[email protected]>
1 parent bf6a62c commit bc2aac0

File tree

7 files changed

+51
-29
lines changed

7 files changed

+51
-29
lines changed

dsl/attribute.go

+20-22
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,27 @@ import (
112112
// })
113113
func Attribute(name string, args ...any) {
114114
var parent *expr.AttributeExpr
115-
{
116-
switch def := eval.Current().(type) {
117-
case *expr.AttributeExpr:
118-
parent = def
119-
case expr.CompositeExpr:
120-
parent = def.Attribute()
121-
default:
122-
eval.IncompatibleDSL()
123-
return
124-
}
125-
if parent == nil {
126-
eval.ReportError("invalid syntax, attribute %#v has no parent", name)
115+
switch def := eval.Current().(type) {
116+
case *expr.AttributeExpr:
117+
parent = def
118+
case expr.CompositeExpr:
119+
parent = def.Attribute()
120+
default:
121+
eval.IncompatibleDSL()
122+
return
123+
}
124+
if parent == nil {
125+
eval.ReportError("invalid syntax, attribute %#v has no parent", name)
126+
return
127+
}
128+
if parent.Type == nil {
129+
parent.Type = &expr.Object{}
130+
}
131+
if _, ok := parent.Type.(*expr.Object); !ok {
132+
if _, ok := parent.Type.(*expr.Union); !ok {
133+
eval.ReportError("can't define child attribute %#v on attribute of type %s %T", name, parent.Type.Name(), parent.Type)
127134
return
128135
}
129-
if parent.Type == nil {
130-
parent.Type = &expr.Object{}
131-
}
132-
if _, ok := parent.Type.(*expr.Object); !ok {
133-
if _, ok := parent.Type.(*expr.Union); !ok {
134-
eval.ReportError("can't define child attribute %#v on attribute of type %s %T", name, parent.Type.Name(), parent.Type)
135-
return
136-
}
137-
}
138136
}
139137

140138
var attr *expr.AttributeExpr
@@ -298,7 +296,7 @@ func Default(def any) {
298296
// })
299297
func Example(args ...any) {
300298
if len(args) == 0 {
301-
eval.ReportError("not enough arguments")
299+
eval.TooFewArgError()
302300
return
303301
}
304302
if len(args) > 2 {

dsl/error.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func Error(name string, args ...any) {
173173
// }
174174
func ErrorName(args ...any) {
175175
if len(args) == 0 {
176-
eval.IncompatibleDSL()
176+
eval.TooFewArgError()
177177
return
178178
}
179179
dsl, ok := args[len(args)-1].(func())
@@ -191,8 +191,8 @@ func ErrorName(args ...any) {
191191
case string:
192192
Attribute(actual, args[1:]...)
193193
case int:
194-
if len(args) == 1 {
195-
eval.IncompatibleDSL()
194+
if len(args) == 2 {
195+
eval.TooFewArgError()
196196
return
197197
}
198198
name, ok := args[1].(string)

dsl/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ func SkipResponseBodyEncodeDecode() {
895895
// })
896896
func Body(args ...any) {
897897
if len(args) == 0 {
898-
eval.ReportError("not enough arguments, use Body(name), Body(type), Body(func()) or Body(type, func())")
898+
eval.TooFewArgError()
899899
return
900900
}
901901

dsl/response.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func Code(code int) {
202202

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

261261
func httpError(n string, p eval.Expression, args ...any) *expr.HTTPErrorExpr {
262262
if len(args) == 0 {
263-
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
263+
eval.TooFewArgError()
264264
return nil
265265
}
266266
var (

eval/eval.go

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

129+
// TooFewArgError records a too few arguments error. It is used by DSL
130+
// functions that take dynamic arguments.
131+
func TooFewArgError() {
132+
ReportError("too few arguments given to %s", caller())
133+
}
134+
129135
// TooManyArgError records a too many arguments error. It is used by DSL
130136
// functions that take dynamic arguments.
131137
func TooManyArgError() {

eval/eval_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ func TestInvalidArgError(t *testing.T) {
3535
}
3636
}
3737

38+
func TestTooFewArgError(t *testing.T) {
39+
dsls := map[string]func(){
40+
"Body": func() { Body() },
41+
"ErrorName": func() { ErrorName() },
42+
"ErrorName (int)": func() { Type("name", func() { ErrorName(1) }) },
43+
"Example": func() { Example() },
44+
"Response (grpc)": func() { Service("s", func() { GRPC(func() { Response("name") }) }) },
45+
"Response (http)": func() { Service("s", func() { HTTP(func() { Response("name") }) }) },
46+
}
47+
for name, dsl := range dsls {
48+
t.Run(name, func(t *testing.T) {
49+
err := expr.RunInvalidDSL(t, dsl)
50+
assert.Len(t, strings.Split(err.Error(), "\n"), 1)
51+
assert.Contains(t, err.Error(), "too few arguments given to "+strings.Split(name, " ")[0])
52+
})
53+
}
54+
}
55+
3856
func TestTooManyArgError(t *testing.T) {
3957
dsls := map[string]func(){
4058
"APIKey": func() { Type("name", func() { APIKey("scheme", "name", 1, 2, 3) }) },

expr/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestExample(t *testing.T) {
6363
{"overriding-example", testdata.OverridingExampleDSL, map[string]any{"name": "overridden"}, ""},
6464
{"with-extend", testdata.WithExtendExampleDSL, map[string]any{"name": "example"}, ""},
6565
{"invalid-example-type", testdata.InvalidExampleTypeDSL, nil, "example value map[int]int{1:1} is incompatible with attribute of type map in attribute"},
66-
{"empty-example", testdata.EmptyExampleDSL, nil, "not enough arguments in attribute"},
66+
{"empty-example", testdata.EmptyExampleDSL, nil, "too few arguments given to Example in attribute"},
6767
{"hiding-example", testdata.HidingExampleDSL, nil, ""},
6868
{"overriding-hidden-examples", testdata.OverridingHiddenExamplesDSL, "example", ""},
6969
}

0 commit comments

Comments
 (0)