-
-
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
added to_json
function
#4229
added to_json
function
#4229
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1423,3 +1423,56 @@ define_sql_function! { | |
/// ``` | ||
fn array_upper<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, dimension: Integer) -> Nullable<Integer>; | ||
} | ||
|
||
#[cfg(feature = "postgres_backend")] | ||
define_sql_function! { | ||
/// Converts any SQL value to json | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # include!("../../doctest_setup.rs"); | ||
/// # | ||
/// # fn main() { | ||
/// # run_test().unwrap(); | ||
/// # } | ||
/// # | ||
/// # fn run_test() -> QueryResult<()> { | ||
/// # use diesel::dsl::to_json; | ||
/// # use serde_json::Value; | ||
/// # use diesel::sql_types::{Integer, Array, Json, Text, Nullable}; | ||
/// # let connection = &mut establish_connection(); | ||
/// let result = diesel::select(to_json::<Integer, _>(1)) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// let expected: Value = serde_json::from_str("1").unwrap(); | ||
/// assert_eq!(expected, result); | ||
/// | ||
/// let result = diesel::select(to_json::<Array<Integer>, _>(vec![1, 2, 3])) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// let expected: Value = serde_json::from_str("[1, 2, 3]").unwrap(); | ||
/// assert_eq!(expected, result); | ||
/// | ||
/// let result = diesel::select(to_json::<Array<Text>, _>(vec!["abcd", "xyz"])) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// let expected: Value = serde_json::from_str(r#"["abcd", "xyz"]"#).unwrap(); | ||
/// assert_eq!(expected, result); | ||
/// | ||
/// let result = diesel::select(to_json::<Array<Nullable<Text>>, _>(Vec::<String>::new())) | ||
/// .get_result::<Value>(connection)?; | ||
/// | ||
/// let expected: Value = serde_json::from_str("[]").unwrap(); | ||
/// assert_eq!(expected, result); | ||
/// | ||
/// let result = diesel::select(to_json::<Nullable<Array<Text>>, _>(None::<Vec<String>>)) | ||
/// .get_result::<Value>(connection); | ||
/// | ||
/// assert!(result.is_err()); | ||
/// | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn to_json<E: SingleValue>(e: E) -> Json; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function seems to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ive changed the last test a bit, let me know if its okay There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that's not what I had in mind. My idea was to make the return type To explain what's going on there:
By using that as return type we enforce that we return a non-nullable value for non-nullable inputs and nullable types otherwise, which allows us to correctly write the last test case. |
||
} |
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.
can we use the
serde_json::json!
macro instead to construct the expected values?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.
done