Skip to content

Commit

Permalink
fix the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Le Roux committed Sep 30, 2024
1 parent fd10228 commit 9d5bf4b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
12 changes: 10 additions & 2 deletions diesel/src/pg/expression/expression_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3418,9 +3418,11 @@ where

pub(in crate::pg) mod private {
use crate::sql_types::{
Array, Binary, Cidr, Inet, Integer, Json, Jsonb, MaybeNullableType, Multirange, Nullable,
OneIsNullable, Range, SingleValue, SqlType, Text,
Array, Binary, Cidr, Inet, Integer, MaybeNullableType, Multirange, Nullable, OneIsNullable,
Range, SingleValue, SqlType, Text,
};
#[cfg(feature = "serde_json")]
use crate::sql_types::{Json, Jsonb};
use crate::{Expression, IntoSql};

/// Marker trait used to implement `ArrayExpressionMethods` on the appropriate
Expand Down Expand Up @@ -3558,7 +3560,9 @@ pub(in crate::pg) mod private {
)]
pub trait JsonOrNullableJson {}

#[cfg(feature = "serde_json")]
impl JsonOrNullableJson for Json {}
#[cfg(feature = "serde_json")]
impl JsonOrNullableJson for Nullable<Json> {}

/// A trait that describes valid json indices used by postgresql
Expand Down Expand Up @@ -3646,9 +3650,13 @@ pub(in crate::pg) mod private {
note = "try to provide an expression that produces one of the expected sql types"
)]
pub trait JsonOrNullableJsonOrJsonbOrNullableJsonb {}
#[cfg(feature = "serde_json")]
impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Json {}
#[cfg(feature = "serde_json")]
impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable<Json> {}
#[cfg(feature = "serde_json")]
impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Jsonb {}
#[cfg(feature = "serde_json")]
impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable<Jsonb> {}

pub trait JsonIndex {
Expand Down
1 change: 1 addition & 0 deletions diesel/src/pg/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ToSql<sql_types::Jsonb, Pg> for serde_json::Value {
}

#[cfg(test)]
#[cfg(all(feature = "postgres", feature = "serde_json"))]
mod tests {
use crate::deserialize::FromSql;
use crate::pg::{Pg, PgValue};
Expand Down
31 changes: 20 additions & 11 deletions diesel/src/sql_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub struct Timestamp;
#[cfg(any(
feature = "postgres_backend",
feature = "mysql_backend",
all(feature = "sqlite", feature = "serde_json")
all(feature = "sqlite", feature = "serde_json",)
))]
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[diesel(postgres_type(oid = 114, array_oid = 199))]
Expand Down Expand Up @@ -479,21 +479,24 @@ pub struct Json;
/// }
/// }
///
/// # #[cfg(all(
/// # feature = "serde_json",
/// # any(
/// # feature = "postgres_backend",
/// # all(feature = "sqlite", feature = "returning_clauses_for_sqlite_3_35"),
/// # )
/// # ))]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use diesel::insert_into;
/// # use self::contacts::dsl::*;
/// # let connection = &mut connection_no_data();
/// # #[cfg(all(feature = "postgres_backend", feature = "serde_json"))]
/// # #[cfg(feature = "postgres_backend")]
/// # diesel::sql_query("CREATE TABLE contacts (
/// # id SERIAL PRIMARY KEY,
/// # name VARCHAR NOT NULL,
/// # address JSONB NOT NULL
/// # )").execute(connection)?;
/// # #[cfg(all(
/// # feature = "sqlite",
/// # feature = "serde_json",
/// # feature = "returning_clauses_for_sqlite_3_35"
/// # ))]
/// # #[cfg(feature = "sqlite")]
/// # diesel::sql_query("CREATE TABLE contacts (
/// # id INT PRIMARY KEY,
/// # name TEXT NOT NULL,
Expand All @@ -512,12 +515,18 @@ pub struct Json;
/// assert_eq!(santas_address, inserted_address);
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "serde_json"))]
/// # #[cfg(not(all(
/// # feature = "serde_json",
/// # any(
/// # feature = "postgres_backend",
/// # all(feature = "sqlite", feature = "returning_clauses_for_sqlite_3_35"),
/// # )
/// # )))]
/// # fn main() {}
/// ```
#[cfg(any(
feature = "postgres_backend",
all(feature = "sqlite", feature = "serde_json")
#[cfg(all(
feature = "serde_json",
any(feature = "postgres_backend", feature = "sqlite")
))]
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[diesel(postgres_type(oid = 3802, array_oid = 3807))]
Expand Down
5 changes: 5 additions & 0 deletions diesel/src/sqlite/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ const JSONB_TEXTRAW: u8 = 0x0A;
const JSONB_ARRAY: u8 = 0x0B;
const JSONB_OBJECT: u8 = 0x0C;

#[cfg(all(feature = "sqlite", feature = "serde_json"))]
impl FromSql<sql_types::Json, Sqlite> for serde_json::Value {
fn from_sql(value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
serde_json::from_str(value.read_text()).map_err(|_| "Invalid Json".into())
}
}

#[cfg(all(feature = "sqlite", feature = "serde_json"))]
impl ToSql<sql_types::Json, Sqlite> for serde_json::Value {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(serde_json::to_string(self)?);
Ok(IsNull::No)
}
}

#[cfg(all(feature = "sqlite", feature = "serde_json"))]
impl FromSql<sql_types::Jsonb, Sqlite> for serde_json::Value {
fn from_sql(value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
let bytes = value.read_blob();
Expand All @@ -47,6 +50,7 @@ impl FromSql<sql_types::Jsonb, Sqlite> for serde_json::Value {
}
}

#[cfg(all(feature = "sqlite", feature = "serde_json"))]
impl ToSql<sql_types::Jsonb, Sqlite> for serde_json::Value {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
// Create a buffer to hold the binary JSONB encoding
Expand Down Expand Up @@ -451,6 +455,7 @@ fn write_jsonb_object(
}

#[cfg(test)]
#[cfg(all(feature = "postgres_backend", feature = "serde_json"))]
mod tests {
use super::*;
use crate::query_dsl::RunQueryDsl;
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/sqlite/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod date_and_time;
#[cfg(all(feature = "sqlite", feature = "serde_json"))]
#[cfg(all(feature = "sqlite", feature = "serde_json",))]
mod json;
mod numeric;

Expand Down
7 changes: 6 additions & 1 deletion diesel/src/type_impls/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ use crate::sql_types::Jsonb;
#[derive(AsExpression, FromSqlRow)]
#[diesel(foreign_derive)]
#[diesel(sql_type = Json)]
#[cfg_attr(any(feature = "postgres_backend", feature = "sqlite"), diesel(sql_type = Jsonb))]
#[cfg_attr(
any(
feature = "postgres_backend",
feature = "sqlite"
),
diesel(sql_type = Jsonb))]
struct SerdeJsonValueProxy(serde_json::Value);

0 comments on commit 9d5bf4b

Please sign in to comment.