@fgv/ts-res 5.0.0-21 → 5.0.0-23

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.
Files changed (197) hide show
  1. package/README.md +794 -137
  2. package/dist/ts-res.d.ts +506 -33
  3. package/lib/packlets/common/convert.d.ts +10 -0
  4. package/lib/packlets/common/convert.js +11 -1
  5. package/lib/packlets/common/resources.d.ts +40 -1
  6. package/lib/packlets/common/validate/regularExpressions.d.ts +4 -0
  7. package/lib/packlets/common/validate/regularExpressions.js +5 -1
  8. package/lib/packlets/common/validate/resources.d.ts +35 -1
  9. package/lib/packlets/common/validate/resources.js +54 -0
  10. package/lib/packlets/config/convert.js +1 -1
  11. package/lib/packlets/config/json.d.ts +1 -1
  12. package/lib/packlets/qualifier-types/config/convert.d.ts +22 -1
  13. package/lib/packlets/qualifier-types/config/convert.js +67 -2
  14. package/lib/packlets/qualifier-types/config/json.d.ts +11 -2
  15. package/lib/packlets/qualifier-types/helpers.d.ts +1 -2
  16. package/lib/packlets/qualifier-types/languageQualifierType.d.ts +27 -1
  17. package/lib/packlets/qualifier-types/languageQualifierType.js +75 -0
  18. package/lib/packlets/qualifier-types/literalQualifierType.d.ts +30 -4
  19. package/lib/packlets/qualifier-types/literalQualifierType.js +77 -0
  20. package/lib/packlets/qualifier-types/literalValueHierarchy.d.ts +6 -10
  21. package/lib/packlets/qualifier-types/literalValueHierarchy.js +10 -0
  22. package/lib/packlets/qualifier-types/qualifierType.d.ts +28 -0
  23. package/lib/packlets/qualifier-types/territoryQualifierType.d.ts +28 -4
  24. package/lib/packlets/qualifier-types/territoryQualifierType.js +77 -0
  25. package/lib/packlets/resource-json/compiled/convert.js +2 -1
  26. package/lib/packlets/resource-json/compiled/json.d.ts +6 -2
  27. package/lib/packlets/resource-types/config/convert.js +3 -1
  28. package/lib/packlets/resource-types/config/json.d.ts +2 -0
  29. package/lib/packlets/resource-types/jsonResourceType.d.ts +10 -6
  30. package/lib/packlets/resource-types/jsonResourceType.js +7 -10
  31. package/lib/packlets/resource-types/resourceType.d.ts +20 -8
  32. package/lib/packlets/resource-types/resourceType.js +20 -16
  33. package/lib/packlets/resources/candidateValue.d.ts +92 -0
  34. package/lib/packlets/resources/candidateValue.js +128 -0
  35. package/lib/packlets/resources/candidateValueCollector.d.ts +55 -0
  36. package/lib/packlets/resources/candidateValueCollector.js +96 -0
  37. package/lib/packlets/resources/index.d.ts +2 -0
  38. package/lib/packlets/resources/index.js +2 -0
  39. package/lib/packlets/resources/resource.js +2 -2
  40. package/lib/packlets/resources/resourceBuilder.d.ts +6 -0
  41. package/lib/packlets/resources/resourceBuilder.js +3 -1
  42. package/lib/packlets/resources/resourceCandidate.d.ts +10 -1
  43. package/lib/packlets/resources/resourceCandidate.js +11 -5
  44. package/lib/packlets/resources/resourceManagerBuilder.d.ts +7 -0
  45. package/lib/packlets/resources/resourceManagerBuilder.js +13 -6
  46. package/lib/packlets/runtime/compiledResourceCollection.d.ts +20 -8
  47. package/lib/packlets/runtime/compiledResourceCollection.js +25 -4
  48. package/lib/packlets/runtime/resource-tree/common.d.ts +1 -1
  49. package/lib/packlets/runtime/resourceResolver.d.ts +39 -1
  50. package/lib/packlets/runtime/resourceResolver.js +45 -28
  51. package/lib/test/unit/qualifier-types/testQualifierType.d.ts +4 -0
  52. package/lib/test/unit/qualifier-types/testQualifierType.js +24 -0
  53. package/lib/test/unit/resource-types/derivedResourceType.test.d.ts +2 -0
  54. package/lib/test/unit/resource-types/testDerivedResourceType.d.ts +79 -0
  55. package/lib/test/unit/resource-types/testDerivedResourceType.js +113 -0
  56. package/lib/test/unit/resources/candidateValue.test.d.ts +2 -0
  57. package/lib/test/unit/resources/candidateValueCollector.test.d.ts +2 -0
  58. package/lib/test/unit/runtime/resourceResolver.coverage.test.d.ts +2 -0
  59. package/lib/test/unit/runtime/resourceResolver.newFeatures.test.d.ts +2 -0
  60. package/package.json +7 -7
  61. package/src/index.ts +0 -75
  62. package/src/packlets/bundle/bundleBuilder.ts +0 -159
  63. package/src/packlets/bundle/bundleLoader.ts +0 -157
  64. package/src/packlets/bundle/bundleNormalizer.ts +0 -173
  65. package/src/packlets/bundle/bundleUtils.ts +0 -142
  66. package/src/packlets/bundle/convert.ts +0 -87
  67. package/src/packlets/bundle/index.ts +0 -52
  68. package/src/packlets/bundle/model.ts +0 -138
  69. package/src/packlets/common/conditions.ts +0 -226
  70. package/src/packlets/common/convert.ts +0 -184
  71. package/src/packlets/common/helpers/conditions.ts +0 -84
  72. package/src/packlets/common/helpers/context.ts +0 -88
  73. package/src/packlets/common/helpers/index.ts +0 -26
  74. package/src/packlets/common/helpers/qualifierDefaultValues.ts +0 -91
  75. package/src/packlets/common/helpers/resources.ts +0 -96
  76. package/src/packlets/common/index.ts +0 -30
  77. package/src/packlets/common/resources.ts +0 -78
  78. package/src/packlets/common/validate/conditions.ts +0 -595
  79. package/src/packlets/common/validate/index.ts +0 -27
  80. package/src/packlets/common/validate/regularExpressions.ts +0 -92
  81. package/src/packlets/common/validate/resources.ts +0 -170
  82. package/src/packlets/conditions/condition.ts +0 -351
  83. package/src/packlets/conditions/conditionCollector.ts +0 -103
  84. package/src/packlets/conditions/conditionDecls.ts +0 -50
  85. package/src/packlets/conditions/conditionSet.ts +0 -318
  86. package/src/packlets/conditions/conditionSetCollector.ts +0 -127
  87. package/src/packlets/conditions/conditionSetDecls.ts +0 -41
  88. package/src/packlets/conditions/conditionToken.ts +0 -180
  89. package/src/packlets/conditions/convert/conditionSetDecls.ts +0 -72
  90. package/src/packlets/conditions/convert/decls.ts +0 -95
  91. package/src/packlets/conditions/convert/index.ts +0 -24
  92. package/src/packlets/conditions/index.ts +0 -33
  93. package/src/packlets/config/common.ts +0 -107
  94. package/src/packlets/config/configInitFactory.ts +0 -142
  95. package/src/packlets/config/convert.ts +0 -61
  96. package/src/packlets/config/index.ts +0 -30
  97. package/src/packlets/config/json.ts +0 -43
  98. package/src/packlets/config/predefined/default.ts +0 -150
  99. package/src/packlets/config/predefined/extended.ts +0 -198
  100. package/src/packlets/config/predefined/index.ts +0 -26
  101. package/src/packlets/config/systemConfiguration.ts +0 -217
  102. package/src/packlets/context/contextDecls.ts +0 -76
  103. package/src/packlets/context/contextToken.ts +0 -252
  104. package/src/packlets/context/convert/decls.ts +0 -126
  105. package/src/packlets/context/convert/index.ts +0 -23
  106. package/src/packlets/context/index.ts +0 -27
  107. package/src/packlets/decisions/abstractDecision.ts +0 -87
  108. package/src/packlets/decisions/abstractDecisionCollector.ts +0 -122
  109. package/src/packlets/decisions/candidate.ts +0 -101
  110. package/src/packlets/decisions/common.ts +0 -37
  111. package/src/packlets/decisions/concreteDecision.ts +0 -157
  112. package/src/packlets/decisions/decision.ts +0 -172
  113. package/src/packlets/decisions/index.ts +0 -28
  114. package/src/packlets/import/fsItem.ts +0 -212
  115. package/src/packlets/import/importContext.ts +0 -183
  116. package/src/packlets/import/importManager.ts +0 -187
  117. package/src/packlets/import/importable.ts +0 -107
  118. package/src/packlets/import/importers/collectionImporter.ts +0 -153
  119. package/src/packlets/import/importers/fsItemImporter.ts +0 -154
  120. package/src/packlets/import/importers/importer.ts +0 -58
  121. package/src/packlets/import/importers/index.ts +0 -27
  122. package/src/packlets/import/importers/jsonImporter.ts +0 -133
  123. package/src/packlets/import/importers/pathImporter.ts +0 -134
  124. package/src/packlets/import/index.ts +0 -29
  125. package/src/packlets/qualifier-types/config/convert.ts +0 -108
  126. package/src/packlets/qualifier-types/config/index.ts +0 -27
  127. package/src/packlets/qualifier-types/config/json.ts +0 -126
  128. package/src/packlets/qualifier-types/convert.ts +0 -77
  129. package/src/packlets/qualifier-types/helpers.ts +0 -94
  130. package/src/packlets/qualifier-types/index.ts +0 -34
  131. package/src/packlets/qualifier-types/languageQualifierType.ts +0 -143
  132. package/src/packlets/qualifier-types/literalQualifierType.ts +0 -244
  133. package/src/packlets/qualifier-types/literalValueHierarchy.ts +0 -332
  134. package/src/packlets/qualifier-types/qualifierType.ts +0 -360
  135. package/src/packlets/qualifier-types/qualifierTypeCollector.ts +0 -102
  136. package/src/packlets/qualifier-types/territoryQualifierType.ts +0 -255
  137. package/src/packlets/qualifiers/convert/decls.ts +0 -90
  138. package/src/packlets/qualifiers/convert/index.ts +0 -24
  139. package/src/packlets/qualifiers/convert/qualifier.ts +0 -55
  140. package/src/packlets/qualifiers/index.ts +0 -31
  141. package/src/packlets/qualifiers/qualifier.ts +0 -169
  142. package/src/packlets/qualifiers/qualifierCollector.ts +0 -194
  143. package/src/packlets/qualifiers/qualifierDecl.ts +0 -80
  144. package/src/packlets/qualifiers/qualifierDefaultValueDecls.ts +0 -57
  145. package/src/packlets/qualifiers/qualifierDefaultValueToken.ts +0 -230
  146. package/src/packlets/resource-json/compiled/common.ts +0 -35
  147. package/src/packlets/resource-json/compiled/convert.ts +0 -143
  148. package/src/packlets/resource-json/compiled/index.ts +0 -27
  149. package/src/packlets/resource-json/compiled/json.ts +0 -239
  150. package/src/packlets/resource-json/convert.ts +0 -236
  151. package/src/packlets/resource-json/helpers.ts +0 -284
  152. package/src/packlets/resource-json/index.ts +0 -33
  153. package/src/packlets/resource-json/json.ts +0 -295
  154. package/src/packlets/resource-json/normalized.ts +0 -211
  155. package/src/packlets/resource-json/resourceDeclCollection.ts +0 -117
  156. package/src/packlets/resource-json/resourceDeclContainer.ts +0 -48
  157. package/src/packlets/resource-json/resourceDeclTree.ts +0 -116
  158. package/src/packlets/resource-types/config/convert.ts +0 -36
  159. package/src/packlets/resource-types/config/index.ts +0 -27
  160. package/src/packlets/resource-types/config/json.ts +0 -30
  161. package/src/packlets/resource-types/helpers.ts +0 -44
  162. package/src/packlets/resource-types/index.ts +0 -30
  163. package/src/packlets/resource-types/jsonResourceType.ts +0 -104
  164. package/src/packlets/resource-types/resourceType.ts +0 -259
  165. package/src/packlets/resource-types/resourceTypeCollector.ts +0 -79
  166. package/src/packlets/resources/candidateReducer.ts +0 -331
  167. package/src/packlets/resources/common.ts +0 -114
  168. package/src/packlets/resources/index.ts +0 -28
  169. package/src/packlets/resources/resource.ts +0 -307
  170. package/src/packlets/resources/resourceBuilder.ts +0 -269
  171. package/src/packlets/resources/resourceCandidate.ts +0 -333
  172. package/src/packlets/resources/resourceManagerBuilder.ts +0 -1169
  173. package/src/packlets/runtime/cacheListener.ts +0 -113
  174. package/src/packlets/runtime/cacheMetrics.ts +0 -206
  175. package/src/packlets/runtime/compiledResourceCollection.ts +0 -607
  176. package/src/packlets/runtime/conditionSetResolutionResult.ts +0 -160
  177. package/src/packlets/runtime/context/contextQualifierProvider.ts +0 -117
  178. package/src/packlets/runtime/context/contextQualifierProviderValidator.ts +0 -281
  179. package/src/packlets/runtime/context/index.ts +0 -26
  180. package/src/packlets/runtime/context/simpleContextQualifierProvider.ts +0 -225
  181. package/src/packlets/runtime/context/validatingSimpleContextQualifierProvider.ts +0 -96
  182. package/src/packlets/runtime/iResourceManager.ts +0 -130
  183. package/src/packlets/runtime/index.ts +0 -35
  184. package/src/packlets/runtime/resource-tree/common.ts +0 -126
  185. package/src/packlets/runtime/resource-tree/index.ts +0 -26
  186. package/src/packlets/runtime/resource-tree/readOnlyResourceTree.ts +0 -342
  187. package/src/packlets/runtime/resource-tree/resourceTreeChildren.ts +0 -125
  188. package/src/packlets/runtime/resource-tree/resourceTreeChildrenValidator.ts +0 -187
  189. package/src/packlets/runtime/resourceResolver.ts +0 -626
  190. package/src/packlets/runtime/validate.ts +0 -63
  191. package/src/packlets/zip-archive/convert.ts +0 -121
  192. package/src/packlets/zip-archive/index.ts +0 -76
  193. package/src/packlets/zip-archive/json.ts +0 -91
  194. package/src/packlets/zip-archive/types.ts +0 -140
  195. package/src/packlets/zip-archive/zipArchiveCreator.ts +0 -229
  196. package/src/packlets/zip-archive/zipArchiveFormat.ts +0 -158
  197. package/src/packlets/zip-archive/zipArchiveLoader.ts +0 -370
