Skip to content
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

Proposal: Value type-checks #172

Open
gregsdennis opened this issue Mar 16, 2022 · 5 comments
Open

Proposal: Value type-checks #172

gregsdennis opened this issue Mar 16, 2022 · 5 comments
Labels

Comments

@gregsdennis
Copy link
Collaborator

NOTE I definitely think support for this proposal should not be part of the initial release but should be considered for follow-up releases. I had the idea and wanted to document it.

Scenario

Given the following JSON

[
  "foo",
  5,
  { "bar": null },
  "baz",
  null
]

there is no way to isolate only string values.

Proposal

To address this, we should introduce a type check.

I would like to propose the is operator for JSON Path expressions in the filter selector [?()]. Examples of usage would be:

@ is boolean
@ is string
@ is number
@.foo is object

To support this, we would need to reserve object, array, boolean, number, null, and string as reserved keywords in expressions, meaning that functions could not bear these names. I don't think that this would cause any problems.

It could also support value coercion on the RHS. For example @ is $.foo, could return true if $.foo is a string containing one of these type keywords.

A sub-par alternative

I had also considered a type(x) function to build on my proposal for functions.

The type(x) function would take a single parameter and return... well that's the problem. What would it return? It can't rightly return a string. It would have to return some implementation-internal concept of a "value type", and then we'd have to add support for equality.

type(@) == object and type(@) == "object" just don't feel right.

I think the is operator is the way to go for this feature.

@cabo
Copy link
Member

cabo commented Mar 29, 2022

A function syntax would have the advantage that it could use an extension point, while new syntax such as " is " always would lead to harder to manage syntax errors.

@gregsdennis
Copy link
Collaborator Author

A function syntax would have the advantage that it could use an extension point, while new syntax such as " is " always would lead to harder to manage syntax errors.

I'm not sure why is would be any harder to parse than any other equivalence operator, e.g. == or !=.

And again, what would a function return? How would you test that a value is, for example, an array?

@cabo cabo added the enhancement New feature or request label Jun 14, 2022
@timbray
Copy link
Contributor

timbray commented Jun 14, 2022

Captured as a request for enhancement.

@timbray timbray closed this as completed Jun 14, 2022
@gregsdennis
Copy link
Collaborator Author

Where was this captured? Isn't that the point of this issue? Why close it? It should be marked as an enhancement (done) and left open until added or we've decided we're not going to do it.

@cabo
Copy link
Member

cabo commented Jun 14, 2022

Actually, it should be labeled "revisit-after-base-done", which I just did. Thanks for catching this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants