Sfoglia il codice sorgente

Adding metadata generation utilities

Richard Knight 6 anni fa
parent
commit
1d1b90ec92
2 ha cambiato i file con 69 aggiunte e 2 eliminazioni
  1. 32 0
      _notes/data-transformations.txt
  2. 37 2
      src/app/dynaform/libs/index.ts

+ 32 - 0
_notes/data-transformations.txt

@@ -0,0 +1,32 @@
+Raw model ...
+
+{
+	a: "Value 1",
+	b: "Value 2",
+	c: "Value 3"
+}
+
+Goes to ...
+
+{
+	a: { name: "a", value: "Value 1" },
+	b: { name: "b", value: "Value 2" },
+	c: { name: "c", value: "Value 3" }
+}
+
+Merges with lazy metadata ...
+
+{
+  b: { type: "checkbutton" },
+  c: { label: "Property Three" }
+
+}
+
+Giving ...
+
+{
+	a: { name: "a", value: "Value 1" },
+	b: { name: "b", value: "Value 2", type: "checkbutton" },
+	c: { name: "c", value: "Value 3", label: "Property Three" }
+}
+

+ 37 - 2
src/app/dynaform/libs/index.ts

@@ -42,14 +42,49 @@ const addMissingNames = metaG => Object.entries(metaG)
 	.map(addNameToSelfAndChildren)
 	.reduce((res, [key, val]) => addProp(res, key, val), {});
 
+// ---------------------------------------------------------------------------------------------------------------------
+// Raw Model (or Mapped Raw Model) to Automatic Metadata
+
+const isScalar = val => typeof val === 'boolean' || typeof val === 'number' || typeof val === 'string';
+
+const keyValPairToMeta = (val, key) => ({ name: key, [isScalar(val) ? 'value' : 'meta']: val });
+const keyValPairToMetaRecursive = ( [key, val] ) => {
+	const innerVal = isScalar(val) ? val : rawModelAutoMeta(val);
+	const metaVal = keyValPairToMeta(innerVal, key);
+	return [key, metaVal];
+};
+const rawModelAutoMeta = model => Object.entries(model)
+	.map(keyValPairToMetaRecursive)
+	.reduce((res, [key, val]) => addProp(res, key, val), {});
+
+
+// If value is complex, should we make name & meta, not name & value,
+// as form fields can't generally express complex values
+
+// ---------------------------------------------------------------------------------------------------------------------
+// Combine automatically generated metadata with overrides
+
+const combineMetaForField = (metaF, metaFextra) => Object.assign(metaF, metaFextra);
+const combineExtraMeta = (metaG, metaExtra) => {
+	let combinedMeta = {};
+	Object.entries(metaExtra).forEach(([key, val]) => {
+		if (typeof metaG[key] === 'object') {
+			combinedMeta[key] = val.meta ?
+				combineExtraMeta(metaG[key].meta, val.meta) :
+				combineMetaForField(metaG[key], val);
+		}
+	});
+	return {...metaG, ...combinedMeta};
+};
+
 // ---------------------------------------------------------------------------------------------------------------------
 // Form Metadata Factory
 
 const buildModeledField = metaF => {
 	// TODO: Add validity check
-	const modeledMetaF= new fmdModels[metaF.type](metaF);
+	const modeledMetaF = new fmdModels[metaF.type](metaF);
 	return modeledMetaF;
-}
+};
 
 // Build Form Group Member
 const buildModeledGroupMember = metaFoG => isGroup(metaFoG) ? _buildModeledGroup(metaFoG.meta) : buildModeledField(metaFoG);