config_defaults.hpp 1.1 KB

123456789101112131415161718192021222324252627
  1. #pragma once
  2. #include <nlohmann/json.hpp>
  3. namespace smartbotic::common {
  4. // Applies configSchema-declared defaults to a node config.
  5. //
  6. // Returns a copy of `config` with any property from
  7. // `config_schema["properties"]` that declares a "default" filled in, but
  8. // only when `config` does not already contain that key. A key that is
  9. // present - even with a falsy value like false, 0, null, or "" - is a
  10. // deliberate, stored value and is never overwritten.
  11. //
  12. // Scope limit (by design, not an oversight): only top-level properties are
  13. // considered. Defaults nested inside object properties or array item
  14. // schemas are NOT applied. Nodes are not currently written with nested
  15. // config shapes that rely on defaults, so recursing was left out to keep
  16. // this function's behaviour easy to reason about; revisit if that changes.
  17. //
  18. // Tolerant of a missing/null/non-object schema, or one with no
  19. // "properties" - in all of those cases the config is returned unchanged
  20. // rather than throwing.
  21. nlohmann::json applyConfigDefaults(const nlohmann::json& config,
  22. const nlohmann::json& config_schema);
  23. } // namespace smartbotic::common