-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: rpc: use convenience fn to auto parse non-string parameters #26506
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
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsNo conflicts as of last run. |
What is "non-RFC JSON values"? |
The documentation/naming quite confused me too, as it seems to imply that the string contains a notation that is not JSON RFC compliant, e.g. parse "TRUE" to
Perhaps I could rename |
f4cdeaf
to
bd1685c
Compare
4334cae
to
32bfd59
Compare
With #19762 now merged, PR is rebased and ready for review. |
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.
Code review ACK 32bfd59. This makes client code less repetitive and messy.
re: #26506 (comment)
Perhaps I could rename
ParseNonRFCJSONValue()
I believe since jgarzik/univalue#31 the ParseNonRFCJSONValue()
function is obsolete and you can just call UniValue::read
directly instead. IIRC before that PR UniValue::read
could only parse JSON object and array values, but now it will parse string/number/boolean/null values as well. @laanwj pointed this out in #9028 (comment)
I didn't know that background - thanks for sharing. I already thought it was unnecessary so I've addressed it in #26612 b143564. I think there is merit in keeping the function to maintain the error throwing behaviour that |
32bfd59
to
135e832
Compare
Force pushed to:
|
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.
left a comment
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.
lgtm, left some nits
135e832
to
ae7c013
Compare
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.
Force pushed to address review comments from MarcoFalke:
- renamed
ParseIfNonStringParam()
->ArgToUniValue()
- improved parameter naming of
ArgToUniValue()
- tidy and fixed clang-format warnings in new code
Minimizes code duplication and improves function naming by having a single (overloaded) convenience function that both checks if the parameter is a non-string parameter and automatically parses the value if so.
ae7c013
to
6d0ab07
Compare
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.
ACK 6d0ab07
Looks good and simplifies the code.
Reviewers of this PR might be interested in the follow-up: #26612 |
…non-string parameters 6d0ab07 refactor: use convenience fn to auto parse non-string parameters (stickies-v) Pull request description: Minimizes code duplication and improves function naming by having a single (overloaded) convenience function `ParseIfNonString` that both checks if the parameter is a non-string parameter and automatically parses the value if so. ACKs for top commit: aureleoules: ACK 6d0ab07 Tree-SHA512: 8cbf68a17cfbdce1e31a19916f447a2965c139fdef00c19e32a9b679f4a4015dfe69ceea0bbe1723711e1c5033ea8d4005d1f4485dfbeea22226140f8cbe8aa3
… as string_view 545ff92 refactor: use string_view for RPC named argument values (stickies-v) 7727603 refactor: reduce unnecessary complexity in ParseNonRFCJSONValue (stickies-v) 1d02e59 test: add cases to JSON parsing (stickies-v) Pull request description: Inspired by MarcoFalke's [comment](bitcoin/bitcoin#26506 (comment)). Main purpose of this PR is to minimize copying (potentially large) RPC named arguments when calling `.substr()` by using `std::string_view` instead of `std::string`. Furthermore, cleans up the code by removing unnecessary complexity in `ParseNonRFCJSONValue()` (done first to avoid refactoring required to concatenate `string` and `string_view`), updates some naming and adds a few test cases. Should not introduce any behaviour change. ## Questions - ~Was there actually any merit to `ParseNonRFCJSONValue()` surrounding the value with brackets and then parsing it as an array? I don't see it, and the new approach doesn't fail any tests. Still a bit suspicious about it though.~ - Cleared up by bitcoin/bitcoin#26506 (review) - If there are no objections to 7727603, I think we should follow up with a PR to rename `ParseNonRFCJSONValue()` to a local `Parse()` helper function (that throws if invalid), remove it from `client.h` and merge the test coverage we currently have on `ParseNonRFCJSONValue()` with the coverage we have on `UniValue::read()`. ACKs for top commit: ryanofsky: Code review ACK 545ff92 MarcoFalke: review ACK 545ff92 📻 Tree-SHA512: b1c89fb010ac9c3054b023cac1acbba2a539a09cf39a7baffbd7f7571ee268d5a6d98701c7ac10d68a814526e8fd0fe96ac1d1fb072f272033e415b753f64a5c
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, a new local Parse() function is introduced that throws on invalid JSON input, but is not in the header file.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, a new local Parse() function is introduced that throws on invalid JSON input, but is not in the header file.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, a new local Parse() function is introduced that throws on invalid JSON input, but is not in the header file.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, a new local Parse() function is introduced that throws on invalid JSON input, but is not in the header file.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, a new local Parse() function is introduced that throws on invalid JSON input, but is not in the header file.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, we keep the function to throw on invalid input data but rename it to Parse() and remove it from the header.
As per bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, we keep the function to throw on invalid input data but rename it to Parse() and remove it from the header.
…f ParseNonRFCJSONValue() and rename it cfbc8a6 refactor: rpc: hide and rename ParseNonRFCJSONValue() (stickies-v) 6c8bde6 test: move coverage on ParseNonRFCJSONValue() to UniValue::read() (stickies-v) Pull request description: Follow-up to bitcoin/bitcoin#26612 (comment). As per bitcoin/bitcoin#26506 (review), `ParseNonRFCJSONValue()` is no longer necessary and we can use `UniValue::read()` directly: > IIRC before that PR UniValue::read could only parse JSON object and array values, but now it will parse string/number/boolean/null values as well. laanwj pointed this out in bitcoin/bitcoin#9028 (comment) The implementation of `ParseNonRFCJSONValue()` was already [simplified in #26612](https://github.com/bitcoin/bitcoin/pull/26612/files#diff-84c7a7f36362b9724c31e5dec9879b2f81eae0d0addbc9c0933c3558c577de65R259-R263) and [test coverage updated](https://github.com/bitcoin/bitcoin/pull/26612/files#diff-fc0f86b6c3bb23b0e983bcf79d7546d1c9eaa15d6e4d8a7b03b5b85955f585abR292-R312) to ensure behaviour didn't change. To avoid code duplication, we keep the function to throw on invalid input data but rename it to `Parse()` and remove it from the header. The existing test coverage we had on `ParseNonRFCJSONValue()` is moved over to `UniValue::read()`. ACKs for top commit: ryanofsky: Code review ACK cfbc8a6. Only change since last review is adding a new test Tree-SHA512: 89be959d2353af7ace0c1348ba1600b9ac1d3c7b3cf7f0b59f6e005b7fb9d695ce3e8720e1be3cf77fe7e318a4017c880df108928e7179ec50447583d13bc781
As per bitcoin/bitcoin#26506 (review), this function is no longer necessary and we can use UniValue::read() directly. To avoid code duplication, we keep the function to throw on invalid input data but rename it to Parse() and remove it from the header.
Minimizes code duplication and improves function naming by having a single (overloaded) convenience function
ParseIfNonString
that both checks if the parameter is a non-string parameter and automatically parses the value if so.