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

json_typeof and jsonb_typeof function #4250

Merged
merged 5 commits into from
Sep 12, 2024

Conversation

anna-ahmed19
Copy link
Contributor

Added the json_typeof and jsonb_typeof functions under Issue#4216

@weiznich weiznich requested a review from a team September 11, 2024 07:06
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 opening the PR!

The changes look good, I only would like to see some tests about the null behavior, in past cases, Postgres returns NULL when you pass NULL to this kind of functions.
If this is case, the function signature is wrong, It should also cover the NULL input return.

To fix that we can use

fn json_typeof<E: JsonOrNullableJson + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;

The E: MaybeNulllableValue<Text> says that the output values has the same nullability as the input value. That means Nullable<Json<T>> evaluates to Nullable<Text> and Json<T> evaluates to Text via the E::Out return type

/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
/// # Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a test with None input?
It is something like this:

    /// let result = diesel::select(json_typeof::<Nullable<Json>, _>(None::<Value>))
    ///     .get_result::<String>(connection)?;
    ///
    /// assert_eq!(<something>, result);

/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(null)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!("null".to_string(), result);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add a None test here?

@anna-ahmed19
Copy link
Contributor Author

Thank you @guissalustiano for the suggestions.
I have added MaybeNullableValue<Text> and the None tests.
Let me know any further changes.

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!

@weiznich weiznich added this pull request to the merge queue Sep 12, 2024
@weiznich
Copy link
Member

Thanks for submitting this PR ❤️

Merged via the queue into diesel-rs:master with commit 97898ec Sep 12, 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