@@ -322,20 +322,38 @@ namespace
322322
323323 void parse_external_mode (
324324 json::TracingJSON mode,
325+ // In read mode, the metadata section stored under 'external_storage'
326+ // These are default values, overridable with the first argument
327+ std::optional<nlohmann::json const *> previousCfg,
325328 std::string const &configLocation,
326329 JSONIOHandlerImpl::DatasetMode_s &res)
327330 {
328331 using SpecificationVia = JSONIOHandlerImpl::SpecificationVia;
329332 using ExternalBlockStorage = openPMD::ExternalBlockStorage;
330333
331- auto get_mandatory = [&]( char const *key,
332- bool lowercase ) -> std::string {
333- if (! mode.json ().contains (key))
334+ auto get_key =
335+ [&]( char const *key ) -> std::optional<nlohmann::json const *> {
336+ if (mode.json ().contains (key))
334337 {
335- throw error::BackendConfigSchema (
336- {configLocation, " mode" , key}, " Mandatory key." );
338+ return {&mode.json ({key})};
339+ }
340+ else if (previousCfg.has_value () && (*previousCfg)->contains (key))
341+ {
342+ return {&(**previousCfg).at (key)};
343+ }
344+ else
345+ {
346+ return std::nullopt ;
337347 }
338- auto const &val = mode.json ({key});
348+ };
349+
350+ auto get_mandatory = [&](char const *key,
351+ bool lowercase) -> std::string {
352+ auto const &val = *optionalOrElse (
353+ get_key (" mode" ), [&]() -> nlohmann::json const * {
354+ throw error::BackendConfigSchema (
355+ {configLocation, " mode" , key}, " Mandatory key." );
356+ });
339357 return optionalOrElse (
340358 lowercase ? openPMD::json::asLowerCaseStringDynamic (val)
341359 : openPMD::json::asStringDynamic (val),
@@ -347,11 +365,12 @@ namespace
347365 };
348366 auto if_contains_optional =
349367 [&](char const *key, bool lowercase, auto &&then) {
350- if (!mode.json ().contains (key))
368+ auto const maybeVal = get_key (key);
369+ if (!maybeVal.has_value ())
351370 {
352371 return ;
353372 }
354- auto const &val = mode. json ({key}) ;
373+ auto const &val = **maybeVal ;
355374 static_cast <decltype (then)>(then)(optionalOrElse (
356375 lowercase ? openPMD::json::asLowerCaseStringDynamic (val)
357376 : openPMD::json::asStringDynamic (val),
@@ -362,11 +381,12 @@ namespace
362381 }));
363382 };
364383 auto if_contains_optional_bool = [&](char const *key, auto &&then) {
365- if (!mode.json ().contains (key))
384+ auto const maybeVal = get_key (key);
385+ if (!maybeVal.has_value ())
366386 {
367387 return ;
368388 }
369- auto const &val = mode. json ({key}) ;
389+ auto const &val = **maybeVal ;
370390 if (!val.is_boolean ())
371391 {
372392 throw error::BackendConfigSchema (
@@ -464,7 +484,8 @@ auto JSONIOHandlerImpl::retrieveDatasetMode(
464484 auto mode = datasetConfig[" mode" ];
465485 if (mode.json ().is_object ())
466486 {
467- parse_external_mode (std::move (mode), configLocation, res);
487+ parse_external_mode (
488+ std::move (mode), std::nullopt , configLocation, res);
468489 }
469490 else
470491 {
0 commit comments