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

feat: impl ToRpcParams for serde_json::Value #1430

Closed
wants to merge 2 commits into from

Conversation

niklasad1
Copy link
Member

Superseeds #1426

@niklasad1 niklasad1 requested a review from a team as a code owner July 30, 2024 08:18
Comment on lines +224 to +229
serde_json::Value::Array(a) => serde_json::to_string(&a),
serde_json::Value::Object(o) => serde_json::to_string(&o),
serde_json::Value::Null => Ok("[null]".to_string()),
serde_json::Value::Bool(b) => serde_json::to_string(&[b]),
serde_json::Value::String(s) => serde_json::to_string(&[s]),
serde_json::Value::Number(n) => serde_json::to_string(&[n]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I feel uneasy about here is that it's no longer so clear to me that some values (number, bool, string) are treated as a single param whereas some values (array, object) are spread into multiple params (rather than also being treated as a single param).

I don't think I understood in the first place exactly what motivated the original PR for this, so I guess I'd still ike to understand: why is it useful to be able to pass a Value to an RPC method in this way?

Copy link
Member Author

@niklasad1 niklasad1 Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not completely convinced about this one myself but the rationale is easy-of-use for users instead of introducing a new type pattern on top of serde_json::Value to implement ToRpcParams. This should mostly be used by the client and somewhat redundant to our rpc_params![] macro

So the implementation is opinionated and the reason for that is that params must be represent as JSON object or JSON array that is way primitive types are serialized as an array of size 1 and array/object already are valid JSON-RPC params.

I'm also fine skipping the impl completely and let users deal with that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess I'm thinking that our basic impls are pretty foolproof and hard to misuse, and things like this might lead to some more confusion (eg somebody apssing a Value in as a param, expecting it to be treated as a single param, but because it's an array it's treated as multiple params or whatever).

I'd be happiest leaving it to the user; somebody can implement a simple wrapper or function which takes a serde_json::Value and turns it into some MyType which has a custom impl of ToRpcParams to do whatever behaviour they prefer imo :)

but yeah like I said in the other PR, I'd have no objection either to just serde_json::Map implementing ToRpcParams for instance, if that helps anybody!

@niklasad1 niklasad1 closed this Jul 30, 2024
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