Skip to content

Add atomic overlay read_into / load_into deserialization for layered configs - #81

Closed
clockblocker wants to merge 5 commits into
mainfrom
read-into-overlay
Closed

clockblocker wants to merge 5 commits into
mainfrom
read-into-overlay

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Adds atomic overlay deserialization (rfl::json::read_into, rfl::json::load_into, rfl::yaml::read_into, rfl::yaml::load_into, rfl::from_generic_into, rfl::generic::read_into) for layered configuration files (workspace -> user -> local) without double-parsing through rfl::Generic.

Semantics & Design

  • Takes T _base by value and returns rfl::Result<T> so overlay calls are atomic on error (caller's original base is untouched on syntax/type/validation/NoExtraFields failure), compose naturally via .and_then(...), and support move-only structs (std::unique_ptr).
  • Missing struct fields preserve values from _base; present fields overwrite _base, recursing into nested structs (and existing std::optional<NestedStruct> / smart-pointer targets) while clearing std::optional to std::nullopt on explicit null.
  • Preserves rfl::Validator, rfl::NoExtraFields, naming processors (SnakeCaseToCamelCase, Rename), rfl::Skip, and rfl::ExtraFields semantics.
  • Added comprehensive JSON, YAML, and Generic tests (tests/{json,yaml,generic}/test_read_into.cpp) and documentation in docs/content/{generic,supported_formats/json,supported_formats/yaml}.md.

Verification

  • Full ctest suite (json, yaml, generic, cli, env, json_c_arrays_and_inheritance) under -Wall -Werror: 100% PASSED.

@clockblocker

Copy link
Copy Markdown
Owner Author

Review Scope Recommendation (Wayfinder Clerk sync):

  • Include rfl::toml::read_into / rfl::toml::load_into alongside json, yaml, and generic: TOML is a primary config-layering format, requires only ~65 lines mirroring yaml/read.hpp + yaml/load.hpp (preserving #if TOML_EXCEPTIONS), and naturally preserves _base on empty/comment-only .toml files (toml::parse("") returns an empty table).
  • Document TOML quirk in docs/content/supported_formats/toml.md: TOML has no null literal, so std::optional<T> fields cannot be explicitly cleared to std::nullopt via overlay (only preserved when absent or updated when present).
  • Keep scope bounded: Skip binary/wire/schemaful formats in #81, and defer rfl::env::read_into / rfl::cli::read_into to a separate follow-up PR (one problem per PR). Keep the comprehensive edge-case test matrix in tests/json/test_read_into.cpp and keep yaml/toml/generic tests lean.

@clockblocker

Copy link
Copy Markdown
Owner Author

Follow-up Review Design Decisions (read-into-overlay, Wayfinder Clerk sync):

  • (b) std::map semantics — keep replace-by-default (do not merge keys by default):

    • Merging std::map keys by default leaves later overlay layers with no way to remove or reset inherited entries (null is a type error for int and collides with std::nullopt for std::map<std::string, std::optional<T>>).
    • Document the clean boundary explicitly: fixed-schema objects (struct, NamedTuple, Flatten, and ExtraFields) merge field-by-field; dynamic containers (std::vector, std::map, std::unordered_map, rfl::Object) replace wholesale when present (omit to preserve, {}/[] to clear).
    • If map-key merging is ever wanted later, add it as an opt-in processor (e.g., rfl::MergeMaps) per Guide Add rfl::CodePoints validator for UTF-8 string length #7.
  • (c) const fields & const_cast handling (relative to upstream #787 73710de4):

    • Upstream main already merged #787 (73710de4), which uses const_cast + std::destroy_at + placement ::new in ViewReaderWithDefault::move_to.
    • Recommended minimal fix (consistent with #787): widen move_to's in-place branch to std::is_const_v<Target> || !std::is_move_assignable_v<Target> (fixes compile failure on nested structs containing a const member such as struct Outer { Inner inner; }; where Inner has const int id;), wrap T(*rfl::get<i>(*_view)) copy in try/catch so throwing copy ctors return rfl::Error, replace the silent overlay→replace fallback on non-copyable const fields with a static_assert (Guide Accept repeated list options and remaining positional arguments in rfl::cli #6), and add tests for top-level and nested const fields.
    • Strictly UB-free alternative (zero [basic.life]/8.3 const_cast risk): keep the fast in-place _base path for normal move-assignable structs, and fall back to a read_struct-style alignas(T) unsigned char buf[sizeof(T)] placement-new reconstruction whenever a struct contains const or non-move-assignable fields (~40 lines).

…eric fast-path, tests) and add rfl::toml::read_into
@clockblocker

Copy link
Copy Markdown
Owner Author

Active Harness Validation Note (clockblocker/slang-server#1, bf857f4):

  • slang-server skipped #81 (read_into) for src/Config.cpp due to two layered-config requirements:
    1. Container merge policies (src/Config.cpp:91-109): appending arrays and merging env maps (validates keeping replace-by-default in #81 and planning opt-in rfl::MergeMaps / rfl::AppendArrays processors in a follow-up).
    2. Field-presence / explicit-null tracking (src/Config.cpp:130-163, buildPatternExplicit / flagsByFile), now folded into Lane 4 #102.

@clockblocker

Copy link
Copy Markdown
Owner Author

Parked into #116 (Easy & Landable) — branch preserved on fork. Real-world layered config adoption in slang-server#1 needed array-append, map-merge, and explicit-vs-null presence tracking rather than blind container replacement, so shrinking the active Phase 2 upstream queue from 18 to 17 PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant