« get me outta code hell

yaml: initialize wiki data arrays with empty arrays and nulls - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-12-12 16:28:20 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-12-12 16:28:20 -0400
commit80d27a412d8e182015842b67253b51c1005f1c30 (patch)
treeae5a4172c076d613ec694849b6344bc0866f5bbc /src
parent24322e79ef3659decc634c036624ace739d2a469 (diff)
yaml: initialize wiki data arrays with empty arrays and nulls
Diffstat (limited to 'src')
-rw-r--r--src/data/yaml.js52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 4ba766c4..fbb4e5d6 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -44,28 +44,38 @@ function inspect(value, opts = {}) {
   return nodeInspect(value, {colors: ENABLE_COLOR, ...opts});
 }
 
-function pushWikiData(a, b) {
-  for (const key of Object.keys(b)) {
-    if (Object.hasOwn(a, key)) {
-      if (Array.isArray(a[key])) {
-        if (Array.isArray(b[key])) {
-          a[key].push(...b[key]);
-        } else {
-          throw new Error(`${key} already present, expected array of items to push`);
-        }
+function makeEmptyWikiData() {
+  const wikiData = {};
+
+  for (const thingConstructor of Object.values(thingConstructors)) {
+    if (thingConstructor[Thing.wikiData]) {
+      if (thingConstructor[Thing.oneInstancePerWiki]) {
+        wikiData[thingConstructor[Thing.wikiData]] = null;
       } else {
-        if (Array.isArray(a[key])) {
-          throw new Error(`${key} already present and not an array, refusing to overwrite`);
-        } else {
-          throw new Error(`${key} already present, refusing to overwrite`);
-        }
+        wikiData[thingConstructor[Thing.wikiData]] = [];
       }
-    } else {
+    }
+  }
+
+  return wikiData;
+}
+
+function pushWikiData(a, b) {
+  for (const key of Object.keys(b)) {
+    if (!Object.hasOwn(a, key)) {
+      throw new Error(`${key} not present`);
+    }
+
+    if (Array.isArray(a[key])) {
       if (Array.isArray(b[key])) {
-        a[key] = [...b[key]];
+        a[key].push(...b[key]);
       } else {
-        a[key] = b[key];
+        throw new Error(`${key} is an array, expected array of items to push`);
       }
+    } else if (a[key] === null) {
+      a[key] = b[key];
+    } else if (b[key] !== null) {
+      throw new Error(`${key} already has a value: ${inspect(a[key])}`);
     }
   }
 }
@@ -187,7 +197,7 @@ function makeProcessDocument(thingConstructor, {
 
     const thing = Reflect.construct(thingConstructor, []);
 
-    const wikiData = {};
+    const wikiData = makeEmptyWikiData();
     const flat = [thing];
     if (thingConstructor[Thing.wikiData]) {
       if (thingConstructor[Thing.oneInstancePerWiki]) {
@@ -1357,7 +1367,7 @@ export function processThingsFromDataStep(documents, dataStep) {
     case documentModes.allTogether: {
       const things = [];
       const flat = [];
-      const wikiData = {};
+      const wikiData = makeEmptyWikiData();
       const aggregate = openAggregate({message: `Errors processing documents`});
 
       documents.forEach(
@@ -1417,7 +1427,7 @@ export function processThingsFromDataStep(documents, dataStep) {
         throw new Error(`Missing header document (empty file or erroneously starting with "---"?)`);
 
       const aggregate = openAggregate({message: `Errors processing documents`});
-      const wikiData = {};
+      const wikiData = makeEmptyWikiData();
 
       const {result: headerResult, aggregate: headerAggregate} =
         processDocument(headerDocument, dataStep.headerDocumentThing);
@@ -1688,7 +1698,7 @@ export function connectThingsFromDataSteps(processThingResultLists, dataSteps) {
 }
 
 export function makeWikiDataFromDataSteps(processThingResultLists, _dataSteps) {
-  const wikiData = {};
+  const wikiData = makeEmptyWikiData();
 
   for (const result of processThingResultLists.flat(2)) {
     pushWikiData(wikiData, result.wikiData);