Repository navigation
Replies: 1 comment
|
I'm not a maintainer, but I think the reason is conceptual rather than technical: in Expr the environment is the namespace that identifiers are resolved against. The good news is that a one-key wrapper gives you exactly the jq-style "transform the whole payload in one run" you're after: program, _ := expr.Compile(`map(filter(rows, .age > 30), .name)`,
expr.Env(map[string]any{"rows": []any{}}))
out, _ := expr.Run(program, map[string]any{"rows": data}) // data = your decoded NDJSON/JSON/CSV slice
A note in the environment docs saying "to evaluate over a list, put it under a key" would probably help others coming from jq, so worth suggesting as a docs PR too. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I was wondering if you've ever considered adding slices as a supported environment type - currently only structs and
map[string]anyare supported - https://expr-lang.org/docs/environment.I've checked previous issues and discussions but I haven't managed to find the reasoning behind this limitation anywhere. I apologise in advance if I've missed something and opened a duplicate.
Is there a technical reason behind the fact that slices aren't supported? A note in the documentation would be super helpful if this is the case.
My use case: let's say that I'd like to filter and transform arrays (slices) of data with Expr where the data comes in from arbitrary NDJSON, JSON array, and CSV files, where I would like to skip the creation of an intermediary variable into which I'd pass the slice of data. Having slices supported as an environment type would also mean that a single expression execution could transform the whole payload instead of re-running the expression on each row (I know that they can be compiled for efficiency and all that, but still) 🙂 Essentially I'm looking at Expr from the perspective of something like https://github.com/itchyny/gojq - as a replacement (alternative) for it.
Thank you!
All reactions