]> git.hungrycats.org Git - bees/commitdiff
config: declare each schema's inheritance edge at registration
authorZygo Blaxell <bees@furryterror.org>
Sat, 29 Aug 2026 18:37:33 +0000 (14:37 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
Which schema a version builds on is real structure, but nothing recorded
it: the only thing relating version 2 to version 1 was that 2 sorts after
1, which is a fact about how the versions are named rather than about how
they relate.  Deriving an order from names also quietly requires every
name to be a number, and to sort correctly, neither of which the config
file's version syntax promises.

Have Register take the version it inherits from, alongside the text.
Adding a schema stays a one-file change, and it is a change that cannot
forget to say where the schema came from.

The edge is stored exactly as written and resolved later.  A Register
runs before main(), and initialization order across translation units is
unspecified, so the version an edge names may not have registered yet.

Two build errors that used to be invisible now abort instead: a version
registered twice, silently dropped by map::insert, and a version
registered under a name that is already an alias, which left that name
meaning two different things depending on the reader.

Nothing reads the edges yet; this only records them.

Assisted-by: Claude-Code:claude-opus-5
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees-config-v1.cc
src/bees-config-v2.cc
src/bees-config.cc
src/bees-config.h

index 285724551eafc9102a54710e2286bfaefb0fdafb..3028ead1778d4edb37aa8dbde2b81c43d280345f 100644 (file)
@@ -487,4 +487,5 @@ static const char bees_config_v1[] = R"--v1-config--(
 
 )--v1-config--";
 
-static BeesConfig::Register s_reg_config_v1("1", bees_config_v1);
+// The oldest schema, so it inherits from nothing.
+static BeesConfig::Register s_reg_config_v1("1", nullptr, bees_config_v1);
index cd5dd9f555d4c1fc3124477cf5777823d7e2c03b..0aa4af1e65827fd716a9089a47268a538fee44dd 100644 (file)
@@ -14,4 +14,4 @@ static const char bees_config_v2[] = R"--v2-config--(
 
 // Schema version 2 is the one under development: new options and changed
 // defaults land here.  bees-config-v1.cc is frozen at what v0.11 shipped.
-static BeesConfig::Register s_reg_config_v2("2", bees_config_v2);
+static BeesConfig::Register s_reg_config_v2("2", "v0.11", bees_config_v2);
index ffe59cb76b17645319c6e0c37170e75b36b26df9..1eaf617d1771796077e0eab46a29abb14be9cfa2 100644 (file)
 #include <sys/statfs.h>
 
 unique_ptr<BeesConfig::DefaultMap> BeesConfig::s_defaults;
+unique_ptr<BeesConfig::PreviousMap> BeesConfig::s_previous;
 
 
-BeesConfig::Register::Register::Register(const char *const version, const char *const text)
+BeesConfig::Register::Register::Register(const char *const version, const char *const previous, const char *const text)
 {
        Innie in;
        in.set_origin(string("<builtin_version_") + version + ">");
@@ -24,7 +25,23 @@ BeesConfig::Register::Register::Register(const char *const version, const char *
        if (!BeesConfig::s_defaults) {
                BeesConfig::s_defaults = make_unique<DefaultMap>();
        }
-       BeesConfig::s_defaults->insert(make_pair(version, in));
+       if (!BeesConfig::s_previous) {
+               BeesConfig::s_previous = make_unique<PreviousMap>();
+       }
+       // config.version accepts versions and aliases from one namespace, so a
+       // schema registered under a name that is already an alias would leave
+       // that name meaning two different things depending on who resolved it.
+       THROW_CHECK1(runtime_error, version, !version_aliases().count(version));
+       // map::insert is a no-op on a key that already exists, so without this
+       // a second registration of the same version would be dropped in silence
+       // and whichever translation unit happened to run first would own the
+       // schema.  Both of these are build errors, and both abort before main()
+       // rather than leave a half-registered version graph behind.
+       const auto rv = BeesConfig::s_defaults->insert(make_pair(version, in));
+       THROW_CHECK1(runtime_error, version, rv.second);
+       if (previous) {
+               BeesConfig::s_previous->insert(make_pair(version, previous));
+       }
        // Only need this when debugging global ctor initialization
        // BEESLOGDEBUG("Registered config defaults " << in.get_origin());
 }
index 6fda8c7062b164efc21772c37ea0e25ee3f799aa..8a6498bb0bcfb73cf343e53a359f2fbdcc792854 100644 (file)
@@ -40,6 +40,11 @@ class BeesConfig {
        /// Map of version string -> built-in default INI text.
        using DefaultMap = map<string, Innie>;
        static unique_ptr<DefaultMap> s_defaults;
+       /// Map of version string -> the version it inherits from, exactly as
+       /// written at registration.  Stored unresolved: a Register runs before
+       /// main() and the version it names may not be registered yet.
+       using PreviousMap = map<string, string>;
+       static unique_ptr<PreviousMap> s_previous;
        /// Newest config schema in this build: the one under development, and
        /// what CURRENT resolves to.
        static constexpr const char *s_current_version = "2";
@@ -124,8 +129,19 @@ public:
         * key already exists, and no error or warning is produced.
         */
        struct Register {
-               /// Register @p text as the default config block for schema @p version.
-               Register(const char *version, const char *text);
+               /**
+                * Register @p text as the built-in defaults for schema @p version,
+                * which inherits every key it does not define from @p previous.
+                * Pass @c nullptr for the oldest schema.
+                *
+                * @p previous may name a concrete version or a frozen alias, and is
+                * resolved on first use rather than here: static initialization
+                * order across translation units is unspecified, so the schema it
+                * names may not have registered yet.  A tracking alias is rejected
+                * when the edge is resolved, since it would not keep meaning the
+                * same schema.
+                */
+               Register(const char *version, const char *previous, const char *text);
        };
 
        /// Merge command-line INI values on top of all other config layers.