-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
added support for json_array_length
and jsonb_array_length
functions
#4252
Conversation
@weiznich I would really appreciate some help on the issues mentioned in the PR description. Thanks! |
/// # } | ||
/// ``` | ||
|
||
fn json_array_length<E: JsonOrNullableJson + SingleValue>(e: E) -> Integer; |
There was a problem hiding this comment.
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:
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)
There was a problem hiding this comment.
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!
There was a problem hiding this 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 🙏
Added support for
json_array_length
andjsonb_array_length
functions under #4216.Help needed:
JsonOrNullableJson
trait but it does not tell that the functions are for json arrays only.json!(null)
gives a scalar.I'd really appreciate some guidance on this. TIA.