Clean up JSON utils in a few ways#14351
Merged
Ericson2314 merged 1 commit intoNixOS:masterfrom Oct 25, 2025
Merged
Conversation
21a27c3 to
b0b9739
Compare
Ericson2314
commented
Oct 25, 2025
Comment on lines
33
to
36
| /** | ||
| * Prevents bugs; see `get` for the same trick. | ||
| */ | ||
| const nlohmann::json * optionalValueAt(const nlohmann::json::object_t && value, const std::string & key) = delete; |
Member
Author
There was a problem hiding this comment.
Ah, it is the same problem as before. This allowed me to fix it.
(Really, this whole function is just an alias for get, and could be deleted.)
b0b9739 to
e11f331
Compare
Ericson2314
commented
Oct 25, 2025
Comment on lines
36
to
37
| const nlohmann::json & valueAt(const nlohmann::json::object_t && map, std::string_view key) = delete; | ||
| const nlohmann::json * optionalValueAt(const nlohmann::json::object_t && value, std::string_view key) = delete; |
Member
Author
There was a problem hiding this comment.
Did the delete trick for both of them now
Ericson2314
commented
Oct 25, 2025
Comment on lines
+25
to
+31
| const nlohmann::json & valueAt(const nlohmann::json::object_t & map, std::string_view key); | ||
|
|
||
| std::optional<nlohmann::json> optionalValueAt(const nlohmann::json::object_t & value, const std::string & key); | ||
| std::optional<nlohmann::json> nullableValueAt(const nlohmann::json::object_t & value, const std::string & key); | ||
| /** | ||
| * @return A pointer to the value assiocated with `key` if `value` | ||
| * contains `key`, otherwise return `nullptr` (not JSON `null`!). | ||
| */ | ||
| const nlohmann::json * optionalValueAt(const nlohmann::json::object_t & value, std::string_view key); |
Member
Author
There was a problem hiding this comment.
Using std::string_view is just better, and made GCC happy
e11f331 to
afa5e8f
Compare
xokdvium
reviewed
Oct 25, 2025
xokdvium
reviewed
Oct 25, 2025
668470a to
88a34c9
Compare
In particular - Remove `get`, it is redundant with `valueAt` and the `get` in `util.hh`. - Remove `nullableValueAt`. It is morally just the function composition `getNullable . valueAt`, not an orthogonal combinator like the others. - `optionalValueAt` return a pointer, not `std::optional`. This also expresses optionality, but without creating a needless copy. This brings it in line with the other combinators which also return references. - Delete `valueAt` and `optionalValueAt` taking the map by value, as we did for `get` in 408c09a, which prevents bugs / unnecessary copies. `adl_serializer<DerivationOptions::OutputChecks>::from_json` was the one use of `getNullable`. I give it a little static function for the ultimate creation of a `std::optional` it does need to do (after switching it to using `getNullable . valueAt`. That could go in `json-utils.hh` eventually, but I didn't bother for now since only one things needs it. Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
88a34c9 to
0f0d925
Compare
xokdvium
approved these changes
Oct 25, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
In particular
Remove
get, it is redundant withvalueAtand thegetinutil.hh.Remove
nullableValueAt. It is morally just the function compositiongetNullable . valueAt, not an orthogonal combinator like the others.optionalValueAtreturn a pointer, notstd::optional. This also expresses optionality, but without creating a needless copy. This brings it in line with the other combinators which also return references.Delete
valueAtandoptionalValueAttaking the map by value, as we did forgetin 408c09a, which prevents bugs / unnecessary copies.Context
adl_serializer<DerivationOptions::OutputChecks>::from_jsonwas the one use ofgetNullable. I give it a little static function for the ultimate creation of astd::optionalit does need to do (after switching it to usinggetNullable . valueAt. That could go injson-utils.hheventually, but I didn't bother for now since only one things needs it.Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.