Skip to content

Commit

Permalink
Be aware of TOML in error messages
Browse files Browse the repository at this point in the history
This way we don't confuse users when they use TOML but get a JSON-based
error message.
  • Loading branch information
franzpoeschel committed Nov 19, 2021
1 parent be5c6ed commit b30930f
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 24 deletions.
2 changes: 2 additions & 0 deletions include/openPMD/auxiliary/JSON_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ namespace json
std::shared_ptr< nlohmann::json > shadow,
nlohmann::json * positionInOriginal,
nlohmann::json * positionInShadow,
SupportedLanguages originallySpecifiedAs,
bool trace );
};

Expand All @@ -175,6 +176,7 @@ namespace json
m_shadow,
newPositionInOriginal,
newPositionInShadow,
originallySpecifiedAs,
traceFurther );
}

Expand Down
2 changes: 1 addition & 1 deletion src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace error
BackendConfigSchema::BackendConfigSchema(
std::vector< std::string > errorLocation_in, std::string what )
: Error(
"Wrong JSON schema at index '" +
"Wrong JSON/TOML schema at index '" +
concatVector( errorLocation_in ) + "': " + std::move( what ) )
, errorLocation( std::move( errorLocation_in ) )
{
Expand Down
19 changes: 16 additions & 3 deletions src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2365,9 +2365,22 @@ namespace detail
auto shadow = impl.m_config.invertShadow();
if( shadow.size() > 0 )
{
std::cerr << "Warning: parts of the backend configuration for "
"ADIOS2 remain unused:\n"
<< shadow << std::endl;
switch( impl.m_config.originallySpecifiedAs )
{
case json::SupportedLanguages::JSON:
std::cerr << "Warning: parts of the backend configuration for "
"ADIOS2 remain unused:\n"
<< shadow << std::endl;
break;
case json::SupportedLanguages::TOML:
{
auto asToml = json::jsonToToml( shadow );
std::cerr << "Warning: parts of the backend configuration for "
"ADIOS2 remain unused:\n"
<< asToml << std::endl;
break;
}
}
}
auto notYetConfigured =
[ &alreadyConfigured ]( std::string const & param ) {
Expand Down
13 changes: 5 additions & 8 deletions src/IO/ADIOS/CommonADIOS1IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,11 @@ CommonADIOS1IOHandlerImpl< ChildClass >::createDataset(Writable* writable,
transform = maybeTransform.get();
}

auto shadow = options.invertShadow();
if( shadow.size() > 0 )
{
std::cerr << "Warning: parts of the JSON configuration for "
"ADIOS1 dataset '"
<< name << "' remain unused:\n"
<< shadow << std::endl;
}
parameters.warnUnusedParameters(
options,
"ADIOS1",
"Warning: parts of the backend configuration for "
"ADIOS1 dataset '" + name + "' remain unused:\n" );
}
// Fallback: global option
if( transform.empty() )
Expand Down
21 changes: 17 additions & 4 deletions src/IO/HDF5/HDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,22 @@ HDF5IOHandlerImpl::HDF5IOHandlerImpl(
auto shadow = m_config.invertShadow();
if( shadow.size() > 0 )
{
std::cerr << "Warning: parts of the JSON configuration for "
"HDF5 remain unused:\n"
<< shadow << std::endl;
switch( m_config.originallySpecifiedAs )
{
case json::SupportedLanguages::JSON:
std::cerr << "Warning: parts of the backend configuration for "
"HDF5 remain unused:\n"
<< shadow << std::endl;
break;
case json::SupportedLanguages::TOML:
{
auto asToml = json::jsonToToml( shadow );
std::cerr << "Warning: parts of the backend configuration for "
"HDF5 remain unused:\n"
<< asToml << std::endl;
break;
}
}
}
}

Expand Down Expand Up @@ -330,7 +343,7 @@ HDF5IOHandlerImpl::createDataset(Writable* writable,
parameters.warnUnusedParameters(
config,
"hdf5",
"Warning: parts of the JSON configuration for HDF5 dataset '" +
"Warning: parts of the backend configuration for HDF5 dataset '" +
name + "' remain unused:\n" );

hid_t gapl = H5Pcreate(H5P_GROUP_ACCESS);
Expand Down
13 changes: 12 additions & 1 deletion src/IO/IOTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ void Parameter< Operation::CREATE_DATASET >::warnUnusedParameters<
}
if( shadow.size() > 0 )
{
std::cerr << warningMessage << shadow.dump() << std::endl;
switch( config.originallySpecifiedAs )
{
case json::SupportedLanguages::JSON:
std::cerr << warningMessage << shadow.dump() << std::endl;
break;
case json::SupportedLanguages::TOML:
{
auto asToml = json::jsonToToml( shadow );
std::cerr << warningMessage << asToml << std::endl;
break;
}
}
}
}
} // openPMD
88 changes: 81 additions & 7 deletions src/auxiliary/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ namespace json
TracingJSON::TracingJSON(
nlohmann::json originalJSON,
SupportedLanguages originallySpecifiedAs_in )
: m_originalJSON(
: originallySpecifiedAs( originallySpecifiedAs_in )
, m_originalJSON(
std::make_shared< nlohmann::json >( std::move( originalJSON ) ) )
, m_shadow( std::make_shared< nlohmann::json >() )
, m_positionInOriginal( &*m_originalJSON )
, m_positionInShadow( &*m_shadow )
, originallySpecifiedAs( originallySpecifiedAs_in )
{
}

Expand Down Expand Up @@ -122,8 +122,10 @@ namespace json
std::shared_ptr< nlohmann::json > shadow,
nlohmann::json * positionInOriginal,
nlohmann::json * positionInShadow,
SupportedLanguages originallySpecifiedAs_in,
bool trace )
: m_originalJSON( std::move( originalJSON ) ),
: originallySpecifiedAs( originallySpecifiedAs_in ),
m_originalJSON( std::move( originalJSON ) ),
m_shadow( std::move( shadow ) ),
m_positionInOriginal( positionInOriginal ),
m_positionInShadow( positionInShadow ),
Expand Down Expand Up @@ -213,6 +215,58 @@ namespace json
"Unexpected datatype in TOML configuration. This is probably a "
"bug." );
}

