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

added support for json_array_length and jsonb_array_length functions #4252

Merged

Conversation

zaira-bibi
Copy link

Added support for json_array_length and jsonb_array_length functions under #4216.

Help needed:

  1. I can't figure out the correct signature for both of the functions. I have implemented a JsonOrNullableJson trait but it does not tell that the functions are for json arrays only.
  2. Under the current signature, I can't seem to test for nullability as json!(null) gives a scalar.

I'd really appreciate some guidance on this. TIA.

@zaira-bibi zaira-bibi marked this pull request as ready for review September 11, 2024 09:02
@weiznich weiznich requested a review from a team September 11, 2024 09:09
@zaira-bibi
Copy link
Author

@weiznich I would really appreciate some help on the issues mentioned in the PR description. Thanks!

@zaira-bibi zaira-bibi marked this pull request as draft September 11, 2024 11:25
/// # }
/// ```

fn json_array_length<E: JsonOrNullableJson + SingleValue>(e: E) -> Integer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking a few days to write this answer, this somehow got lost between all the other PR's 😞

This is already mostly the correct signature. We cannot restrict this function to only accept json arrays at compile time as that's a purely runtime concept, so it's fine to accept JsonOrNullableJson here. What needs to be done is to make the nullability of the return value dependend on the input nullability, which means if you put in a Nullable<Json> we want to return a Nullable<Integer> as the function returns null for null inputs.

You can do that by changing the function signature as follows:

Suggested change
fn json_array_length<E: JsonOrNullableJson + SingleValue>(e: E) -> Integer;
fn json_array_length<E: JsonOrNullableJson + MaybeNullableValue<Integer>>(e: E) -> E::Out;

As for the test: You should be able to test for null values like this:

let result = diesel::select(json_array_length::<Nullable<Json>, _>(None::<serde_json::Value>)).get_result::<Option<i32>(conn);

(The same applies to the jsonb variant of this function)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the explanation! I have pushed in a new commit for the required changed. Please let me know if anything else needs to be changed/fixed. Thanks once again!

@zaira-bibi zaira-bibi marked this pull request as ready for review September 16, 2024 05:58
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, thanks 🙏

@weiznich weiznich added this pull request to the merge queue Sep 16, 2024
Merged via the queue into diesel-rs:master with commit 810c3e3 Sep 16, 2024
48 checks passed
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

Successfully merging this pull request may close these issues.

2 participants