-
Notifications
You must be signed in to change notification settings - Fork 237
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
Custom builtin functions #223
Comments
It is possible and quite easy. These are called "native functions" in jsonnet. Take a look at The following code explains what a native function is:
The So for example (code taken from our tests):
Then the native function can be accessed using There are some gotchas:
I hope it helps. And btw if you think any of your functions would be useful to a wider crowd, please let us know, we are interested in extending the stdlib. |
Nice, thank you for the explanation. I noticed this in the ksonnet PR. But to make this work you have to effectively recompile jsonnet or wrap jsonnet VM in your own code? |
No, it is not pluggable in this way. And yes, to add a native function, you need to use jsonnet as a library and call Btw could you describe what kind of functionality you want to use as native functions? What are these Go functions for? |
Maybe one day we'll have |
@sbarzowski Just to answer your question about the desired functionality of native functions. Its really nothing complicated that could not be done in jsonnet but I already have these libraries implemented in Go with all unit testing etc. One of the uses cases is that I use ksonnet to parametrize my K8s manifests, there I need for example to merge start arguments for my docker binary with the default arguments, I have a package that does that and checks that there are no duplicates, required flags are there, etc. So really its only string manipulation for most part but I dont want to duplicate that in jsonnet again. |
@sparkprime @sbarzowski I just opened #243 for an idea to extend go-jsonnet with a native
local terraform_output = std.native('exec')('terraform', ['output', '-json']) |
Is there a way how I can call golang functions from jsonnet?
Now that there is a go port of jsonnet and for example ksonnet is adding custom native functions (see here ksonnet/ksonnet#521) are there any plans on how to support this?
I have many packages written in golang (with unit-testing, etc) and now it seems like I will need to rewrite some of them into jsonnet.
The text was updated successfully, but these errors were encountered: