-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Jsonnet lacks slightly when compared to other unix tools that take an input document and an expression and applies that expression to the input document. For example, sed/awk can apply expressions to text documents. jq applies expressions in its own language to a json document. However, jsonnet requires the expressions be embedded in the document and evaluates a single input.
Consider how to chain multiple documents so that result of evaluating each is available to the next. Each document/expression should be able to be a literal on the command line or come from a file. Some symbol needs to be introduced so a subsequent document can reference the prior one.
For example:
$ echo '{a:1}' | jx -f - -e 'x + {b:2}'
{
"a": 1,
"b": 2
}
The first document is stdin as specified by -f -, the second is the expression after -e. Further expressions/documents can be specified with subsequent -f/-e flags.
The first question is what to do about the x at the start of the second expression. We can't just arbitrarily call the previous document x. We probably should not introduce a new symbol either (if one is available - jq/jquery uses $ for the document being processed, but that is already taken in jsonnet). Perhaps allow it to be specified with what would otherwise be invalid syntax. Perhaps a leading = ?
$ echo '{a:1}' | jx -f - -e '=x x + {b:2}' -f '=top output.jsonnet'
That can be interpreted as binding a local at the top level named x in the first case to stdin, and top in the second to the result of the expression.
The second question is whether we can simplify that expression list somehow. -f - can be considered syntactic sugar for import '/dev/stdin'. -f <file> can be import @'<file>' (with single quotes in <file> replaced with two single quotes). If we had some sugar for denoting a file or code, the two options could be unified as command arguments and not need flags at all. e.g. the example above could be:
$ echo '{a:1}' | jx - ':x + {b:2}'
where the leading colon indicates the argument is an expression (code literal).
Combing the two:
$ echo '{a:1}' | jx - '=x :x + {b:2}' -f '=top output.jsonnet'
One thing to consider is that filenames could potentially start with = or :. A valid jsonnet expression cannot start with : so that should be safe to use, but it cannot be differentiated from a file starting with :. Do we need an escape here?
Or something entirely different?