-
-
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
Add SQLite function json_pretty
#4395
Add SQLite function json_pretty
#4395
Conversation
Related to Issue #4366 |
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. I think the signature is not right yet, as pointed out in the comment at the function. It would be great to fix that before merging.
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
fn json_pretty<E: TextOrNullableTextOrBinaryOrNullableBinary + MaybeNullableValue<Text>>(e: E) -> E::Out; |
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.
I think this signature is unfortunately not correct yet. The sqlite documentation says:
The json_pretty() function works like json() except that it adds extra whitespace to make the JSON result easier for humans to read. The first argument is the JSON or JSONB that is to be pretty-printed. The optional second argument is a text string that is used for indentation. If the second argument is omitted or is NULL, then indentation is four spaces per level.
which would translate to
fn json_pretty<J: JsonOrJsonbOrNullableJson + MaybeNullableValue<Text>>(j: J) -> J::Out;
this then would require users to call it that way: select(json_pretty(json("your_json_text"))
or select(json_pretty(jsonb(b"your_json_blob"))
As for the optional second argument: We normally deal with that by having a second function definition for this for that.
So something like:
#[sql_name = "json_pretty"]
fn json_pretty_with_indentation<J: JsonOrJsonbOrNullableJson + MaybeNullableValue<Text>>(j: J, indentation: Nullable<Text>) -> J::Out;
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.
Very helpful for me since I'm new to diesel
! Well, are you sure we should use JsonOrJsonbOrNullableJson
? I found JsonOrNullableJsonOrJsonbOrNullableJsonb
in the PostgreSQL implementation, which adds nullable JSONB
implementation.
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.
You are correct we want to use JsonOrNullableJsonOrJsonbOrNullableJsonb
there (any likely also for most of the other sqlite json functions).
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 the fast update. Looks good now
TextOrNullableTextOrBinaryOrNullableBinary
. SQLite doc says "the first argument is the JSON or JSONB that is to be pretty-printed" so I added such a trait. I don't know whether it's proper.