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

add jsonb_set support #4357

Merged
merged 8 commits into from
Nov 27, 2024
Merged

Conversation

accestfromim
Copy link
Contributor

add jsonb_set support function mentioned in issue #4216

@accestfromim
Copy link
Contributor Author

I am not sure if I did it right. I'd be glad if I can receive your instruction

@weiznich weiznich requested a review from a team November 26, 2024 10:43
Copy link
Member

@prkbuilds prkbuilds left a comment

Choose a reason for hiding this comment

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

LGTM. Types are correctly defined as well the nullability, Great Work!

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 submitting this PR.

I've found an potential issue with nullability inference that should be fixed. You can use the existing CombinedNullableValue trait for this.

/// )).get_result::<Value>(connection)?;
/// let expected = json!([]);
/// assert_eq!(result, expected);
///
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry to bring that up, but the following example won't infer nullablity correctly:

    /// let result = diesel::select(jsonb_set::<Jsonb, Nullable<Array<Nullable<Text>>>, _, _, _>(
    ///         // cannot be json!(null)
    ///         json!([]),
    ///         None,
    ///         json!({"foo": 42})
    ///     )).get_result::<Value>(connection)?;

This is expected to only accept a Option<Value> as return type as the return value will be NULL in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your kind instuction.
I fixed it by doing this:
before:

fn jsonb_set<
        E: JsonbOrNullableJsonb + SingleValue,
        Arr: TextArrayOrNullableTextArray + SingleValue
    >(base: E, path: Arr, new_value: E) -> E;

after:

fn jsonb_set<
        E: JsonbOrNullableJsonb + SingleValue,
        Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E,Jsonb>
    >(base: E, path: Arr, new_value: E) -> Arr::Out;

Thanks for your instruction again!

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 Nov 27, 2024
Merged via the queue into diesel-rs:master with commit 6e76f04 Nov 27, 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