Skip to content

Commit

Permalink
added null doctest and fixed type signature
Browse files Browse the repository at this point in the history
  • Loading branch information
valkrypton committed Sep 18, 2024
1 parent de212f2 commit ac30be5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
36 changes: 23 additions & 13 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,22 +2017,28 @@ define_sql_function! {
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world"]))
/// .get_result::<Value>(connection)?;
/// let expected:Value = serde_json::json!({"hello":"world"});
/// assert_eq!(expected,jsonb);
/// assert_eq!(expected, jsonb);
///
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world","John","Doe"]))
/// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John","Doe"]))
/// .get_result::<Value>(connection)?;
/// let expected:Value = serde_json::json!({"hello": "world","John": "Doe"});
/// assert_eq!(expected,jsonb);
/// assert_eq!(expected, jsonb);
///
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world","John"]))
/// .get_result::<Value>(connection);
/// assert!(jsonb.is_err());
/// let jsonb = diesel::select(jsonb_object::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
/// .get_result::<Option<Value>>(connection)?;
/// assert!(jsonb.is_none());
///
/// let empty:Vec<String> = Vec::new();
/// let jsonb = diesel::select(jsonb_object::<Array<Nullable<Text>>,_>(empty))
/// .get_result::<Value>(connection)?;
/// let expected = serde_json::json!({});
/// assert_eq!(expected, jsonb);
///
/// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John"]))
/// .get_result::<Value>(connection);
/// assert!(jsonb.is_err());
///
///
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -2060,19 +2066,19 @@ define_sql_function! {
/// # use diesel::sql_types::{Array, Nullable, Text};
/// # use serde_json::Value;
/// # let connection = &mut establish_connection();
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, _, _>(
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
/// vec!["hello","John"],vec!["world","Doe"]))
/// .get_result::<Value>(connection)?;
/// let expected:Value = serde_json::json!({"hello":"world","John":"Doe"});
/// assert_eq!(expected,jsonb);
/// assert_eq!(expected, jsonb);
///
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Nullable<Array<Text>>, _, _>(
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Nullable<Array<Text>>, Nullable<Array<Text>>, _, _>(
/// Some(vec!["hello","John"]),None::<Vec<String>>))
/// .get_result::<Option<Value>>(connection)?;
/// assert_eq!(None::<Value>,jsonb);
///
/// let empty: Vec<String> = Vec::new();
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, _, _>(
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
/// vec!["hello","John"],empty))
/// .get_result::<Value>(connection);
/// assert!(jsonb.is_err());
Expand All @@ -2081,7 +2087,11 @@ define_sql_function! {
/// # }
/// ```
#[sql_name = "jsonb_object"]
fn jsonb_object_with_keys_and_values<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(
keys: Arr, values: Arr
) -> Arr::Out;
fn jsonb_object_with_keys_and_values<
Arr1: TextArrayOrNullableTextArray + SingleValue,
Arr2: TextArrayOrNullableTextArray + CombinedNullableValue<Arr1, Jsonb>
>(
keys: Arr1,
values: Arr2
) -> Arr2::Out;
}
2 changes: 1 addition & 1 deletion diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,4 @@ pub type jsonb_object<A> = super::functions::jsonb_object<SqlTypeOf<A>, A>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_object_with_keys_and_values<K, V> =
super::functions::jsonb_object_with_keys_and_values<SqlTypeOf<K>, K, V>;
super::functions::jsonb_object_with_keys_and_values<SqlTypeOf<K>, SqlTypeOf<V>, K, V>;

0 comments on commit ac30be5

Please sign in to comment.