From 5db738ea6b6eafb5f0f8839123ddcb08b4f27bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 25 Nov 2024 11:32:05 +0100 Subject: [PATCH] Add functionality to selectively suppress key warnings 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 --- include/openPMD/auxiliary/JSON_internal.hpp | 3 +++ src/auxiliary/JSON.cpp | 30 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/include/openPMD/auxiliary/JSON_internal.hpp b/include/openPMD/auxiliary/JSON_internal.hpp index f419842b0b..04123087cb 100644 --- a/include/openPMD/auxiliary/JSON_internal.hpp +++ b/include/openPMD/auxiliary/JSON_internal.hpp @@ -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 diff --git a/src/auxiliary/JSON.cpp b/src/auxiliary/JSON.cpp index 8b758fe70e..eab70eb647 100644 --- a/src/auxiliary/JSON.cpp +++ b/src/auxiliary/JSON.cpp @@ -53,7 +53,9 @@ TracingJSON::TracingJSON( , m_shadow(std::make_shared()) , m_positionInOriginal(&*m_originalJSON) , m_positionInShadow(&*m_shadow) -{} +{ + init(); +} TracingJSON::TracingJSON(ParsedConfig parsedConfig) : TracingJSON{ @@ -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>(); + for (auto const &key : suppress_warnings_for_these) + { + init(original[key], shadow[key]); + } + shadow["dont_warn_unused_keys"] = nlohmann::json(); + } +} namespace {