Skip to content

Commit 9300854

Browse files
committed
fix: Resolved conflicts with master
2 parents 3c253a8 + 97898ec commit 9300854

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed

diesel/src/pg/expression/expression_methods.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! PostgreSQL specific expression methods
22
33
pub(in crate::pg) use self::private::{
4-
ArrayOrNullableArray, InetOrCidr, JsonIndex, JsonOrNullableJsonOrJsonbOrNullableJsonb,
5-
JsonRemoveIndex, JsonbOrNullableJsonb, MaybeNullableValue, MultirangeOrNullableMultirange,
6-
MultirangeOrRangeMaybeNullable, RangeHelper, RangeOrNullableRange,
7-
TextArrayOrNullableTextArray, TextOrNullableText,
4+
ArrayOrNullableArray, InetOrCidr, JsonIndex, JsonOrNullableJson,
5+
JsonOrNullableJsonOrJsonbOrNullableJsonb, JsonRemoveIndex, JsonbOrNullableJsonb,
6+
MaybeNullableValue, MultirangeOrNullableMultirange, MultirangeOrRangeMaybeNullable,
7+
RangeHelper, RangeOrNullableRange, TextArrayOrNullableTextArray, TextOrNullableText,
88
};
99
use super::date_and_time::{AtTimeZone, DateTimeLike};
1010
use super::operators::*;
@@ -3552,6 +3552,11 @@ pub(in crate::pg) mod private {
35523552
impl JsonbOrNullableJsonb for Jsonb {}
35533553
impl JsonbOrNullableJsonb for Nullable<Jsonb> {}
35543554

3555+
pub trait JsonOrNullableJson {}
3556+
3557+
impl JsonOrNullableJson for Json {}
3558+
impl JsonOrNullableJson for Nullable<Json> {}
3559+
35553560
/// A trait that describes valid json indices used by postgresql
35563561
pub trait JsonRemoveIndex {
35573562
/// The Expression node created by this index type

diesel/src/pg/expression/functions.rs

+121
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::expression_methods::InetOrCidr;
44
use crate::expression::functions::define_sql_function;
55
use crate::pg::expression::expression_methods::ArrayOrNullableArray;
6+
use crate::pg::expression::expression_methods::JsonOrNullableJson;
67
use crate::pg::expression::expression_methods::JsonbOrNullableJsonb;
78
use crate::pg::expression::expression_methods::MaybeNullableValue;
89
use crate::pg::expression::expression_methods::MultirangeOrNullableMultirange;
@@ -1636,6 +1637,126 @@ define_sql_function! {
16361637
) -> Arr::Out;
16371638
}
16381639

1640+
#[cfg(feature = "postgres_backend")]
1641+
define_sql_function! {
1642+
/// Returns the type of the top-level json value as a text-string
1643+
///
1644+
/// # Example
1645+
///
1646+
/// ```rust
1647+
/// # include!("../../doctest_setup.rs");
1648+
/// #
1649+
/// # fn main() {
1650+
/// # #[cfg(feature = "serde_json")]
1651+
/// # run_test().unwrap();
1652+
/// # }
1653+
/// #
1654+
/// # #[cfg(feature = "serde_json")]
1655+
/// # fn run_test() -> QueryResult<()> {
1656+
/// # use diesel::dsl::json_typeof;
1657+
/// # use serde_json::{json, Value};
1658+
/// # use diesel::sql_types::{Json, Nullable};
1659+
/// # let connection = &mut establish_connection();
1660+
/// let result = diesel::select(json_typeof::<Json, _>(json!({"a": "b", "c": 1})))
1661+
/// .get_result::<String>(connection)?;
1662+
///
1663+
/// assert_eq!("object".to_string(), result);
1664+
///
1665+
/// let result = diesel::select(json_typeof::<Json, _>(json!([1,2,3])))
1666+
/// .get_result::<String>(connection)?;
1667+
///
1668+
/// assert_eq!("array".to_string(), result);
1669+
///
1670+
/// let result = diesel::select(json_typeof::<Json, _>(json!("abc")))
1671+
/// .get_result::<String>(connection)?;
1672+
///
1673+
/// assert_eq!("string".to_string(), result);
1674+
///
1675+
/// let result = diesel::select(json_typeof::<Json, _>(json!(-123.4)))
1676+
/// .get_result::<String>(connection)?;
1677+
///
1678+
/// assert_eq!("number".to_string(), result);
1679+
///
1680+
/// let result = diesel::select(json_typeof::<Json, _>(json!(true)))
1681+
/// .get_result::<String>(connection)?;
1682+
///
1683+
/// assert_eq!("boolean".to_string(), result);
1684+
///
1685+
/// let result = diesel::select(json_typeof::<Json, _>(json!(null)))
1686+
/// .get_result::<String>(connection)?;
1687+
///
1688+
/// assert_eq!("null".to_string(), result);
1689+
///
1690+
/// let result = diesel::select(json_typeof::<Nullable<Json>, _>(None::<Value>))
1691+
/// .get_result::<Option<String>>(connection)?;
1692+
///
1693+
/// assert!(result.is_none());
1694+
/// # Ok(())
1695+
/// # }
1696+
/// ```
1697+
fn json_typeof<E: JsonOrNullableJson + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;
1698+
}
1699+
1700+
#[cfg(feature = "postgres_backend")]
1701+
define_sql_function! {
1702+
/// Returns the type of the top-level jsonb value as a text-string
1703+
///
1704+
/// # Example
1705+
///
1706+
/// ```rust
1707+
/// # include!("../../doctest_setup.rs");
1708+
/// #
1709+
/// # fn main() {
1710+
/// # #[cfg(feature = "serde_json")]
1711+
/// # run_test().unwrap();
1712+
/// # }
1713+
/// #
1714+
/// # #[cfg(feature = "serde_json")]
1715+
/// # fn run_test() -> QueryResult<()> {
1716+
/// # use diesel::dsl::jsonb_typeof;
1717+
/// # use serde_json::{json, Value};
1718+
/// # use diesel::sql_types::{Jsonb, Nullable};
1719+
/// # let connection = &mut establish_connection();
1720+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!({"a": "b", "c": 1})))
1721+
/// .get_result::<String>(connection)?;
1722+
///
1723+
/// assert_eq!("object".to_string(), result);
1724+
///
1725+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!([1,2,3])))
1726+
/// .get_result::<String>(connection)?;
1727+
///
1728+
/// assert_eq!("array".to_string(), result);
1729+
///
1730+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!("abc")))
1731+
/// .get_result::<String>(connection)?;
1732+
///
1733+
/// assert_eq!("string".to_string(), result);
1734+
///
1735+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(-123.4)))
1736+
/// .get_result::<String>(connection)?;
1737+
///
1738+
/// assert_eq!("number".to_string(), result);
1739+
///
1740+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(true)))
1741+
/// .get_result::<String>(connection)?;
1742+
///
1743+
/// assert_eq!("boolean".to_string(), result);
1744+
///
1745+
/// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(null)))
1746+
/// .get_result::<String>(connection)?;
1747+
///
1748+
/// assert_eq!("null".to_string(), result);
1749+
///
1750+
/// let result = diesel::select(jsonb_typeof::<Nullable<Jsonb>, _>(None::<Value>))
1751+
/// .get_result::<Option<String>>(connection)?;
1752+
///
1753+
/// assert!(result.is_none());
1754+
/// # Ok(())
1755+
/// # }
1756+
/// ```
1757+
fn jsonb_typeof<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;
1758+
}
1759+
16391760
#[cfg(feature = "postgres_backend")]
16401761
define_sql_function! {
16411762
/// Converts the given json value to pretty-printed, indented text

diesel/src/pg/expression/helper_types.rs

+10
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ pub type to_jsonb<E> = super::functions::to_jsonb<SqlTypeOf<E>, E>;
487487
#[cfg(feature = "postgres_backend")]
488488
pub type json_object<A> = super::functions::json_object<SqlTypeOf<A>, A>;
489489

490+
/// Return type of [`json_typeof(json)`](super::functions::json_typeof())
491+
#[allow(non_camel_case_types)]
492+
#[cfg(feature = "postgres_backend")]
493+
pub type json_typeof<E> = super::functions::json_typeof<SqlTypeOf<E>, E>;
494+
495+
/// Return type of [`jsonb_typeof(jsonb)`](super::functions::jsonb_typeof())
496+
#[allow(non_camel_case_types)]
497+
#[cfg(feature = "postgres_backend")]
498+
pub type jsonb_typeof<E> = super::functions::jsonb_typeof<SqlTypeOf<E>, E>;
499+
490500
/// Return type of [`jsonb_pretty(jsonb)`](super::functions::jsonb_pretty())
491501
#[allow(non_camel_case_types)]
492502
#[cfg(feature = "postgres_backend")]

diesel_derives/tests/auto_type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ fn postgres_functions() -> _ {
438438
to_json(pg_extras::id),
439439
to_jsonb(pg_extras::id),
440440
json_object(pg_extras::text_array),
441+
json_typeof(pg_extras::json),
442+
jsonb_typeof(pg_extras::jsonb),
441443
jsonb_pretty(pg_extras::jsonb),
442444
)
443445
}

0 commit comments

Comments
 (0)