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

Tilde operator treats values other than true and 1 as false #327

Closed
kcaashish opened this issue Jul 27, 2023 · 3 comments
Closed

Tilde operator treats values other than true and 1 as false #327

kcaashish opened this issue Jul 27, 2023 · 3 comments

Comments

@kcaashish
Copy link

I tested vals.#(b==~false)#.a with the example provided:

{
  "vals": [
    { "a": 1, "b": "true" },
    { "a": 2, "b": true },
    { "a": 3, "b": false },
    { "a": 4, "b": "0" },
    { "a": 5, "b": 0 },
    { "a": 6, "b": "1" },
    { "a": 7, "b": 1 },
    { "a": 8, "b": "true" },
    { "a": 9, "b": false },
    { "a": 10, "b": null },
    { "a": 11 }
  ],
  "anything": "else"
}

And it gives the desired result: [3,4,5,9,10,11]

However, when we have values other than true or 1, they are treated as false:

{
  "vals": [
    { "a": 1, "b": "something" },
    { "a": 2, "b": "else" },
    { "a": 3, "b": false },
    { "a": 4, "b": "0" },
    { "a": 5, "b": 0 },
    { "a": 6, "b": "1" },
    { "a": 7, "b": 1 },
    { "a": 8, "b": "true" },
    { "a": 9, "b": false },
    { "a": 10, "b": null },
    { "a": 11 }
  ],
  "anything": "else"
}

Gives: [1,2,3,4,5,9,10,11]

Try: https://go.dev/play/p/ey6dS3zFk3p

I don't this is the desired behavior.
Is there a way to get the fields, for which the data is either null or missing?

tidwall added a commit that referenced this issue Jul 27, 2023
This commit fixes an issue with ~false where the it's value was
simply the opposite of ~true. Now ~false explicitly checks for
false-ish values.

Also added ~null and ~* for testing null-ish and non-existent
values.

see #327
@tidwall
Copy link
Owner

tidwall commented Jul 27, 2023

In your case, gjson is simply treating ~false as all values that are not true-ish.

I agree that this is probably not desired.

Rather, we likely want to more explicit, where all true-ish values are true, and all false-ish are false.

I made some updates moments ago to the syntax that addresses the ~false issue.

Now vals.#(b==~false)#.a returns [3,4,5,9,10,11].

I also added two more tilde types:

~null      Converts null and non-existent values to true
~*         Converts any existing value to true

Is there a way to get the fields, for which the data is either null or missing?

Yes, using ~null or ~*:

vals.#(b==~null)#.a   => [10,11]
vals.#(b!=~null)#.a   => [1,2,3,4,5,6,7,8,9]
vals.#(b==~*)#.a      => [1,2,3,4,5,6,7,8,9,10]
vals.#(b!=~*)#.a      => [11]

@kcaashish
Copy link
Author

Great! Just tried and it works like a charm! ~* is a handy addition too. Thank you!

@tidwall
Copy link
Owner

tidwall commented Jul 27, 2023

No problem. I'm happy to hear it's working. :)

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

No branches or pull requests

2 participants