From 378a34431f9bc6035889a655d26f47f612c47596 Mon Sep 17 00:00:00 2001 From: Anna Ahmed Date: Wed, 11 Sep 2024 13:47:27 +0500 Subject: [PATCH 1/2] feat: Implemented jsonb_pretty function --- diesel/src/pg/expression/functions.rs | 73 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 5 ++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 79 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index bc851a5258e5..f5bc6f123f3f 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -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; @@ -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::(json!([{"f1":1,"f2":null},2,null,3]))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"[ + /// { + /// "f1": 1, + /// "f2": null + /// }, + /// 2, + /// null, + /// 3 + /// ]"#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!({"a": 1, "b": "cd"}))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"{ + /// "a": 1, + /// "b": "cd" + /// }"#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!("abc"))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#""abc""#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!(22))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"22"#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!(false))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"false"#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!(null))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"null"#, result); + /// + /// let result = diesel::select(jsonb_pretty::(json!({}))) + /// .get_result::(connection)?; + /// + /// assert_eq!(r#"{ + /// }"#, result); + /// # Ok(()) + /// # } + /// ``` + fn jsonb_pretty(e: E) -> Text; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 81cd81bd10a1..339434c36ec9 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -486,3 +486,8 @@ pub type to_jsonb = super::functions::to_jsonb, E>; #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] pub type json_object = super::functions::json_object, A>; + +/// Return type of [`jsonb_pretty(jsonb)`](super::functions::jsonb_pretty()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type jsonb_pretty = super::functions::jsonb_pretty, E>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 6c96ad2b20b4..ae681db6a300 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -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), ) } From 3c253a8871361e7a6575ef8bcbcdb653ab60650d Mon Sep 17 00:00:00 2001 From: Anna Ahmed Date: Thu, 12 Sep 2024 11:22:45 +0500 Subject: [PATCH 2/2] fix: Added MaybeNullableValue and None test --- diesel/src/pg/expression/functions.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index f5bc6f123f3f..a9102453a82b 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1653,8 +1653,8 @@ define_sql_function! { /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use diesel::dsl::jsonb_pretty; - /// # use serde_json::json; - /// # use diesel::sql_types::Jsonb; + /// # use serde_json::{json, Value}; + /// # use diesel::sql_types::{Jsonb, Nullable}; /// # let connection = &mut establish_connection(); /// let result = diesel::select(jsonb_pretty::(json!([{"f1":1,"f2":null},2,null,3]))) /// .get_result::(connection)?; @@ -1702,8 +1702,13 @@ define_sql_function! { /// /// assert_eq!(r#"{ /// }"#, result); + /// + /// let result = diesel::select(jsonb_pretty::, _>(None::)) + /// .get_result::>(connection)?; + /// + /// assert!(result.is_none()); /// # Ok(()) /// # } /// ``` - fn jsonb_pretty(e: E) -> Text; + fn jsonb_pretty>(e: E) -> E::Out; }