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

implement json_object with separate keys and values arguments #4253

Merged
merged 4 commits into from
Sep 13, 2024

Conversation

valkrypton
Copy link
Contributor

added support for json_object(keys[],values[]) under #4216

@weiznich weiznich requested a review from a team September 11, 2024 09:09
Copy link
Contributor

@guissalustiano guissalustiano left a comment

Choose a reason for hiding this comment

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

Thanks for open the PR!
The changes look good to me.

@valkrypton
Copy link
Contributor Author

@weiznich i came up with this type signature

fn json_object_with_keys_and_values<K: TextArrayOrNullableTextArray + MaybeNullableValue<Json>, V: TextArrayOrNullableTextArray + MaybeNullableValue<Json>>(
        keys: K, values: V
    ) -> K::Out;

to fix the auto_type error but i dont know if it's correct or not?

/// # }
/// ```
#[sql_name = "json_object"]
fn json_object_with_keys_and_values<K: TextArrayOrNullableTextArray + MaybeNullableValue<Json>, V: TextArrayOrNullableTextArray + MaybeNullableValue<Json>>(
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately that's still not 100% correct as it only couples the nullability of the return value onto the first argument, but this function seems to return also Null if you pass in Null as second argument. (I'm sorry that null value handling is such a mess. I also feel that postgres doesn't document this in a good fashion at all.)

I cannot say without further experimentation how to solve this. I will update the comment as soon as I know more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did write a doc test where the 2nd argument is null and it returns null as expected, but you're right that the output is coupled with the first argument only. I couldn't think of a way to couple it with both arguments

@valkrypton
Copy link
Contributor Author

@weiznich i made a small change to the signature, take a look now

@weiznich
Copy link
Member

I've pushed b488df4 which introduces a new helper trait that essentially evaluates to Nullable<Out> if one of the input types in nullable, that should fix the issue.

@weiznich weiznich added this pull request to the merge queue Sep 13, 2024
Merged via the queue into diesel-rs:master with commit bfd2e45 Sep 13, 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.

3 participants