Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON: Selectively suppress unknown key warnings #1700

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/source/details/backendconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,42 @@ For JSON and ADIOS2, all datasets are resizable, independent of this option.

The key ``rank_table`` allows specifying the creation of a **rank table**, used for tracking :ref:`chunk provenance especially in streaming setups <rank_table>`, refer to the streaming documentation for details.

A warning is printed if the JSON/TOML configuration contains keys that are not understood (and hence ignored) by the openPMD-api.
Such warnings can be suppressed by listing key names under ``"dont_warn_unused_keys"``.

Example:

.. code-block:: json

{
"adio2": {
"dataset": {
"put dataset options": "here"
}
},
"hdf5": {
"an unknown": "key",
"another unknown": "key",
"a third unknown": "key",
"dont_warn_unused_keys": ["an unknown", "another unknown"]
}
}

The openPMD-api will warn about the unknown key ``"adio2"`` (in order to give feedback about the apparent misspelling of ``"adios2"`` to users); there will similarly be a warning for the unknown HDF5 key ``"a third unknown"``. Warnings about the other two unknown keys have been suppressed. The printed warning will hence be about these unknown parts of the JSON configuration:

.. code-block:: json

{
"adio2": {
"dataset": {
"put dataset options": "here"
}
},
"hdf5": {
"a third unknown": "key"
}
}

Configuration Structure per Backend
-----------------------------------

Expand Down
9 changes: 9 additions & 0 deletions include/openPMD/auxiliary/JSON_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ namespace json
nlohmann::json *positionInShadow,
SupportedLanguages originallySpecifiedAs,
bool trace);

void init();
/*
* Called upon each traced access of a location in the JSON value, along
* with the matching subtree of the shadow.
* This implements the `dont_warn_unused_keys` functionality.
*/
static void
init(nlohmann::json const &original, nlohmann::json &shadow);
};

template <typename Key>
Expand Down
38 changes: 36 additions & 2 deletions src/auxiliary/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ TracingJSON::TracingJSON(
, m_shadow(std::make_shared<nlohmann::json>())
, m_positionInOriginal(&*m_originalJSON)
, m_positionInShadow(&*m_shadow)
{}
{
init();
}

TracingJSON::TracingJSON(ParsedConfig parsedConfig)
: TracingJSON{
Expand Down Expand Up @@ -158,7 +160,39 @@ TracingJSON::TracingJSON(
, m_positionInOriginal(positionInOriginal)
, m_positionInShadow(positionInShadow)
, m_trace(trace)
{}
{
init();
}

void TracingJSON::init()
{
if (m_originalJSON)
{
init(*m_positionInOriginal, *m_positionInShadow);
}
}

void TracingJSON::init(nlohmann::json const &original, nlohmann::json &shadow)
{
if (original.is_object() && original.contains("dont_warn_unused_keys"))
{
auto suppress_warnings_for_these = original.at("dont_warn_unused_keys")
.get<std::vector<std::string>>();
for (auto const &key : suppress_warnings_for_these)
{
// For each suppressed key, we now emulate an access to that key.
// This entails calling init() recursively since that function is
// called upon each access to a traced key.
auto it = original.find(key);
if (it == original.end())
{
continue;
}
init(*it, shadow[key]);
}
shadow["dont_warn_unused_keys"] = nlohmann::json();
}
}

namespace
{
Expand Down
Loading