google / go-jsonnet

Apache License 2.0
1.61k stars 231 forks source link

Is it possible to define a native function with a default argument? #751

Open suzuki-shunsuke opened 4 months ago

suzuki-shunsuke commented 4 months ago

Jsonnet's function supports default argument.

https://jsonnet.org/learning/tutorial.html#functions

e.g.

// Default arguments are like Python:
local my_function(x, y=10) = x + y;

https://pkg.go.dev/github.com/google/go-jsonnet@v0.20.0#NativeFunction

NativeFunction has a field Params, which a list of Identifier. Identifier represents a variable / parameter / field name. +gen set.

https://pkg.go.dev/github.com/google/go-jsonnet@v0.20.0/ast#Identifier

I want to define a default argument, but is it possible?

I tried to add a default argument named opts, but it didn't work.

Params: ast.Identifiers{"pattern", "s", "opts"},

If I omit the argument, the error RUNTIME ERROR: Missing argument: opts occurred.

I tried the following code, but it also didn't work. RUNTIME ERROR: Missing argument: opts={}

Params: ast.Identifiers{"pattern", "s", "opts={}"},