mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Simplistic argument checking
To be expanded when optional arguments arrive.
This commit is contained in:
parent
a92b30146a
commit
544fe25700
1
testdata/bad_function_call.golden
vendored
Normal file
1
testdata/bad_function_call.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
RUNTIME ERROR: function expected 1 argument(s), but got 0
|
1
testdata/bad_function_call.jsonnet
vendored
Normal file
1
testdata/bad_function_call.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
(function(x) x)()
|
1
testdata/bad_function_call2.golden
vendored
Normal file
1
testdata/bad_function_call2.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
RUNTIME ERROR: function expected 1 argument(s), but got 2
|
1
testdata/bad_function_call2.jsonnet
vendored
Normal file
1
testdata/bad_function_call2.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
(function(x) x)(1, 2)
|
1
testdata/bad_function_call_and_error.golden
vendored
Normal file
1
testdata/bad_function_call_and_error.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
RUNTIME ERROR: function expected 1 argument(s), but got 2
|
1
testdata/bad_function_call_and_error.jsonnet
vendored
Normal file
1
testdata/bad_function_call_and_error.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
(function(x) x)(error "x", error "y")
|
@ -80,7 +80,10 @@ func makeCallThunk(ec evalCallable, args callArguments) potentialValue {
|
||||
|
||||
func (th *callThunk) getValue(i *interpreter, trace *TraceElement) (value, error) {
|
||||
evaluator := makeEvaluator(i, trace)
|
||||
// TODO(sbarzowski): actually this trace is kinda useless inside...
|
||||
err := checkArguments(evaluator, th.args, th.function.Parameters())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return th.function.EvalCall(th.args, evaluator)
|
||||
}
|
||||
|
||||
|
10
value.go
10
value.go
@ -244,6 +244,16 @@ func (f *valueFunction) parameters() ast.Identifiers {
|
||||
return f.ec.Parameters()
|
||||
}
|
||||
|
||||
func checkArguments(e *evaluator, args callArguments, params ast.Identifiers) error {
|
||||
// TODO(sbarzowski) this will get much more complicated with named params
|
||||
numPassed := len(args.positional)
|
||||
numExpected := len(params)
|
||||
if numPassed != numExpected {
|
||||
return e.Error(fmt.Sprintf("function expected %v argument(s), but got %v", numExpected, numPassed))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *valueFunction) typename() string {
|
||||
return "function"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user