@@ -1239,16 +1239,14 @@ DerivationOutput DerivationOutput::fromJSON(
12391239 const ExperimentalFeatureSettings & xpSettings)
12401240{
12411241 std::set<std::string_view> keys;
1242- ensureType (_json, nlohmann::detail::value_t ::object);
1243- auto json = (std::map<std::string, nlohmann::json>) _json;
1242+ auto & json = getObject (_json);
12441243
12451244 for (const auto & [key, _] : json)
12461245 keys.insert (key);
12471246
12481247 auto methodAlgo = [&]() -> std::pair<ContentAddressMethod, HashAlgorithm> {
1249- std::string hashAlgoStr = json[" hashAlgo" ];
1250- // remaining to parse, will be mutated by parsers
1251- std::string_view s = hashAlgoStr;
1248+ auto & str = getString (valueAt (json, " hashAlgo" ));
1249+ std::string_view s = str;
12521250 ContentAddressMethod method = ContentAddressMethod::parsePrefix (s);
12531251 if (method == TextIngestionMethod {})
12541252 xpSettings.require (Xp::DynamicDerivations);
@@ -1258,7 +1256,7 @@ DerivationOutput DerivationOutput::fromJSON(
12581256
12591257 if (keys == (std::set<std::string_view> { " path" })) {
12601258 return DerivationOutput::InputAddressed {
1261- .path = store.parseStorePath ((std::string) json[ " path" ] ),
1259+ .path = store.parseStorePath (getString ( valueAt ( json, " path" )) ),
12621260 };
12631261 }
12641262
@@ -1267,10 +1265,10 @@ DerivationOutput DerivationOutput::fromJSON(
12671265 auto dof = DerivationOutput::CAFixed {
12681266 .ca = ContentAddress {
12691267 .method = std::move (method),
1270- .hash = Hash::parseNonSRIUnprefixed ((std::string) json[ " hash" ] , hashAlgo),
1268+ .hash = Hash::parseNonSRIUnprefixed (getString ( valueAt ( json, " hash" )) , hashAlgo),
12711269 },
12721270 };
1273- if (dof.path (store, drvName, outputName) != store.parseStorePath ((std::string) json[ " path" ] ))
1271+ if (dof.path (store, drvName, outputName) != store.parseStorePath (getString ( valueAt ( json, " path" )) ))
12741272 throw Error (" Path doesn't match derivation output" );
12751273 return dof;
12761274 }
@@ -1357,20 +1355,19 @@ nlohmann::json Derivation::toJSON(const StoreDirConfig & store) const
13571355
13581356Derivation Derivation::fromJSON (
13591357 const StoreDirConfig & store,
1360- const nlohmann::json & json ,
1358+ const nlohmann::json & _json ,
13611359 const ExperimentalFeatureSettings & xpSettings)
13621360{
13631361 using nlohmann::detail::value_t ;
13641362
13651363 Derivation res;
13661364
1367- ensureType ( json, value_t ::object );
1365+ auto & json = getObject (_json );
13681366
1369- res.name = ensureType (valueAt (json, " name" ), value_t ::string );
1367+ res.name = getString (valueAt (json, " name" ));
13701368
13711369 try {
1372- auto & outputsObj = ensureType (valueAt (json, " outputs" ), value_t ::object);
1373- for (auto & [outputName, output] : outputsObj.items ()) {
1370+ for (auto & [outputName, output] : getObject (valueAt (json, " outputs" ))) {
13741371 res.outputs .insert_or_assign (
13751372 outputName,
13761373 DerivationOutput::fromJSON (store, res.name , outputName, output));
@@ -1381,8 +1378,7 @@ Derivation Derivation::fromJSON(
13811378 }
13821379
13831380 try {
1384- auto & inputsList = ensureType (valueAt (json, " inputSrcs" ), value_t ::array);
1385- for (auto & input : inputsList)
1381+ for (auto & input : getArray (valueAt (json, " inputSrcs" )))
13861382 res.inputSrcs .insert (store.parseStorePath (static_cast <const std::string &>(input)));
13871383 } catch (Error & e) {
13881384 e.addTrace ({}, " while reading key 'inputSrcs'" );
@@ -1391,29 +1387,28 @@ Derivation Derivation::fromJSON(
13911387
13921388 try {
13931389 std::function<DerivedPathMap<StringSet>::ChildNode (const nlohmann::json &)> doInput;
1394- doInput = [&](const auto & json) {
1390+ doInput = [&](const auto & _json) {
1391+ auto & json = getObject (_json);
13951392 DerivedPathMap<StringSet>::ChildNode node;
1396- node.value = static_cast <const StringSet &>(
1397- ensureType (valueAt (json, " outputs" ), value_t ::array));
1398- for (auto & [outputId, childNode] : ensureType (valueAt (json, " dynamicOutputs" ), value_t ::object).items ()) {
1393+ node.value = getStringSet (valueAt (json, " outputs" ));
1394+ for (auto & [outputId, childNode] : getObject (valueAt (json, " dynamicOutputs" ))) {
13991395 xpSettings.require (Xp::DynamicDerivations);
14001396 node.childMap [outputId] = doInput (childNode);
14011397 }
14021398 return node;
14031399 };
1404- auto & inputDrvsObj = ensureType (valueAt (json, " inputDrvs" ), value_t ::object);
1405- for (auto & [inputDrvPath, inputOutputs] : inputDrvsObj.items ())
1400+ for (auto & [inputDrvPath, inputOutputs] : getObject (valueAt (json, " inputDrvs" )))
14061401 res.inputDrvs .map [store.parseStorePath (inputDrvPath)] =
14071402 doInput (inputOutputs);
14081403 } catch (Error & e) {
14091404 e.addTrace ({}, " while reading key 'inputDrvs'" );
14101405 throw ;
14111406 }
14121407
1413- res.platform = ensureType (valueAt (json, " system" ), value_t ::string );
1414- res.builder = ensureType (valueAt (json, " builder" ), value_t ::string );
1415- res.args = ensureType (valueAt (json, " args" ), value_t ::array );
1416- res.env = ensureType (valueAt (json, " env" ), value_t ::object );
1408+ res.platform = getString (valueAt (json, " system" ));
1409+ res.builder = getString (valueAt (json, " builder" ));
1410+ res.args = getStringList (valueAt (json, " args" ));
1411+ res.env = getStringMap (valueAt (json, " env" ));
14171412
14181413 return res;
14191414}
0 commit comments