Replace JSONPath with jq in jsondocck by fluiderson · Pull Request #143089 · rust-lang/rust (original) (raw)

I think that porting the current directives instead of declaring new ones might confuse devs that got used to how the current directives work since they won't be exactly they same.

For example, I'm not sure whether has can be ported at all because you can't just check a path existence the same way you do in JSONPath. For nonexistent paths, jq returns null, but if an existing path contains null, you'll get null, too. Instead, you need to use the special has function, which defeats the purpose of the has directive, replacing it with is/jq:

// true (if returns any value, null returned in this case) //@ has .non_existent_field

// false //@ jq .exisiting_field_with_null

// false //@ jq .non_existent_field

// a right way

//@ is has("exisiting_field_with_null"), true //@ is has("non_existent_field"), false

What I'm also trying to do is shift as much work as possible to jq, so that there'd be a minimal set of absolutely obvious directives for contributors that are already familiar with jq without diving into details about using a more appropriate directive instead of using just jq.

For example, with count we'd have multiple ways to count matches:

//@ count .some_array_field, 42 //@ is .some_array_field | length, 42

Without - just the last one.

With ismany, I'd need to get shlex back along with tons of quotes and have multiple ways to assert objects:

//@ ismany '.index[].inner.static.expr' '""' '""' '""' '""' '""' '""' '""' '""' //@ is '[.index[].inner.static.expr]' '["", "", "", "", "", "", "", ""]'

Without - just the last one.


As some kind of a middle-ground, I can suggest to avoid renaming and port only 3 directives: set, is, and !is. And use

not do a general jq directive. (That behavior could be with something like //@ is '.some.jq.query' true, but I don't think this should be encouraged).

Okay, I'll remove it and use (!)is instead. jq is indeed quite generic in comparison to the current directives. And is can be compacted to automatically assert a value with true, if it has no second argument.

What do you think?