Skip to content

Commit

Permalink
Add functions to validate nested JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
iFreilicht committed Feb 29, 2024
1 parent 91cd38d commit bddf5c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1392,8 +1392,7 @@ Derivation Derivation::fromJSON(
std::function<DerivedPathMap<StringSet>::ChildNode(const nlohmann::json &)> doInput;
doInput = [&](const auto & json) {
DerivedPathMap<StringSet>::ChildNode node;
node.value = static_cast<const StringSet &>(
ensureType(valueAt(json, "outputs"), value_t::array));
node.value = getStringSet(valueAt(json, "outputs"));
for (auto & [outputId, childNode] : getObject(valueAt(json, "dynamicOutputs"))) {
xpSettings.require(Xp::DynamicDerivations);
node.childMap[outputId] = doInput(childNode);
Expand All @@ -1411,8 +1410,8 @@ Derivation Derivation::fromJSON(

res.platform = getString(valueAt(json, "system"));
res.builder = getString(valueAt(json, "builder"));
res.args = ensureType(valueAt(json, "args"), value_t::array);
res.env = ensureType(valueAt(json, "env"), value_t::object);
res.args = getStringList(valueAt(json, "args"));
res.env = getStringMap(valueAt(json, "env"));

return res;
}
Expand Down
32 changes: 32 additions & 0 deletions src/libutil/json-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,36 @@ const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value)

return value.get_ref<const nlohmann::json::boolean_t &>();
}

StringSet getStringSet(const nlohmann::json & value)
{
StringSet stringSet = {};
auto asArray = getArray(value);
for(auto & elem: asArray){
stringSet.insert(getString(elem));
}
return stringSet;
}

Strings getStringList(const nlohmann::json & value)
{
Strings stringList = {};
auto asArray = getArray(value);
for(auto & elem: asArray){
stringList.push_back(getString(elem));
}
return stringList;
}


StringMap getStringMap(const nlohmann::json & value)
{
StringMap stringMap = {};
auto asObject = getObject(value);
for(auto & [key, elem]: asObject){
stringMap.insert(key, getString(elem));
}
return stringMap;
}

}
4 changes: 4 additions & 0 deletions src/libutil/json-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <nlohmann/json.hpp>
#include <list>
#include "types.hh"

namespace nix {

Expand Down Expand Up @@ -41,6 +42,9 @@ const nlohmann::json::array_t & getArray(const nlohmann::json & value);
const nlohmann::json::string_t & getString(const nlohmann::json & value);
const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value);
const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value);
StringSet getStringSet(const nlohmann::json & value);
Strings getStringList(const nlohmann::json & value);
StringMap getStringMap(const nlohmann::json & value);

/**
* For `adl_serializer<std::optional<T>>` below, we need to track what
Expand Down

0 comments on commit bddf5c5

Please sign in to comment.