Skip to content

Commit ac30be5

Browse files
committed
added null doctest and fixed type signature
1 parent de212f2 commit ac30be5

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

diesel/src/pg/expression/functions.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -2017,22 +2017,28 @@ define_sql_function! {
20172017
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world"]))
20182018
/// .get_result::<Value>(connection)?;
20192019
/// let expected:Value = serde_json::json!({"hello":"world"});
2020-
/// assert_eq!(expected,jsonb);
2020+
/// assert_eq!(expected, jsonb);
20212021
///
2022-
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world","John","Doe"]))
2022+
/// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John","Doe"]))
20232023
/// .get_result::<Value>(connection)?;
20242024
/// let expected:Value = serde_json::json!({"hello": "world","John": "Doe"});
2025-
/// assert_eq!(expected,jsonb);
2025+
/// assert_eq!(expected, jsonb);
20262026
///
2027-
/// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world","John"]))
2028-
/// .get_result::<Value>(connection);
2029-
/// assert!(jsonb.is_err());
2027+
/// let jsonb = diesel::select(jsonb_object::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
2028+
/// .get_result::<Option<Value>>(connection)?;
2029+
/// assert!(jsonb.is_none());
20302030
///
20312031
/// let empty:Vec<String> = Vec::new();
20322032
/// let jsonb = diesel::select(jsonb_object::<Array<Nullable<Text>>,_>(empty))
2033+
/// .get_result::<Value>(connection)?;
2034+
/// let expected = serde_json::json!({});
2035+
/// assert_eq!(expected, jsonb);
2036+
///
2037+
/// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John"]))
20332038
/// .get_result::<Value>(connection);
20342039
/// assert!(jsonb.is_err());
20352040
///
2041+
///
20362042
/// # Ok(())
20372043
/// # }
20382044
/// ```
@@ -2060,19 +2066,19 @@ define_sql_function! {
20602066
/// # use diesel::sql_types::{Array, Nullable, Text};
20612067
/// # use serde_json::Value;
20622068
/// # let connection = &mut establish_connection();
2063-
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, _, _>(
2069+
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
20642070
/// vec!["hello","John"],vec!["world","Doe"]))
20652071
/// .get_result::<Value>(connection)?;
20662072
/// let expected:Value = serde_json::json!({"hello":"world","John":"Doe"});
2067-
/// assert_eq!(expected,jsonb);
2073+
/// assert_eq!(expected, jsonb);
20682074
///
2069-
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Nullable<Array<Text>>, _, _>(
2075+
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Nullable<Array<Text>>, Nullable<Array<Text>>, _, _>(
20702076
/// Some(vec!["hello","John"]),None::<Vec<String>>))
20712077
/// .get_result::<Option<Value>>(connection)?;
20722078
/// assert_eq!(None::<Value>,jsonb);
20732079
///
20742080
/// let empty: Vec<String> = Vec::new();
2075-
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, _, _>(
2081+
/// let jsonb = diesel::select(jsonb_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
20762082
/// vec!["hello","John"],empty))
20772083
/// .get_result::<Value>(connection);
20782084
/// assert!(jsonb.is_err());
@@ -2081,7 +2087,11 @@ define_sql_function! {
20812087
/// # }
20822088
/// ```
20832089
#[sql_name = "jsonb_object"]
2084-
fn jsonb_object_with_keys_and_values<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(
2085-
keys: Arr, values: Arr
2086-
) -> Arr::Out;
2090+
fn jsonb_object_with_keys_and_values<
2091+
Arr1: TextArrayOrNullableTextArray + SingleValue,
2092+
Arr2: TextArrayOrNullableTextArray + CombinedNullableValue<Arr1, Jsonb>
2093+
>(
2094+
keys: Arr1,
2095+
values: Arr2
2096+
) -> Arr2::Out;
20872097
}

diesel/src/pg/expression/helper_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,4 @@ pub type jsonb_object<A> = super::functions::jsonb_object<SqlTypeOf<A>, A>;
527527
#[allow(non_camel_case_types)]
528528
#[cfg(feature = "postgres_backend")]
529529
pub type jsonb_object_with_keys_and_values<K, V> =
530-
super::functions::jsonb_object_with_keys_and_values<SqlTypeOf<K>, K, V>;
530+
super::functions::jsonb_object_with_keys_and_values<SqlTypeOf<K>, SqlTypeOf<V>, K, V>;

0 commit comments

Comments
 (0)