-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Sqllite json_valid #4465
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening this PR and sorry for taking some days to get back with hints what's wrong here.
The implementation is already on a good way, there are only minor nits that needs to be fixed to run the tests + make this good to merge.
/// # } | ||
/// ``` | ||
#[sql_name = "json_valid"] | ||
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J) -> Integer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J) -> Integer; | |
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Bool>>(j: J) -> J::Out; |
Diesel maps such flag integers to a boolean for sqlite so we use Bool
as return type. That would usually be -> Bool
. For this particular function sqlite returns NULL
if a null value is passed in, which means we want to return Nullable<Bool>
if the input value is nullable and Bool
otherwise. The MaybeNullableValue<Bool>
part takes care of that by transforming the generic parameter into the right variant. That variant then can be accessed via the Out
associated type.
/// } | ||
/// | ||
/// let result = diesel::select(json_valid::<Json>(json!({"x":35}))) | ||
/// .get_result::<Integer>(connection)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// .get_result::<Integer>(connection)?; | |
/// .get_result::<bool>(connection)?; |
/// # | ||
/// # #[cfg(feature = "serde_json")] | ||
/// # fn run_test() -> QueryResult<()> { | ||
/// # use diesel::dsl::{sql, json_pretty_with_indentation}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// # use diesel::dsl::{sql, json_pretty_with_indentation}; | |
/// # use diesel::dsl::{sql, json_valid}; |
|
||
#[cfg(feature = "sqlite")] | ||
define_sql_function! { | ||
/// Returns 1 if the argument is well-formed JSON, or returns 0 if is not well-formed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns 1 if the argument is well-formed JSON, or returns 0 if is not well-formed. | |
/// Returns `true` if the argument is well-formed JSON, or returns `false` if is not well-formed. |
/// Return type of [`json_valid(json)`](super::functions::json_valid()) | ||
#[allow(non_camel_case_types)] | ||
#[cfg(feature = "sqlite")] | ||
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, Integer>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, Integer>; | |
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, E>; |
That needs to be just E
and not Integer
as the helper type accepts one generic argument for the SQL type (the SqlTypeOf<E>
part) and one generic argument for the expression itself (the E
))
(That also means removing the use crate::sql_types::Integer;
line again)
Related to #4366
also I couldn't test the functionality fully, and I've asked a question related to testing in #4463