manyfest 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @license MIT
3
+ * @author <steven@velozo.com>
4
+ */
5
+ let libSimpleLog = require('./Manyfest-LogToConsole.js');
6
+
7
+ /**
8
+ * Schema Manipulation Functions
9
+ *
10
+ * @class ManyfestSchemaManipulation
11
+ */
12
+ class ManyfestSchemaManipulation
13
+ {
14
+ constructor(pInfoLog, pErrorLog)
15
+ {
16
+ // Wire in logging
17
+ this.logInfo = (typeof(pInfoLog) === 'function') ? pInfoLog : libSimpleLog;
18
+ this.logError = (typeof(pErrorLog) === 'function') ? pErrorLog : libSimpleLog;
19
+ }
20
+
21
+ // This translates the default address mappings to something different.
22
+ //
23
+ // For instance you can pass in manyfest schema descriptor object:
24
+ // {
25
+ // "Address.Of.a": { "Hash": "a", "Type": "Number" },
26
+ // "Address.Of.b": { "Hash": "b", "Type": "Number" }
27
+ // }
28
+ //
29
+ //
30
+ // And then an address mapping (basically a Hash->Address map)
31
+ // {
32
+ // "a": "New.Address.Of.a",
33
+ // "b": "New.Address.Of.b"
34
+ // }
35
+ //
36
+ // NOTE: This mutates the schema object permanently, altering the base hash.
37
+ // If there is a collision with an existing address, it can lead to overwrites.
38
+ // TODO: Discuss what should happen on collisions.
39
+ resolveAddressMappings(pManyfestSchemaDescriptors, pAddressMapping)
40
+ {
41
+ if (typeof(pManyfestSchemaDescriptors) != 'object')
42
+ {
43
+ this.logError(`Attempted to resolve address mapping but the descriptor was not an object.`);
44
+ return false;
45
+ }
46
+
47
+ if (typeof(pAddressMapping) != 'object')
48
+ {
49
+ // No mappings were passed in
50
+ return true;
51
+ }
52
+
53
+ // Get the arrays of both the schema definition and the hash mapping
54
+ let tmpManyfestAddresses = Object.keys(pManyfestSchemaDescriptors);
55
+ let tmpHashMapping = {};
56
+ tmpManyfestAddresses.forEach(
57
+ (pAddress) =>
58
+ {
59
+ if (pManyfestSchemaDescriptors[pAddress].hasOwnProperty('Hash'))
60
+ {
61
+ tmpHashMapping[pManyfestSchemaDescriptors[pAddress].Hash] = pAddress;
62
+ }
63
+ });
64
+
65
+ let tmpAddressMappingSet = Object.keys(pAddressMapping);
66
+
67
+ tmpAddressMappingSet.forEach(
68
+ (pInputAddress) =>
69
+ {
70
+ let tmpNewDescriptorAddress = pAddressMapping[pInputAddress];
71
+ let tmpOldDescriptorAddress = false;
72
+ let tmpDescriptor = false;
73
+
74
+ // See if there is a matching descriptor either by Address directly or Hash
75
+ if (pManyfestSchemaDescriptors.hasOwnProperty(pInputAddress))
76
+ {
77
+ tmpOldDescriptorAddress = pInputAddress;
78
+ }
79
+ else if (tmpHashMapping.hasOwnProperty(pInputAddress))
80
+ {
81
+ tmpOldDescriptorAddress = tmpHashMapping[pInputAddress];
82
+ }
83
+
84
+ // If there was a matching descriptor in the manifest, store it in the temporary descriptor
85
+ if (tmpOldDescriptorAddress)
86
+ {
87
+ tmpDescriptor = pManyfestSchemaDescriptors[tmpOldDescriptorAddress];
88
+ delete pManyfestSchemaDescriptors[tmpOldDescriptorAddress];
89
+ }
90
+ else
91
+ {
92
+ // Create a new descriptor! Map it to the input address.
93
+ tmpDescriptor = { Hash:pInputAddress };
94
+ }
95
+
96
+ // Now re-add the descriptor to the manyfest schema
97
+ pManyfestSchemaDescriptors[tmpNewDescriptorAddress] = tmpDescriptor;
98
+ });
99
+
100
+ return true;
101
+ }
102
+
103
+ safeResolveAddressMappings(pManyfestSchemaDescriptors, pAddressMapping)
104
+ {
105
+ // This returns the descriptors as a new object, safely remapping without mutating the original schema Descriptors
106
+ let tmpManyfestSchemaDescriptors = JSON.parse(JSON.stringify(pManyfestSchemaDescriptors));
107
+ this.resolveAddressMappings(tmpManyfestSchemaDescriptors, pAddressMapping);
108
+ return tmpManyfestSchemaDescriptors;
109
+ }
110
+ }
111
+
112
+ module.exports = ManyfestSchemaManipulation;