Skip to content

Commit

Permalink
Add functionality to selectively suppress key warnings
Browse files Browse the repository at this point in the history
For unknown keys.

{
  "dont_warn_unused_keys": [
    "key1",
    "key3"
  ],
  "key1": 5,
  "key2": {
    "dont_warn_unused_keys": [
      "key1"
    ],
    "key1": 5,
    "key2": 6
  },
  "key3": {
    "dont_warn_unused_keys": [
      "key1"
    ],
    "key1": 5,
    "key2": 6
  }
}

Will yield the warning:

[Series] The following parts of the global JSON config remains unused:
{
  "key2": {
    "dont_warn_unused_keys": [
      "key1"
    ],
    "key1": 5,
    "key2": 6
  },
  "key3": {
    "key2": 6
  }
}

TODO: Better errors when parsing, documentation
  • Loading branch information
franzpoeschel committed Dec 11, 2024
1 parent b8ce8a0 commit 5db738e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/openPMD/auxiliary/JSON_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ namespace json
nlohmann::json *positionInShadow,
SupportedLanguages originallySpecifiedAs,
bool trace);

void init();
static void init(nlohmann::json &original, nlohmann::json &shadow);
};

template <typename Key>
Expand Down
30 changes: 28 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,31 @@ TracingJSON::TracingJSON(
, m_positionInOriginal(positionInOriginal)
, m_positionInShadow(positionInShadow)
, m_trace(trace)
{}
{
init();
}

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

void TracingJSON::init(nlohmann::json &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)
{
init(original[key], shadow[key]);
}
shadow["dont_warn_unused_keys"] = nlohmann::json();
}
}

namespace
{
Expand Down

0 comments on commit 5db738e

Please sign in to comment.