« get me outta code hell

yaml: subdoc bindInto - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-03-31 11:49:13 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-04-10 16:02:35 -0300
commit0ee4a82115ae8d41260521ea9080bf0c7d455ab0 (patch)
treed85b39d5aedcaad93d33d7ed753c62c9792f9351 /src/data
parent7fec9da98aadbf5d55cc49c2a8512e802bfbc3e7 (diff)
yaml: subdoc bindInto
Diffstat (limited to 'src/data')
-rw-r--r--src/data/yaml.js26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 3c5c6bb7..6618fd4c 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -157,6 +157,8 @@ function makeProcessDocument(thingConstructor, {
       message: `Errors processing ${constructorPart}` + namePart,
     });
 
+    const thing = Reflect.construct(thingConstructor, []);
+
     const documentEntries = Object.entries(document)
       .filter(([field]) => !ignoredFields.includes(field));
 
@@ -210,15 +212,25 @@ function makeProcessDocument(thingConstructor, {
     const transformUtilities = {
       ...thingConstructors,
 
-      subdoc(documentType, data) {
+      subdoc(documentType, data, {
+        bindInto = null,
+      } = {}) {
         if (!documentType)
           throw new Error(`Expected document type, got ${typeAppearance(documentType)}`);
         if (!data)
           throw new Error(`Expected data, got ${typeAppearance(data)}`);
         if (typeof data !== 'object' || data === null)
-          throw new Error(`Expected data to be an object, got ${data}`);
-
-        return {[subdocSymbol]: {documentType, data}};
+          throw new Error(`Expected data to be an object, got ${typeAppearance(data)}`);
+        if (typeof bindInto !== 'string' && bindInto !== null)
+          throw new Error(`Expected bindInto to be a string, got ${typeAppearance(bindInto)}`);
+
+        return {
+          [subdocSymbol]: {
+            documentType,
+            data,
+            bindInto,
+          },
+        };
       },
     };
 
@@ -279,6 +291,10 @@ function makeProcessDocument(thingConstructor, {
           field, setup, {cause: caughtError}));
       }
 
+      if (setup.bindInto) {
+        subthing[setup.bindInto] = thing;
+      }
+
       if (subthing) {
         fieldValues[field] = subthing;
       }
@@ -289,8 +305,6 @@ function makeProcessDocument(thingConstructor, {
         subdocErrors, thingConstructor));
     }
 
-    const thing = Reflect.construct(thingConstructor, []);
-
     const fieldValueErrors = [];
 
     for (const [field, value] of Object.entries(fieldValues)) {