feat(jsonnet): add support for importing all files in a directory#1374
feat(jsonnet): add support for importing all files in a directory#1374partcyborg wants to merge 4 commits into
Conversation
|
If accepted, I will add support for this function to jsonnet-libs Additionally, I noticed that the |
d0afe89 to
a73c5d1
Compare
dsotirakis
left a comment
There was a problem hiding this comment.
@partcyborg 👋
Thanks for your contribution! Would you also be able to add some tests please, for our better understanding and ease of reviewing?
Hey @dsotirakis, thank you for your reply. I have added a unit test for the Update: I noticed i was missing a test for the exclude functionality, so i updated the test to ensure that is working too |
Add native function `importFiles` which reads and evaluates all jsonnet files in a directory. Returns an object of with key/value pairs of `<file name>: <evaluated object>` Directory path is found using a `calledFrom` option, similar to `helmTemplate`. Includes support for: - Choosing files by extension using the `extension` option (default is `.libsonnet`) - Excluding files using the `exclude` option, which takes an array of file names to skip
4c46530 to
0f7d83d
Compare
0f7d83d to
af47706
Compare
zerok
left a comment
There was a problem hiding this comment.
Thank you for proposing this 🙂 I've left you a comment also in the issue with an idea for a different approach 🙂
| } | ||
|
|
||
| func TestImportFiles(t *testing.T) { | ||
| tempDir, err := os.MkdirTemp("", "importFilesTest") |
There was a problem hiding this comment.
suggestion: Please use T.TempDir here. With that you don't need to do the os.RemoveAll manually later on.
There was a problem hiding this comment.
Replaced this with a call to T.TempDir()
| importDir := filepath.Join(tempDir, importDirName) | ||
| err = os.Mkdir(importDir, 0750) | ||
| assert.Nil(t, err) | ||
| importFiles := []string{"test1.libsonnet", "test2.libsonnet"} |
There was a problem hiding this comment.
suggestion: I think it would make the test easier to read if the files were explicitly filled with certain content and the result was compared to an "expected" JSON string 🙂
There was a problem hiding this comment.
I have refactored the test to
- Use static jsonnet content in the imported and excluded files
- Compare the result in json format with the expected json
Please let me know if this is what you were thinking.
| } | ||
|
|
||
| // importFiles imports and evaluates all matching jsonnet files in the given relative directory | ||
| func importFiles(vm *jsonnet.VM) *jsonnet.NativeFunction { |
There was a problem hiding this comment.
todo: Please add documentation about this new function to https://github.com/grafana/tanka/blob/main/docs/src/content/docs/jsonnet/native.md 🙂
There was a problem hiding this comment.
@zerok I am happy to do this, but held off because I don't see documents for helmTemplate here either. My original plan was to add jsonnet library code similar to the helm functions in tanka-util and document those.
That being said, I am happy to also add documentation for the raw native function here, but I would like to get some clarity on whether this will be accepted before I do the work to add it.
|
This breaks the hermitic nature of jsonnet, I'd vouch heavily against doing this in Tanka as we'd be creating a parallel ecosystem that is different from upstream. See upstream discussion here: https://groups.google.com/g/jsonnet/c/JdvlXDAdvq0 |
While I see your point, I don't think has the impact you think it does for a few reasons:
|
|
Why not render the JSON beforehand then? Seems like this could be handled by the tooling pipeline rather than the jsonnet code. The fact that we break hermiticity in another feature does not warrant that we should do it regardless. These features have different tradeoffs. |
Add native function
importFileswhich reads and evaluates all jsonnet files in a directory.Returns an object of with key/value pairs of
<file name>: <evaluated object>Directory path is found using a
calledFromoption, similar tohelmTemplate.Includes support for:
extensionoption (default is.libsonnet)excludeoption, which takes an array of file names to skipFixes #1375