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

jsonb_pretty function #4254

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
73 changes: 73 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::expression_methods::InetOrCidr;
use crate::expression::functions::define_sql_function;
use crate::pg::expression::expression_methods::ArrayOrNullableArray;
use crate::pg::expression::expression_methods::JsonbOrNullableJsonb;
use crate::pg::expression::expression_methods::MaybeNullableValue;
use crate::pg::expression::expression_methods::MultirangeOrNullableMultirange;
use crate::pg::expression::expression_methods::MultirangeOrRangeMaybeNullable;
Expand Down Expand Up @@ -1634,3 +1635,75 @@ define_sql_function! {
text_array: Arr,
) -> Arr::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Converts the given json value to pretty-printed, indented text
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_pretty;
/// # use serde_json::json;
/// # use diesel::sql_types::Jsonb;
/// # let connection = &mut establish_connection();
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!([{"f1":1,"f2":null},2,null,3])))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"[
/// {
/// "f1": 1,
/// "f2": null
/// },
/// 2,
/// null,
/// 3
/// ]"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!({"a": 1, "b": "cd"})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"{
/// "a": 1,
/// "b": "cd"
/// }"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!("abc")))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#""abc""#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(22)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"22"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(false)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"false"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(null)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"null"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!({})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"{
/// }"#, 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 case with a None input?

/// # }
/// ```
fn jsonb_pretty<E: JsonbOrNullableJsonb + SingleValue>(e: E) -> Text;
Copy link
Member

@weiznich weiznich Sep 11, 2024

Choose a reason for hiding this comment

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

Suggested change
fn jsonb_pretty<E: JsonbOrNullableJsonb + SingleValue>(e: E) -> Text;
fn jsonb_pretty<E: JsonbOrNullableJsonb + MaybeNullableValue<Text> + SingleValue>(e: E) -> E::Out;

to correctly handle null value inputs.

See the longer explanation here how this works. (You are not expected to know this on your own, I just don't want to copy this comment everywhere)

}
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,8 @@ pub type to_jsonb<E> = super::functions::to_jsonb<SqlTypeOf<E>, E>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type json_object<A> = super::functions::json_object<SqlTypeOf<A>, A>;

/// Return type of [`jsonb_pretty(jsonb)`](super::functions::jsonb_pretty())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_pretty<E> = super::functions::jsonb_pretty<SqlTypeOf<E>, E>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ fn postgres_functions() -> _ {
to_json(pg_extras::id),
to_jsonb(pg_extras::id),
json_object(pg_extras::text_array),
jsonb_pretty(pg_extras::jsonb),
)
}

Expand Down
Loading