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 jsonb_object (both variants) #4256

Merged
merged 3 commits into from
Sep 18, 2024

Conversation

valkrypton
Copy link
Contributor

added support for jsonb_object(text[]) and jsonb_object(keys[], values[]) under #4216

@weiznich weiznich requested a review from a team September 12, 2024 13:45
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.

Thanks for opening this PR. The general definitions look fine, but I think we might need some small tweaks for correctly handling null values in all the cases.

/// # }
/// ```
#[sql_name = "jsonb_object"]
fn jsonb_object_with_keys_and_values<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to use the `CombinedNullableValue trait that I've introduced in b488df4. The signature should be essentially the same as there.

/// # Ok(())
/// # }
/// ```
fn jsonb_object<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test case for calling this function with a null value here? Something like jsonb_object::<Nullable<Array<Text>>, _>(None)?

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.

Thanks for the update. I locally checked how jsonb_object(null) works and it seems to return a null value. Therefore I'm not sure why the doc tests checks for an error instead, that does not seem to be correct.

///
/// let jsonb = diesel::select(jsonb_object::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
/// .get_result::<Option<Value>>(connection);
/// assert!(jsonb.is_err());
Copy link
Member

Choose a reason for hiding this comment

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

That does seem to be not correct, this should return None and no error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I replace the current statement with this:

let jsonb = diesel::select(jsonb_object::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
           .get_result::<Option<Value>>(connection)?;

The test fails because of this error:

called `Result::unwrap()` on an `Err` value: DatabaseError(Unknown, "current transaction is aborted, commands ignored until end of transaction block")

I tried with different variations of the argument type, but couldn't get this error to resolve.

Copy link
Member

Choose a reason for hiding this comment

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

That's likely caused by the error in the statement beforehand. Either get a new connection here or reorder the test cases to first have those without error and then these that return an error.

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.

Thanks for the update, looks good now ❤️

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