Skip to content
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

Merged
merged 5 commits into from
Dec 19, 2024

Conversation

xuehaonan27
Copy link
Contributor

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.

@xuehaonan27
Copy link
Contributor Author

Related to Issue #4366

@weiznich weiznich requested a review from a team December 18, 2024 14:56
Copy link
Member

@weiznich weiznich left a 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;
Copy link
Member

@weiznich weiznich Dec 19, 2024

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;

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Member

@weiznich weiznich left a 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

@weiznich weiznich added this pull request to the merge queue Dec 19, 2024
Merged via the queue into diesel-rs:master with commit 569639d Dec 19, 2024
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants