google / jsonnet

Jsonnet - The data templating language
http://jsonnet.org
Apache License 2.0
6.92k stars 438 forks source link

Reflection of function arguments #1106

Open Timmmm opened 1 year ago

Timmmm commented 1 year ago

Is it possible to use reflection to access function arguments? Ideally I would like to reconstruct a string of jsonnet code that I would use to call the function.

My use case is that I have functions that evaluate to a test definition, and I would like them to include their "name" - i.e. how to rerun the test.

local cpptest(source, build_seed, run_seed) = {
    name: 'cpptest(source="%s",build_seed=%d,run_seed=%d)' % [source, build_seed, run_seed],
    ... information about how to build/run the test ...
};

local pythontest(source, run_seed, disable_asserts) = {
    name: 'pythontest(source="%s",run_seed=%d,disable_asserts=%b)' % [source, run_seed, disable_asserts],
    ... information about how to build/run the test ...
};

[
  cpptest("foo.cpp", 0, 1),
  cpptest("foo.cpp", 0, 2),
  cpptest("bar.cpp", 0, 0),
  pythontest("baz.py", 0, false),
]

I can do it manually but it's tedious. One alternative I thought of was to pass the parameters as an object since there is already reflection for that, but it's not very ergonomic. If there's no way to do this, one nice way might be to provide the function arguments as a special object like $args or something so you can use the existing object reflection functions.

I would say accessing the function name is just nice to have. Not too tedious to add manually.

If I implemented something like this would you accept it?