@@ -1,607 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import {
24
- captureResult,
25
- Collections,
26
- fail,
27
- mapResults,
28
- MessageAggregator,
29
- Result,
30
- succeed,
31
- ValidatingResultMap
32
- } from '@fgv/ts-utils';
33
- import {
34
- ConditionCollector,
35
- ConditionSetCollector,
36
- ReadOnlyConditionCollector,
37
- ReadOnlyConditionSetCollector
38
- } from '../conditions';
39
- import { AbstractDecisionCollector, ReadOnlyAbstractDecisionCollector } from '../decisions';
40
- import { QualifierCollector } from '../qualifiers';
41
- import { ResourceType, ResourceTypeCollector } from '../resource-types';
42
- import { QualifierType, QualifierTypeCollector } from '../qualifier-types';
43
- import { Convert, ResourceId, Helpers } from '../common';
44
- import { Converters } from '@fgv/ts-json-base';
45
- import { IResourceManager, IResource, IResourceCandidate } from './iResourceManager';
46
- import { ConcreteDecision } from '../decisions';
47
- import * as Validate from './validate';
48
- import * as ResourceJson from '../resource-json';
49
- import { ReadOnlyResourceTreeRoot } from './resource-tree';
50
- import * as Context from '../context';
51
-
52
- /**
53
- * Interface for parameters to create a {@link Runtime.CompiledResourceCollection | CompiledResourceCollection}.
54
- * @public
55
- */
56
- export interface ICompiledResourceCollectionCreateParams {
57
- /**
58
- * The compiled resource collection data.
59
- */
60
- compiledCollection: ResourceJson.Compiled.ICompiledResourceCollection;
61
- /**
62
- * Map of qualifier type names to qualifier type objects.
63
- */
64
- qualifierTypes: Collections.IReadOnlyResultMap<string, QualifierType>;
65
- /**
66
- * Map of resource type names to resource type objects.
67
- */
68
- resourceTypes: Collections.IReadOnlyResultMap<string, ResourceType>;
69
- }
70
-
71
- /**
72
- * A compiled resource collection implements {@link Runtime.IResourceManager | IResourceManager}
73
- * by reconstructing runtime objects from compiled data. This provides an efficient way to load
74
- * and use pre-compiled resource collections without rebuilding them from scratch.
75
- * @public
76
- */
77
- export class CompiledResourceCollection implements IResourceManager<IResource> {
78
- public readonly conditions: ReadOnlyConditionCollector;
79
- public readonly conditionSets: ReadOnlyConditionSetCollector;
80
- public readonly decisions: ReadOnlyAbstractDecisionCollector;
81
-
82
- private readonly _qualifierTypes: QualifierTypeCollector;
83
- private readonly _qualifiers: QualifierCollector;
84
- private readonly _resourceTypes: ResourceTypeCollector;
85
- private readonly _builtResources: ValidatingResultMap<ResourceId, IResource>;
86
- private _cachedResourceTree?: ReadOnlyResourceTreeRoot<IResource>;
87
-
88
- /**
89
- * A {@link QualifierTypes.QualifierTypeCollector | QualifierTypeCollector} which
90
- * contains the {@link QualifierTypes.QualifierType | qualifier types} used in this collection.
91
- */
92
- public get qualifierTypes(): QualifierTypeCollector {
93
- return this._qualifierTypes;
94
- }
95
-
96
- /**
97
- * A {@link Qualifiers.QualifierCollector | QualifierCollector} which
98
- * contains the {@link Qualifiers.Qualifier | qualifiers} used in this collection.
99
- */
100
- public get qualifiers(): QualifierCollector {
101
- return this._qualifiers;
102
- }
103
-
104
- /**
105
- * A {@link ResourceTypes.ResourceTypeCollector | ResourceTypeCollector} which
106
- * contains the {@link ResourceTypes.ResourceType | resource types} used in this collection.
107
- */
108
- public get resourceTypes(): ResourceTypeCollector {
109
- return this._resourceTypes;
110
- }
111
-
112
- /**
113
- * {@inheritdoc Runtime.IResourceManager.builtResources}
114
- */
115
- public get builtResources(): Collections.IReadOnlyValidatingResultMap<ResourceId, IResource> {
116
- return this._builtResources;
117
- }
118
-
119
- /**
120
- * {@inheritdoc Runtime.IResourceManager.numResources}
121
- */
122
- public get numResources(): number {
123
- return this._builtResources.size;
124
- }
125
-
126
- protected _numCandidates?: number;
127
-
128
- /**
129
- * {@inheritdoc Runtime.IResourceManager.numCandidates}
130
- */
131
- public get numCandidates(): number {
132
- if (this._numCandidates === undefined) {
133
- this._numCandidates = [...this._builtResources.values()].reduce(
134
- (acc, resource) => acc + resource.candidates.length,
135
- 0
136
- );
137
- }
138
- return this._numCandidates;
139
- }
140
-
141
- /**
142
- * Constructor for a {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object.
143
- * @param params - Parameters to create a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection}.
144
- * @internal
145
- */
146
- protected constructor(params: ICompiledResourceCollectionCreateParams) {
147
- // Reconstruct collectors from compiled data
148
- this._qualifierTypes = this._buildQualifierTypes(
149
- params.compiledCollection,
150
- params.qualifierTypes
151
- ).orThrow();
152
- this._qualifiers = this._buildQualifiers(params.compiledCollection, this._qualifierTypes).orThrow();
153
- this._resourceTypes = this._buildResourceTypes(params.compiledCollection, params.resourceTypes).orThrow();
154
-
155
- // Build collectors from compiled data
156
- const conditionCollector = this._buildConditions(params.compiledCollection, this._qualifiers).orThrow();
157
- const conditionSetCollector = this._buildConditionSets(
158
- params.compiledCollection,
159
- conditionCollector
160
- ).orThrow();
161
- const decisionCollector = this._buildDecisions(
162
- params.compiledCollection,
163
- conditionSetCollector
164
- ).orThrow();
165
-
166
- this.conditions = conditionCollector;
167
- this.conditionSets = conditionSetCollector;
168
- this.decisions = decisionCollector;
169
-
170
- // Build resources from compiled data
171
- this._builtResources = this._buildResources(
172
- params.compiledCollection,
173
- this._resourceTypes,
174
- decisionCollector
175
- ).orThrow();
176
- }
177
-
178
- /**
179
- * Creates a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object.
180
- * @param params - Parameters to create a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection}.
181
- * @returns `Success` with the new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object if successful,
182
- * or `Failure` with an error message if not.
183
- * @public
184
- */
185
- public static create(params: ICompiledResourceCollectionCreateParams): Result<CompiledResourceCollection> {
186
- return captureResult(() => new CompiledResourceCollection(params));
187
- }
188
-
189
- /**
190
- * {@inheritdoc Runtime.IResourceManager.getBuiltResource}
191
- */
192
- public getBuiltResource(id: string): Result<IResource> {
193
- return this._builtResources.validating.get(id);
194
- }
195
-
196
- /**
197
- * {@inheritdoc Runtime.IResourceManager.validateContext}
198
- */
199
- public validateContext(context: Context.IContextDecl): Result<Context.IValidatedContextDecl> {
200
- return Context.Convert.validatedContextDecl.convert(context, {
201
- qualifiers: this._qualifiers
202
- });
203
- }
204
-
205
- /**
206
- * Gets a resource tree built from the resources in this collection.
207
- * The tree provides hierarchical access to resources based on their ResourceId structure.
208
- * For example, resources with IDs like "app.messages.welcome" create a tree structure
209
- * where "app" and "messages" are branch nodes, and "welcome" is a leaf containing the resource.
210
- *
211
- * String-based validation is available through the `children.validating` property,
212
- * allowing callers to use `tree.children.validating.getById(stringId)` for validated access.
213
- *
214
- * Uses lazy initialization with caching for performance.
215
- * @returns Result containing the resource tree root, or failure if tree construction fails
216
- * @public
217
- */
218
- public getBuiltResourceTree(): Result<ReadOnlyResourceTreeRoot<IResource>> {
219
- if (this._cachedResourceTree) {
220
- return succeed(this._cachedResourceTree);
221
- }
222
-
223
- // Convert all built resources to [ResourceId, IResource] pairs
224
- const resources: [ResourceId, IResource][] = [];
225
- for (const [id, resource] of this._builtResources.entries()) {
226
- resources.push([id, resource]);
227
- }
228
-
229
- // Create the resource tree with lazy initialization
230
- return ReadOnlyResourceTreeRoot.create(resources).onSuccess((tree) => {
231
- this._cachedResourceTree = tree;
232
- return succeed(tree);
233
- });
234
- }
235
-
236
- /**
237
- * Reconstructs a QualifierTypeCollector from compiled data.
238
- * @param compiled - The compiled resource collection
239
- * @param qualifierTypes - Map of qualifier type names to qualifier type objects
240
- * @returns The reconstructed QualifierTypeCollector
241
- * @internal
242
- */
243
- private _buildQualifierTypes(
244
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
245
- qualifierTypes: Collections.IReadOnlyResultMap<string, QualifierType>
246
- ): Result<QualifierTypeCollector> {
247
- return mapResults(
248
- compiled.qualifierTypes.map((compiledQualifierType) => qualifierTypes.get(compiledQualifierType.name))
249
- ).onSuccess((referencedQualifierTypes) => {
250
- return QualifierTypeCollector.create({
251
- qualifierTypes: referencedQualifierTypes
252
- });
253
- });
254
- }
255
-
256
- /**
257
- * Reconstructs a QualifierCollector from compiled data.
258
- * @param compiled - The compiled resource collection
259
- * @param qualifierTypes - The reconstructed QualifierTypeCollector
260
- * @returns The reconstructed QualifierCollector
261
- * @internal
262
- */
263
- private _buildQualifiers(
264
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
265
- qualifierTypes: QualifierTypeCollector
266
- ): Result<QualifierCollector> {
267
- return mapResults(
268
- compiled.qualifiers.map((compiledQualifier) => {
269
- return qualifierTypes.getAt(compiledQualifier.type).onSuccess((qualifierType) => {
270
- return succeed({
271
- name: compiledQualifier.name,
272
- typeName: qualifierType.name,
273
- defaultPriority: compiledQualifier.defaultPriority
274
- });
275
- });
276
- })
277
- ).onSuccess((qualifierDecls) => {
278
- return QualifierCollector.create({
279
- qualifierTypes,
280
- qualifiers: qualifierDecls
281
- });
282
- });
283
- }
284
-
285
- /**
286
- * Reconstructs a ResourceTypeCollector from compiled data.
287
- * @param compiled - The compiled resource collection
288
- * @param resourceTypes - Map of resource type names to resource type objects
289
- * @returns The reconstructed ResourceTypeCollector
290
- * @internal
291
- */
292
- private _buildResourceTypes(
293
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
294
- resourceTypes: Collections.IReadOnlyResultMap<string, ResourceType>
295
- ): Result<ResourceTypeCollector> {
296
- return mapResults(
297
- compiled.resourceTypes.map((compiledResourceType) => resourceTypes.get(compiledResourceType.name))
298
- ).onSuccess((referencedResourceTypes) => {
299
- return ResourceTypeCollector.create({
300
- resourceTypes: referencedResourceTypes
301
- });
302
- });
303
- }
304
-
305
- /**
306
- * Reconstructs a ConditionCollector from compiled data.
307
- * @param compiled - The compiled resource collection
308
- * @param qualifiers - The reconstructed QualifierCollector
309
- * @returns The reconstructed ConditionCollector
310
- * @internal
311
- */
312
- private _buildConditions(
313
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
314
- qualifiers: QualifierCollector
315
- ): Result<ConditionCollector> {
316
- const errors = new MessageAggregator();
317
-
318
- const conditionCollectorResult = ConditionCollector.create({ qualifiers });
319
- /* c8 ignore next 3 - defensive coding for ConditionCollector creation failure */
320
- if (conditionCollectorResult.isFailure()) {
321
- return conditionCollectorResult;
322
- }
323
- const conditionCollector = conditionCollectorResult.value;
324
-
325
- for (const [index, compiledCondition] of compiled.conditions.entries()) {
326
- const qualifierResult = qualifiers.getAt(compiledCondition.qualifierIndex);
327
- if (qualifierResult.isFailure()) {
328
- errors.addMessage(
329
- `Invalid qualifier index ${compiledCondition.qualifierIndex} at condition ${index}: ${qualifierResult.message}`
330
- );
331
- continue;
332
- }
333
- const qualifier = qualifierResult.value;
334
-
335
- /* c8 ignore next 1 - not really testable atm as "matches" is the only supported operator */
336
- const operator = compiledCondition.operator ?? 'matches'; // Default to 'matches' if not provided
337
- const conditionDecl = {
338
- qualifierName: qualifier.name,
339
- value: compiledCondition.value,
340
- operator,
341
- priority: compiledCondition.priority,
342
- scoreAsDefault: compiledCondition.scoreAsDefault
343
- };
344
-
345
- const conditionResult = conditionCollector.validating.add(conditionDecl);
346
- /* c8 ignore next 4 - defensive coding for condition addition failure */
347
- if (conditionResult.isFailure()) {
348
- errors.addMessage(`Failed to add condition at index ${index}: ${conditionResult.message}`);
349
- continue;
350
- }
351
- const condition = conditionResult.value;
352
-
353
- // Validate that the assigned index matches our expected index
354
- const expectedIndexResult = Convert.conditionIndex.convert(index);
355
- /* c8 ignore next 4 - defensive coding for invalid condition index conversion */
356
- if (expectedIndexResult.isFailure()) {
357
- errors.addMessage(`Invalid condition index ${index}: ${expectedIndexResult.message}`);
358
- continue;
359
- }
360
- const expectedIndex = expectedIndexResult.value;
361
-
362
- /* c8 ignore next 5 - defensive coding for condition index mismatch */
363
- if (condition.index !== expectedIndex) {
364
- errors.addMessage(
365
- `Index mismatch at condition ${index}: expected ${expectedIndex}, got ${condition.index}`
366
- );
367
- }
368
- }
369
-
370
- return errors.hasMessages ? fail(errors.toString()) : succeed(conditionCollector);
371
- }
372
-
373
- /**
374
- * Reconstructs a ConditionSetCollector from compiled data.
375
- * @param compiled - The compiled resource collection
376
- * @param conditions - The reconstructed ConditionCollector
377
- * @returns The reconstructed ConditionSetCollector
378
- * @internal
379
- */
380
- private _buildConditionSets(
381
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
382
- conditions: ConditionCollector
383
- ): Result<ConditionSetCollector> {
384
- const errors = new MessageAggregator();
385
-
386
- const conditionSetCollectorResult = ConditionSetCollector.create({ conditions });
387
- /* c8 ignore next 3 - defensive coding for ConditionSetCollector creation failure */
388
- if (conditionSetCollectorResult.isFailure()) {
389
- return conditionSetCollectorResult;
390
- }
391
- const conditionSetCollector = conditionSetCollectorResult.value;
392
-
393
- for (const [index, compiledConditionSet] of compiled.conditionSets.entries()) {
394
- const referencedConditionsResult = mapResults(
395
- compiledConditionSet.conditions.map((conditionIndex) => conditions.getAt(conditionIndex))
396
- );
397
- if (referencedConditionsResult.isFailure()) {
398
- errors.addMessage(
399
- `Failed to resolve conditions for condition set ${index}: ${referencedConditionsResult.message}`
400
- );
401
- continue;
402
- }
403
- const referencedConditions = referencedConditionsResult.value;
404
-
405
- const conditionSetResult = conditionSetCollector.validating.getOrAdd(referencedConditions);
406
- /* c8 ignore next 4 - defensive coding for condition set addition failure */
407
- if (conditionSetResult.isFailure()) {
408
- errors.addMessage(`Failed to add condition set at index ${index}: ${conditionSetResult.message}`);
409
- continue;
410
- }
411
- const conditionSet = conditionSetResult.value;
412
-
413
- // Validate that the assigned index matches our expected index
414
- const expectedIndexResult = Convert.conditionSetIndex.convert(index);
415
- /* c8 ignore next 4 - defensive coding for invalid condition set index conversion */
416
- if (expectedIndexResult.isFailure()) {
417
- errors.addMessage(`Invalid condition set index ${index}: ${expectedIndexResult.message}`);
418
- continue;
419
- }
420
- const expectedIndex = expectedIndexResult.value;
421
-
422
- /* c8 ignore next 5 - defensive coding for condition set index mismatch */
423
- if (conditionSet.index !== expectedIndex) {
424
- errors.addMessage(
425
- `Index mismatch at condition set ${index}: expected ${expectedIndex}, got ${conditionSet.index}`
426
- );
427
- }
428
- }
429
-
430
- return errors.hasMessages ? fail(errors.toString()) : succeed(conditionSetCollector);
431
- }
432
-
433
- /**
434
- * Reconstructs an AbstractDecisionCollector from compiled data.
435
- * @param compiled - The compiled resource collection
436
- * @param conditionSets - The reconstructed ConditionSetCollector
437
- * @returns The reconstructed AbstractDecisionCollector
438
- * @internal
439
- */
440
- private _buildDecisions(
441
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
442
- conditionSets: ConditionSetCollector
443
- ): Result<AbstractDecisionCollector> {
444
- const errors = new MessageAggregator();
445
-
446
- const decisionCollectorResult = AbstractDecisionCollector.create({ conditionSets });
447
- /* c8 ignore next 3 - defensive coding for AbstractDecisionCollector creation failure */
448
- if (decisionCollectorResult.isFailure()) {
449
- return decisionCollectorResult;
450
- }
451
- const decisionCollector = decisionCollectorResult.value;
452
-
453
- for (const [index, compiledDecision] of compiled.decisions.entries()) {
454
- const referencedConditionSetsResult = mapResults(
455
- compiledDecision.conditionSets.map((conditionSetIndex) => conditionSets.getAt(conditionSetIndex))
456
- );
457
- if (referencedConditionSetsResult.isFailure()) {
458
- errors.addMessage(
459
- `Failed to resolve condition sets for decision ${index}: ${referencedConditionSetsResult.message}`
460
- );
461
- continue;
462
- }
463
- const referencedConditionSets = referencedConditionSetsResult.value;
464
-
465
- const decisionResult = decisionCollector.validating.getOrAdd(referencedConditionSets);
466
- /* c8 ignore next 4 - defensive coding for decision addition failure */
467
- if (decisionResult.isFailure()) {
468
- errors.addMessage(`Failed to add decision at index ${index}: ${decisionResult.message}`);
469
- continue;
470
- }
471
- const decision = decisionResult.value;
472
-
473
- // Validate that the assigned index matches our expected index
474
- const expectedIndexResult = Convert.decisionIndex.convert(index);
475
- /* c8 ignore next 4 - defensive coding for invalid decision index conversion */
476
- if (expectedIndexResult.isFailure()) {
477
- errors.addMessage(`Invalid decision index ${index}: ${expectedIndexResult.message}`);
478
- continue;
479
- }
480
- const expectedIndex = expectedIndexResult.value;
481
-
482
- /* c8 ignore next 5 - defensive coding for decision index mismatch */
483
- if (decision.index !== expectedIndex) {
484
- errors.addMessage(
485
- `Index mismatch at decision ${index}: expected ${expectedIndex}, got ${decision.index}`
486
- );
487
- }
488
- }
489
-
490
- return errors.hasMessages ? fail(errors.toString()) : succeed(decisionCollector);
491
- }
492
-
493
- /**
494
- * Reconstructs a ValidatingResultMap of resources from compiled data.
495
- * @param compiled - The compiled resource collection
496
- * @param resourceTypes - The reconstructed ResourceTypeCollector
497
- * @param decisions - The reconstructed AbstractDecisionCollector
498
- * @returns The reconstructed ValidatingResultMap of resources
499
- * @internal
500
- */
501
- private _buildResources(
502
- compiled: ResourceJson.Compiled.ICompiledResourceCollection,
503
- resourceTypes: ResourceTypeCollector,
504
- decisions: AbstractDecisionCollector
505
- ): Result<ValidatingResultMap<ResourceId, IResource>> {
506
- const errors = new MessageAggregator();
507
-
508
- const resourceMap = new ValidatingResultMap<ResourceId, IResource>({
509
- converters: new Collections.KeyValueConverters<ResourceId, IResource>({
510
- key: Convert.resourceId,
511
- value: (from: unknown): Result<IResource> => {
512
- return Validate.resource.validate(from);
513
- }
514
- })
515
- });
516
-
517
- for (const compiledResource of compiled.resources) {
518
- const resourceTypeResult = resourceTypes.getAt(compiledResource.type);
519
- if (resourceTypeResult.isFailure()) {
520
- errors.addMessage(
521
- `Invalid resource type index ${compiledResource.type} for resource ${compiledResource.id}: ${resourceTypeResult.message}`
522
- );
523
- continue;
524
- }
525
- const resourceType = resourceTypeResult.value;
526
-
527
- const decisionResult = decisions.getAt(compiledResource.decision);
528
- if (decisionResult.isFailure()) {
529
- errors.addMessage(
530
- `Invalid decision index ${compiledResource.decision} for resource ${compiledResource.id}: ${decisionResult.message}`
531
- );
532
- continue;
533
- }
534
- const decision = decisionResult.value;
535
-
536
- // Build candidates from compiled data
537
- const candidateDeclsResult = mapResults(
538
- compiledResource.candidates.map((compiledCandidate) =>
539
- Converters.jsonObject.convert(compiledCandidate.json).onSuccess((json) => {
540
- return succeed({
541
- json,
542
- isPartial: compiledCandidate.isPartial,
543
- mergeMethod: compiledCandidate.mergeMethod
544
- });
545
- })
546
- )
547
- );
548
- if (candidateDeclsResult.isFailure()) {
549
- errors.addMessage(
550
- `Failed to convert candidate JSON for resource ${compiledResource.id}: ${candidateDeclsResult.message}`
551
- );
552
- continue;
553
- }
554
- const candidateDecls = candidateDeclsResult.value;
555
-
556
- // Create minimal candidates that implement IResourceCandidate
557
- const candidates: IResourceCandidate[] = candidateDecls.map((candidateDecl) => ({
558
- json: candidateDecl.json,
559
- isPartial: candidateDecl.isPartial,
560
- mergeMethod: candidateDecl.mergeMethod
561
- }));
562
-
563
- // Create a ConcreteDecision from the abstract decision and candidate values
564
- const candidatesWithConditionSets = decision.candidates.map((baseCandidate, idx) => ({
565
- conditionSet: baseCandidate.conditionSet,
566
- value: candidates[idx].json
567
- }));
568
-
569
- const concreteDecisionResult = ConcreteDecision.create({
570
- decisions,
571
- candidates: candidatesWithConditionSets
572
- });
573
- /* c8 ignore next 6 - defensive coding for ConcreteDecision creation failure */
574
- if (concreteDecisionResult.isFailure()) {
575
- errors.addMessage(
576
- `Failed to create concrete decision for resource ${compiledResource.id}: ${concreteDecisionResult.message}`
577
- );
578
- continue;
579
- }
580
- const concreteDecision = concreteDecisionResult.value;
581
-
582
- const { value: name, message: nameError } = Helpers.getNameForResourceId(compiledResource.id);
583
- /* c8 ignore next 4 - defense in depth nearly impossible to reproduce */
584
- if (nameError !== undefined) {
585
- errors.addMessage(`Failed to get name for resource ${compiledResource.id}: ${nameError}`);
586
- continue;
587
- }
588
-
589
- // Create minimal resource that implements IResource
590
- const resource: IResource = {
591
- id: compiledResource.id,
592
- name,
593
- resourceType,
594
- decision: concreteDecision,
595
- candidates
596
- };
597
-
598
- const setResult = resourceMap.set(compiledResource.id, resource);
599
- /* c8 ignore next 3 - defensive coding for resource map set failure */
600
- if (setResult.isFailure()) {
601
- errors.addMessage(`Failed to add resource ${compiledResource.id}: ${setResult.message}`);
602
- }
603
- }
604
-
605
- return errors.hasMessages ? fail(errors.toString()) : succeed(resourceMap);
606
- }
607
- }