toml::value jsonToToml(
nlohmann::json const & val, std::vector< std::string > & currentPath );

toml::value jsonToToml(
nlohmann::json const & val, std::vector< std::string > & currentPath )
{
switch( val.type() )
{
case nlohmann::json::value_t::null:
return toml::value();
case nlohmann::json::value_t::object: {
toml::value::table_type res;
for( auto pair = val.begin(); pair != val.end(); ++pair )
{
currentPath.push_back( pair.key() );
res[ pair.key() ] = jsonToToml( pair.value(), currentPath );
currentPath.pop_back();
}
return toml::value( std::move( res ) );
}
case nlohmann::json::value_t::array: {
toml::value::array_type res;
res.reserve( val.size() );
size_t index = 0;
for( auto const & entry : val )
{
currentPath.push_back( std::to_string( index ) );
res.emplace_back( jsonToToml( entry, currentPath ) );
currentPath.pop_back();
}
return toml::value( std::move( res ) );
}
case nlohmann::json::value_t::string:
return val.get< std::string >();
case nlohmann::json::value_t::boolean:
return val.get< bool >();
case nlohmann::json::value_t::number_integer:
return val.get< nlohmann::json::number_integer_t >();
case nlohmann::json::value_t::number_unsigned:
return val.get< nlohmann::json::number_unsigned_t >();
case nlohmann::json::value_t::number_float:
return val.get< nlohmann::json::number_float_t >();
case nlohmann::json::value_t::binary:
return val.get< nlohmann::json::binary_t >();
case nlohmann::json::value_t::discarded:
throw error::BackendConfigSchema(
currentPath,
"Internal JSON parser datatype leaked into JSON value." );
}
throw std::runtime_error( "Unreachable!" );
}
}

nlohmann::json tomlToJson( toml::value const & val )
Expand All @@ -223,6 +277,14 @@ namespace json
return tomlToJson( val, currentPath );
}

toml::value jsonToToml( nlohmann::json const & val )
{
std::vector< std::string > currentPath;
// that's as deep as our config currently goes, +1 for good measure
currentPath.reserve( 7 );
return jsonToToml( val, currentPath );
}

namespace
{
ParsedConfig parseInlineOptions( std::string const & options )
Expand Down Expand Up @@ -466,10 +528,22 @@ namespace json
}
if( shadow.size() > 0 )
{
std::cerr
<< "[Series] The following parts of the global JSON config "
"remains unused:\n"
<< shadow.dump() << std::endl;
switch( config.originallySpecifiedAs )
{
case SupportedLanguages::JSON:
std::cerr
<< "[Series] The following parts of the global JSON config "
"remains unused:\n"
<< shadow.dump() << std::endl;
break;
case SupportedLanguages::TOML: {
auto asToml = jsonToToml( shadow );
std::cerr
<< "[Series] The following parts of the global TOML config "
"remains unused:\n"
<< asToml << std::endl;
}
}
}
}

Expand Down

0 comments on commit b30930f

Please sign in to comment.