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

added support for json_strip_nulls and jsonb_strip_nulls #4255

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3552,6 +3552,10 @@ pub(in crate::pg) mod private {
impl JsonbOrNullableJsonb for Jsonb {}
impl JsonbOrNullableJsonb for Nullable<Jsonb> {}

#[diagnostic::on_unimplemented(
message = "`{Self}` is neither `diesel::sql_types::Json` nor `diesel::sql_types::Nullable<Json>`",
note = "try to provide an expression that produces one of the expected sql types"
)]
pub trait JsonOrNullableJson {}

impl JsonOrNullableJson for Json {}
Expand Down
89 changes: 89 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,3 +1850,92 @@ define_sql_function! {
/// ```
fn jsonb_pretty<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;
}

define_sql_function! {
/// Deletes all object fields that have null values from the given JSON value, recursively.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::json_strip_nulls;
/// # use diesel::sql_types::{Json, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(json_strip_nulls::<Json, _>(json!({"hello": null})))
/// .get_result::<Value>(connection)?;
/// let expected: Value = json!({});
/// assert_eq!(result, expected);
///
/// let result = diesel::select(json_strip_nulls::<Json, _>(json!([{"f1":1, "f2":null}, 2, null, 3])))
/// .get_result::<Value>(connection)?;
/// let expected: Value = json!([{"f1":1}, 2, null, 3]);
/// assert_eq!(result, expected);
///
/// let result = diesel::select(json_strip_nulls::<Nullable<Json>, _>(None::<Value>))
/// .get_result::<Option<Value>>(connection)?;
/// assert!(result.is_none());
///
/// let result = diesel::select(json_strip_nulls::<Json, _>(json!(null)))
/// .get_result::<Value>(connection)?;
/// let expected = json!(null);
/// assert_eq!(result, expected);
///
///
/// # Ok(())
/// # }
/// ```
fn json_strip_nulls<E: JsonOrNullableJson + SingleValue>(json: E) -> E;
}

define_sql_function! {
/// Deletes all object fields that have null values from the given JSON value, recursively.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_strip_nulls;
/// # use diesel::sql_types::{Jsonb, Nullable};
/// # use serde_json::{json, Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(jsonb_strip_nulls::<Jsonb, _>(json!({"hello": null})))
/// .get_result::<Value>(connection)?;
/// let expected: Value = json!({});
/// assert_eq!(result, expected);
///
/// let result = diesel::select(jsonb_strip_nulls::<Jsonb, _>(json!([{"f1":1, "f2":null}, 2, null, 3])))
/// .get_result::<Value>(connection)?;
/// let expected: Value = json!([{"f1":1}, 2, null, 3]);
/// assert_eq!(result, expected);
///
/// let result = diesel::select(jsonb_strip_nulls::<Nullable<Jsonb>, _>(None::<Value>))
/// .get_result::<Option<Value>>(connection)?;
/// assert!(result.is_none());
///
/// let result = diesel::select(jsonb_strip_nulls::<Jsonb, _>(json!(null)))
/// .get_result::<Value>(connection)?;
/// let expected = json!(null);
/// assert_eq!(result, expected);
///
///
///
/// # Ok(())
/// # }
/// ```
fn jsonb_strip_nulls<E: JsonbOrNullableJsonb + SingleValue>(jsonb: E) -> E;
}
10 changes: 10 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,13 @@ pub type jsonb_typeof<E> = super::functions::jsonb_typeof<SqlTypeOf<E>, E>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_pretty<E> = super::functions::jsonb_pretty<SqlTypeOf<E>, E>;

/// Return type of [`json_strip_nulls(json)`](super::functions::json_strip_nulls())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type json_strip_nulls<E> = super::functions::json_strip_nulls<SqlTypeOf<E>, E>;

/// Return type of [`jsonb_strip_nulls(jsonb)`](super::functions::jsonb_strip_nulls())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_strip_nulls<E> = super::functions::jsonb_strip_nulls<SqlTypeOf<E>, E>;
2 changes: 2 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ fn postgres_functions() -> _ {
json_typeof(pg_extras::json),
jsonb_typeof(pg_extras::jsonb),
jsonb_pretty(pg_extras::jsonb),
json_strip_nulls(pg_extras::json),
jsonb_strip_nulls(pg_extras::jsonb),
)
}

Expand Down
Loading