config_defaults.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  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. //
  22. // Write-time materialisation is permanent: once a workflow is saved with a
  23. // default baked into its stored config, that key now counts as "present",
  24. // so a later change to the schema's default will never reach that already-
  25. // saved workflow - the read path correctly leaves a present key alone. This
  26. // is an accepted consequence of applying defaults at write time, not a bug;
  27. // it only affects workflows saved after write-time materialisation shipped,
  28. // since a workflow saved before that never had the key baked in to begin
  29. // with, and still picks up schema default changes at read time.
  30. nlohmann::json applyConfigDefaults(const nlohmann::json& config,
  31. const nlohmann::json& config_schema);
  32. } // namespace smartbotic::common