mirror of
https://github.com/NixOS/nix.git
synced 2025-11-21 01:39:36 +01:00
Clean up JSON utils in a few ways
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 408c09a120, 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>
This commit is contained in:
parent
f0b95b6d5b
commit
0f0d9255c6
6 changed files with 99 additions and 92 deletions
|
|
@ -1,52 +1,21 @@
|
|||
#include "nix/util/json-utils.hh"
|
||||
#include "nix/util/error.hh"
|
||||
#include "nix/util/types.hh"
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include "nix/util/util.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
const nlohmann::json * get(const nlohmann::json & map, const std::string & key)
|
||||
const nlohmann::json & valueAt(const nlohmann::json::object_t & map, std::string_view key)
|
||||
{
|
||||
auto i = map.find(key);
|
||||
if (i == map.end())
|
||||
return nullptr;
|
||||
return &*i;
|
||||
}
|
||||
|
||||
nlohmann::json * get(nlohmann::json & map, const std::string & key)
|
||||
{
|
||||
auto i = map.find(key);
|
||||
if (i == map.end())
|
||||
return nullptr;
|
||||
return &*i;
|
||||
}
|
||||
|
||||
const nlohmann::json & valueAt(const nlohmann::json::object_t & map, const std::string & key)
|
||||
{
|
||||
if (!map.contains(key))
|
||||
if (auto * p = optionalValueAt(map, key))
|
||||
return *p;
|
||||
else
|
||||
throw Error("Expected JSON object to contain key '%s' but it doesn't: %s", key, nlohmann::json(map).dump());
|
||||
|
||||
return map.at(key);
|
||||
}
|
||||
|
||||
std::optional<nlohmann::json> optionalValueAt(const nlohmann::json::object_t & map, const std::string & key)
|
||||
const nlohmann::json * optionalValueAt(const nlohmann::json::object_t & map, std::string_view key)
|
||||
{
|
||||
if (!map.contains(key))
|
||||
return std::nullopt;
|
||||
|
||||
return std::optional{map.at(key)};
|
||||
}
|
||||
|
||||
std::optional<nlohmann::json> nullableValueAt(const nlohmann::json::object_t & map, const std::string & key)
|
||||
{
|
||||
auto value = valueAt(map, key);
|
||||
|
||||
if (value.is_null())
|
||||
return std::nullopt;
|
||||
|
||||
return std::optional{std::move(value)};
|
||||
return get(map, key);
|
||||
}
|
||||
|
||||
const nlohmann::json * getNullable(const nlohmann::json & value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue