@fgv/ts-res 5.0.1-1 → 5.0.1-10
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.
- package/dist/index.browser.js +53 -0
- package/dist/index.js +45 -0
- package/dist/packlets/bundle/bundleBuilder.js +130 -0
- package/dist/packlets/bundle/bundleLoader.js +101 -0
- package/dist/packlets/bundle/bundleNormalizer.js +138 -0
- package/dist/packlets/bundle/bundleUtils.js +113 -0
- package/dist/packlets/bundle/convert.js +75 -0
- package/dist/packlets/bundle/index.js +42 -0
- package/dist/packlets/bundle/model.js +23 -0
- package/dist/packlets/common/conditions.js +47 -0
- package/dist/packlets/common/convert.js +172 -0
- package/dist/packlets/common/helpers/conditions.js +66 -0
- package/dist/packlets/common/helpers/context.js +70 -0
- package/dist/packlets/common/helpers/index.js +26 -0
- package/dist/packlets/common/helpers/qualifierDefaultValues.js +66 -0
- package/dist/packlets/common/helpers/resources.js +93 -0
- package/dist/packlets/common/index.js +28 -0
- package/dist/packlets/common/resources.js +27 -0
- package/dist/packlets/common/validate/conditions.js +544 -0
- package/dist/packlets/common/validate/index.js +26 -0
- package/dist/packlets/common/validate/regularExpressions.js +84 -0
- package/dist/packlets/common/validate/resources.js +209 -0
- package/dist/packlets/conditions/condition.js +252 -0
- package/dist/packlets/conditions/conditionCollector.js +69 -0
- package/dist/packlets/conditions/conditionDecls.js +23 -0
- package/dist/packlets/conditions/conditionSet.js +247 -0
- package/dist/packlets/conditions/conditionSetCollector.js +89 -0
- package/dist/packlets/conditions/conditionSetDecls.js +23 -0
- package/dist/packlets/conditions/conditionToken.js +147 -0
- package/dist/packlets/conditions/convert/conditionSetDecls.js +51 -0
- package/dist/packlets/conditions/convert/decls.js +75 -0
- package/dist/packlets/conditions/convert/index.js +24 -0
- package/dist/packlets/conditions/index.js +31 -0
- package/dist/packlets/config/common.js +78 -0
- package/dist/packlets/config/configInitFactory.js +258 -0
- package/dist/packlets/config/convert.js +56 -0
- package/dist/packlets/config/index.browser.js +34 -0
- package/dist/packlets/config/index.js +28 -0
- package/dist/packlets/config/json.js +23 -0
- package/dist/packlets/config/predefined/default.js +138 -0
- package/dist/packlets/config/predefined/extended.js +190 -0
- package/dist/packlets/config/predefined/index.js +25 -0
- package/dist/packlets/config/systemConfiguration.js +147 -0
- package/dist/packlets/context/contextDecls.js +23 -0
- package/dist/packlets/context/contextToken.js +202 -0
- package/dist/packlets/context/convert/decls.js +91 -0
- package/dist/packlets/context/convert/index.js +23 -0
- package/dist/packlets/context/index.js +26 -0
- package/dist/packlets/decisions/abstractDecision.js +68 -0
- package/dist/packlets/decisions/abstractDecisionCollector.js +90 -0
- package/dist/packlets/decisions/candidate.js +76 -0
- package/dist/packlets/decisions/common.js +23 -0
- package/dist/packlets/decisions/concreteDecision.js +111 -0
- package/dist/packlets/decisions/decision.js +126 -0
- package/dist/packlets/decisions/index.js +28 -0
- package/dist/packlets/import/fsItem.js +134 -0
- package/dist/packlets/import/importContext.js +129 -0
- package/dist/packlets/import/importManager.js +129 -0
- package/dist/packlets/import/importable.js +32 -0
- package/dist/packlets/import/importers/collectionImporter.js +118 -0
- package/dist/packlets/import/importers/fsItemImporter.js +116 -0
- package/dist/packlets/import/importers/importer.js +23 -0
- package/dist/packlets/import/importers/index.browser.js +29 -0
- package/dist/packlets/import/importers/index.js +27 -0
- package/dist/packlets/import/importers/jsonImporter.js +94 -0
- package/dist/packlets/import/importers/pathImporter.js +84 -0
- package/dist/packlets/import/index.browser.js +34 -0
- package/dist/packlets/import/index.js +28 -0
- package/dist/packlets/qualifier-types/config/convert.js +124 -0
- package/dist/packlets/qualifier-types/config/index.js +25 -0
- package/dist/packlets/qualifier-types/config/json.js +32 -0
- package/dist/packlets/qualifier-types/convert.js +56 -0
- package/dist/packlets/qualifier-types/helpers.js +82 -0
- package/dist/packlets/qualifier-types/index.js +32 -0
- package/dist/packlets/qualifier-types/languageQualifierType.js +142 -0
- package/dist/packlets/qualifier-types/literalQualifierType.js +200 -0
- package/dist/packlets/qualifier-types/literalValueHierarchy.js +236 -0
- package/dist/packlets/qualifier-types/qualifierType.js +184 -0
- package/dist/packlets/qualifier-types/qualifierTypeCollector.js +66 -0
- package/dist/packlets/qualifier-types/territoryQualifierType.js +200 -0
- package/dist/packlets/qualifiers/convert/decls.js +70 -0
- package/dist/packlets/qualifiers/convert/index.js +24 -0
- package/dist/packlets/qualifiers/convert/qualifier.js +40 -0
- package/dist/packlets/qualifiers/index.js +29 -0
- package/dist/packlets/qualifiers/qualifier.js +108 -0
- package/dist/packlets/qualifiers/qualifierCollector.js +123 -0
- package/dist/packlets/qualifiers/qualifierDecl.js +23 -0
- package/dist/packlets/qualifiers/qualifierDefaultValueDecls.js +23 -0
- package/dist/packlets/qualifiers/qualifierDefaultValueToken.js +175 -0
- package/dist/packlets/resource-json/compiled/common.js +23 -0
- package/dist/packlets/resource-json/compiled/convert.js +132 -0
- package/dist/packlets/resource-json/compiled/index.js +26 -0
- package/dist/packlets/resource-json/compiled/json.js +2 -0
- package/dist/packlets/resource-json/convert.js +171 -0
- package/dist/packlets/resource-json/helpers.js +241 -0
- package/dist/packlets/resource-json/index.js +31 -0
- package/dist/packlets/resource-json/json.js +36 -0
- package/dist/packlets/resource-json/normalized.js +23 -0
- package/dist/packlets/resource-json/resourceDeclCollection.js +88 -0
- package/dist/packlets/resource-json/resourceDeclContainer.js +23 -0
- package/dist/packlets/resource-json/resourceDeclTree.js +94 -0
- package/dist/packlets/resource-types/config/convert.js +35 -0
- package/dist/packlets/resource-types/config/index.js +25 -0
- package/dist/packlets/resource-types/config/json.js +23 -0
- package/dist/packlets/resource-types/helpers.js +42 -0
- package/dist/packlets/resource-types/index.js +28 -0
- package/dist/packlets/resource-types/jsonResourceType.js +66 -0
- package/dist/packlets/resource-types/resourceType.js +95 -0
- package/dist/packlets/resource-types/resourceTypeCollector.js +61 -0
- package/dist/packlets/resources/candidateReducer.js +246 -0
- package/dist/packlets/resources/candidateValue.js +92 -0
- package/dist/packlets/resources/candidateValueCollector.js +94 -0
- package/dist/packlets/resources/common.js +23 -0
- package/dist/packlets/resources/deltaGenerator.js +338 -0
- package/dist/packlets/resources/index.js +31 -0
- package/dist/packlets/resources/resource.js +207 -0
- package/dist/packlets/resources/resourceBuilder.js +183 -0
- package/dist/packlets/resources/resourceCandidate.js +216 -0
- package/dist/packlets/resources/resourceManagerBuilder.js +890 -0
- package/dist/packlets/runtime/cacheListener.js +58 -0
- package/dist/packlets/runtime/cacheMetrics.js +149 -0
- package/dist/packlets/runtime/compiledResourceCollection.js +473 -0
- package/dist/packlets/runtime/conditionSetResolutionResult.js +134 -0
- package/dist/packlets/runtime/context/contextQualifierProvider.js +29 -0
- package/dist/packlets/runtime/context/contextQualifierProviderValidator.js +146 -0
- package/dist/packlets/runtime/context/index.js +26 -0
- package/dist/packlets/runtime/context/simpleContextQualifierProvider.js +196 -0
- package/dist/packlets/runtime/context/validatingSimpleContextQualifierProvider.js +60 -0
- package/dist/packlets/runtime/iResourceManager.js +23 -0
- package/dist/packlets/runtime/index.js +34 -0
- package/dist/packlets/runtime/resource-tree/common.js +23 -0
- package/dist/packlets/runtime/resource-tree/index.js +26 -0
- package/dist/packlets/runtime/resource-tree/readOnlyResourceTree.js +216 -0
- package/dist/packlets/runtime/resource-tree/resourceTreeChildren.js +97 -0
- package/dist/packlets/runtime/resource-tree/resourceTreeChildrenValidator.js +161 -0
- package/dist/packlets/runtime/resourceResolver.js +481 -0
- package/dist/packlets/runtime/resourceTreeResolver.js +220 -0
- package/dist/packlets/runtime/validate.js +47 -0
- package/dist/packlets/zip-archive/convert.js +100 -0
- package/dist/packlets/zip-archive/index.browser.js +54 -0
- package/dist/packlets/zip-archive/index.js +47 -0
- package/dist/packlets/zip-archive/json.js +23 -0
- package/dist/packlets/zip-archive/types.js +36 -0
- package/dist/packlets/zip-archive/zipArchiveCreator.js +191 -0
- package/dist/packlets/zip-archive/zipArchiveFormat.js +140 -0
- package/dist/packlets/zip-archive/zipArchiveLoader.js +282 -0
- package/dist/test/data/sample.json +32 -0
- package/dist/test/unit/qualifier-types/testQualifierType.js +76 -0
- package/dist/test/unit/resource-types/testDerivedResourceType.js +109 -0
- package/dist/test/unit/resources/deltaGenerator.helpers.js +282 -0
- package/dist/ts-res.d.ts +15 -15
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +24 -0
- package/lib/index.browser.js +116 -0
- package/lib/packlets/common/helpers/resources.d.ts +1 -1
- package/lib/packlets/common/helpers/resources.js +3 -2
- package/lib/packlets/common/validate/conditions.js +14 -14
- package/lib/packlets/common/validate/resources.js +7 -7
- package/lib/packlets/conditions/conditionSet.js +1 -1
- package/lib/packlets/config/common.js +1 -0
- package/lib/packlets/config/configInitFactory.js +1 -0
- package/lib/packlets/config/index.browser.d.ts +7 -0
- package/lib/packlets/config/index.browser.js +74 -0
- package/lib/packlets/config/systemConfiguration.js +1 -0
- package/lib/packlets/context/convert/decls.js +1 -1
- package/lib/packlets/import/importers/collectionImporter.js +1 -1
- package/lib/packlets/import/importers/index.browser.d.ts +4 -0
- package/lib/packlets/import/importers/index.browser.js +45 -0
- package/lib/packlets/import/importers/jsonImporter.js +1 -1
- package/lib/packlets/import/index.browser.d.ts +7 -0
- package/lib/packlets/import/index.browser.js +74 -0
- package/lib/packlets/qualifier-types/helpers.js +5 -4
- package/lib/packlets/qualifier-types/languageQualifierType.d.ts +4 -4
- package/lib/packlets/qualifier-types/literalQualifierType.d.ts +4 -4
- package/lib/packlets/qualifier-types/literalQualifierType.js +1 -1
- package/lib/packlets/qualifier-types/literalValueHierarchy.js +7 -6
- package/lib/packlets/qualifier-types/qualifierType.d.ts +5 -5
- package/lib/packlets/qualifier-types/qualifierType.js +1 -0
- package/lib/packlets/qualifier-types/territoryQualifierType.d.ts +4 -4
- package/lib/packlets/qualifier-types/territoryQualifierType.js +1 -1
- package/lib/packlets/resource-json/helpers.js +4 -0
- package/lib/packlets/resource-types/helpers.js +1 -0
- package/lib/packlets/resource-types/resourceType.js +1 -0
- package/lib/packlets/resources/candidateValue.js +1 -0
- package/lib/packlets/resources/candidateValueCollector.js +4 -2
- package/lib/packlets/resources/deltaGenerator.js +8 -10
- package/lib/packlets/resources/resourceBuilder.js +4 -0
- package/lib/packlets/resources/resourceManagerBuilder.js +6 -0
- package/lib/packlets/runtime/compiledResourceCollection.js +1 -0
- package/lib/packlets/runtime/context/simpleContextQualifierProvider.js +4 -3
- package/lib/packlets/runtime/resource-tree/resourceTreeChildren.js +3 -0
- package/lib/packlets/runtime/resourceResolver.js +2 -4
- package/lib/packlets/runtime/resourceTreeResolver.js +2 -0
- package/lib/packlets/zip-archive/index.browser.d.ts +27 -0
- package/lib/packlets/zip-archive/index.browser.js +102 -0
- package/package.json +34 -20
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
* A no-op implementation of {@link Runtime.IResourceResolverCacheListener}.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export class NoOpResourceResolverCacheListener {
|
|
27
|
+
/**
|
|
28
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheHit}
|
|
29
|
+
*/
|
|
30
|
+
onCacheHit(cache, index) {
|
|
31
|
+
// no-op
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheMiss}
|
|
35
|
+
*/
|
|
36
|
+
onCacheMiss(cache, index) {
|
|
37
|
+
// no-op
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheError}
|
|
41
|
+
*/
|
|
42
|
+
onCacheError(cache, index) {
|
|
43
|
+
// no-op
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onContextError}
|
|
47
|
+
*/
|
|
48
|
+
onContextError(qualifier, error) {
|
|
49
|
+
// no-op
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheClear}
|
|
53
|
+
*/
|
|
54
|
+
onCacheClear(cache) {
|
|
55
|
+
// no-op
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=cacheListener.js.map
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
+
* Aggregate cache metrics for a specific cache type.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export class AggregateCacheMetrics {
|
|
27
|
+
constructor() {
|
|
28
|
+
this._hits = 0;
|
|
29
|
+
this._misses = 0;
|
|
30
|
+
this._errors = 0;
|
|
31
|
+
this._clears = 0;
|
|
32
|
+
this._hits = 0;
|
|
33
|
+
this._misses = 0;
|
|
34
|
+
this._errors = 0;
|
|
35
|
+
this._clears = 0;
|
|
36
|
+
}
|
|
37
|
+
onHit(__index) {
|
|
38
|
+
this._hits++;
|
|
39
|
+
}
|
|
40
|
+
onMiss(__index) {
|
|
41
|
+
this._misses++;
|
|
42
|
+
}
|
|
43
|
+
onError(__index) {
|
|
44
|
+
this._errors++;
|
|
45
|
+
}
|
|
46
|
+
onClear() {
|
|
47
|
+
this._hits = 0;
|
|
48
|
+
this._misses = 0;
|
|
49
|
+
this._errors = 0;
|
|
50
|
+
this._clears++;
|
|
51
|
+
}
|
|
52
|
+
get hits() {
|
|
53
|
+
return this._hits;
|
|
54
|
+
}
|
|
55
|
+
get misses() {
|
|
56
|
+
return this._misses;
|
|
57
|
+
}
|
|
58
|
+
get errors() {
|
|
59
|
+
return this._errors;
|
|
60
|
+
}
|
|
61
|
+
get clears() {
|
|
62
|
+
return this._clears;
|
|
63
|
+
}
|
|
64
|
+
get totalAccesses() {
|
|
65
|
+
return this._hits + this._misses + this._errors;
|
|
66
|
+
}
|
|
67
|
+
get hitRate() {
|
|
68
|
+
return this.totalAccesses > 0 ? (this._hits / this.totalAccesses) * 100 : 0;
|
|
69
|
+
}
|
|
70
|
+
get errorRate() {
|
|
71
|
+
return this.totalAccesses > 0 ? (this._errors / this.totalAccesses) * 100 : 0;
|
|
72
|
+
}
|
|
73
|
+
reset() {
|
|
74
|
+
this._hits = 0;
|
|
75
|
+
this._misses = 0;
|
|
76
|
+
this._errors = 0;
|
|
77
|
+
this._clears = 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A metrics implementation of {@link Runtime.IResourceResolverCacheListener} that tracks
|
|
82
|
+
* hit counts and rates across all cache types.
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
export class ResourceResolverCacheMetricsListener {
|
|
86
|
+
get numContextErrors() {
|
|
87
|
+
return this._contextErrors;
|
|
88
|
+
}
|
|
89
|
+
constructor(factoryOrMetrics) {
|
|
90
|
+
if (typeof factoryOrMetrics === 'function') {
|
|
91
|
+
this._metrics = {
|
|
92
|
+
condition: factoryOrMetrics(),
|
|
93
|
+
conditionSet: factoryOrMetrics(),
|
|
94
|
+
decision: factoryOrMetrics()
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this._metrics = factoryOrMetrics;
|
|
99
|
+
}
|
|
100
|
+
this._contextErrors = 0;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the metrics for all cache types.
|
|
104
|
+
* @returns The metrics for all cache types.
|
|
105
|
+
*/
|
|
106
|
+
get metrics() {
|
|
107
|
+
return this._metrics;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheHit}
|
|
111
|
+
*/
|
|
112
|
+
onCacheHit(cache, index) {
|
|
113
|
+
this._metrics[cache].onHit(index);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheMiss}
|
|
117
|
+
*/
|
|
118
|
+
onCacheMiss(cache, index) {
|
|
119
|
+
this._metrics[cache].onMiss(index);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheError}
|
|
123
|
+
*/
|
|
124
|
+
onCacheError(cache, index) {
|
|
125
|
+
this._metrics[cache].onError(index);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onContextError}
|
|
129
|
+
*/
|
|
130
|
+
onContextError(qualifier, error) {
|
|
131
|
+
this._contextErrors++;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* {@inheritDoc Runtime.IResourceResolverCacheListener.onCacheClear}
|
|
135
|
+
*/
|
|
136
|
+
onCacheClear(cache) {
|
|
137
|
+
this._metrics[cache].onClear();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Reset all metrics to zero.
|
|
141
|
+
*/
|
|
142
|
+
reset() {
|
|
143
|
+
this._metrics.condition.reset();
|
|
144
|
+
this._metrics.conditionSet.reset();
|
|
145
|
+
this._metrics.decision.reset();
|
|
146
|
+
this._contextErrors = 0;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=cacheMetrics.js.map
|
|
@@ -0,0 +1,473 @@
|
|
|
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
|
+
import { captureResult, Collections, fail, mapResults, MessageAggregator, succeed, ValidatingResultMap } from '@fgv/ts-utils';
|
|
23
|
+
import { ConditionCollector, ConditionSetCollector } from '../conditions';
|
|
24
|
+
import { AbstractDecisionCollector } from '../decisions';
|
|
25
|
+
import { QualifierCollector } from '../qualifiers';
|
|
26
|
+
import { ResourceTypeCollector } from '../resource-types';
|
|
27
|
+
import { QualifierTypeCollector } from '../qualifier-types';
|
|
28
|
+
import { Convert, Helpers } from '../common';
|
|
29
|
+
import { isJsonObject } from '@fgv/ts-json-base';
|
|
30
|
+
import { ConcreteDecision } from '../decisions';
|
|
31
|
+
import * as Validate from './validate';
|
|
32
|
+
import { ReadOnlyResourceTreeRoot } from './resource-tree';
|
|
33
|
+
import * as Context from '../context';
|
|
34
|
+
/**
|
|
35
|
+
* A compiled resource collection implements {@link Runtime.IResourceManager | IResourceManager}
|
|
36
|
+
* by reconstructing runtime objects from compiled data. This provides an efficient way to load
|
|
37
|
+
* and use pre-compiled resource collections without rebuilding them from scratch.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export class CompiledResourceCollection {
|
|
41
|
+
/**
|
|
42
|
+
* A {@link QualifierTypes.ReadOnlyQualifierTypeCollector | ReadOnlyQualifierTypeCollector} which
|
|
43
|
+
* contains the {@link QualifierTypes.QualifierType | qualifier types} used in this collection.
|
|
44
|
+
*/
|
|
45
|
+
get qualifierTypes() {
|
|
46
|
+
return this._qualifierTypes;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A {@link Qualifiers.IReadOnlyQualifierCollector | ReadOnlyQualifierCollector} which
|
|
50
|
+
* contains the {@link Qualifiers.Qualifier | qualifiers} used in this collection.
|
|
51
|
+
*/
|
|
52
|
+
get qualifiers() {
|
|
53
|
+
return this._qualifiers;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The resource IDs contained in this compiled resource collection.
|
|
57
|
+
*/
|
|
58
|
+
get resourceIds() {
|
|
59
|
+
return Array.from(this._builtResources.keys()).sort();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A {@link ResourceTypes.ResourceTypeCollector | ResourceTypeCollector} which
|
|
63
|
+
* contains the {@link ResourceTypes.ResourceType | resource types} used in this collection.
|
|
64
|
+
*/
|
|
65
|
+
get resourceTypes() {
|
|
66
|
+
return this._resourceTypes;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The candidate values in the collection.
|
|
70
|
+
*/
|
|
71
|
+
get candidateValues() {
|
|
72
|
+
return this._candidateValues;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritdoc Runtime.IResourceManager.builtResources}
|
|
76
|
+
*/
|
|
77
|
+
get builtResources() {
|
|
78
|
+
return this._builtResources;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* {@inheritdoc Runtime.IResourceManager.numResources}
|
|
82
|
+
*/
|
|
83
|
+
get numResources() {
|
|
84
|
+
return this._builtResources.size;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* {@inheritdoc Runtime.IResourceManager.numCandidates}
|
|
88
|
+
*/
|
|
89
|
+
get numCandidates() {
|
|
90
|
+
if (this._numCandidates === undefined) {
|
|
91
|
+
this._numCandidates = [...this._builtResources.values()].reduce((acc, resource) => acc + resource.candidates.length, 0);
|
|
92
|
+
}
|
|
93
|
+
return this._numCandidates;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Constructor for a {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object.
|
|
97
|
+
* @param params - Parameters to create a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection}.
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
constructor(params) {
|
|
101
|
+
// Reconstruct collectors from compiled data
|
|
102
|
+
this._qualifierTypes = this._buildQualifierTypes(params.compiledCollection, params.qualifierTypes).orThrow();
|
|
103
|
+
this._qualifiers = this._buildQualifiers(params.compiledCollection, this._qualifierTypes).orThrow();
|
|
104
|
+
this._resourceTypes = this._buildResourceTypes(params.compiledCollection, params.resourceTypes).orThrow();
|
|
105
|
+
// Build collectors from compiled data
|
|
106
|
+
const conditionCollector = this._buildConditions(params.compiledCollection, this._qualifiers).orThrow();
|
|
107
|
+
const conditionSetCollector = this._buildConditionSets(params.compiledCollection, conditionCollector).orThrow();
|
|
108
|
+
const decisionCollector = this._buildDecisions(params.compiledCollection, conditionSetCollector).orThrow();
|
|
109
|
+
this.conditions = conditionCollector;
|
|
110
|
+
this.conditionSets = conditionSetCollector;
|
|
111
|
+
this.decisions = decisionCollector;
|
|
112
|
+
this._candidateValues = [...params.compiledCollection.candidateValues];
|
|
113
|
+
// Build resources from compiled data
|
|
114
|
+
this._builtResources = this._buildResources(params.compiledCollection, this._resourceTypes, decisionCollector).orThrow();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object.
|
|
118
|
+
* @param params - Parameters to create a new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection}.
|
|
119
|
+
* @returns `Success` with the new {@link Runtime.CompiledResourceCollection | CompiledResourceCollection} object if successful,
|
|
120
|
+
* or `Failure` with an error message if not.
|
|
121
|
+
* @public
|
|
122
|
+
*/
|
|
123
|
+
static create(params) {
|
|
124
|
+
return captureResult(() => new CompiledResourceCollection(params));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* {@inheritdoc Runtime.IResourceManager.getBuiltResource}
|
|
128
|
+
*/
|
|
129
|
+
getBuiltResource(id) {
|
|
130
|
+
return this._builtResources.validating.get(id);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* {@inheritdoc Runtime.IResourceManager.validateContext}
|
|
134
|
+
*/
|
|
135
|
+
validateContext(context) {
|
|
136
|
+
return Context.Convert.validatedContextDecl.convert(context, {
|
|
137
|
+
qualifiers: this._qualifiers
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Gets a resource tree built from the resources in this collection.
|
|
142
|
+
* The tree provides hierarchical access to resources based on their ResourceId structure.
|
|
143
|
+
* For example, resources with IDs like "app.messages.welcome" create a tree structure
|
|
144
|
+
* where "app" and "messages" are branch nodes, and "welcome" is a leaf containing the resource.
|
|
145
|
+
*
|
|
146
|
+
* String-based validation is available through the `children.validating` property,
|
|
147
|
+
* allowing callers to use `tree.children.validating.getById(stringId)` for validated access.
|
|
148
|
+
*
|
|
149
|
+
* Uses lazy initialization with caching for performance.
|
|
150
|
+
* @returns Result containing the resource tree root, or failure if tree construction fails
|
|
151
|
+
* @public
|
|
152
|
+
*/
|
|
153
|
+
getBuiltResourceTree() {
|
|
154
|
+
if (this._cachedResourceTree) {
|
|
155
|
+
return succeed(this._cachedResourceTree);
|
|
156
|
+
}
|
|
157
|
+
// Convert all built resources to [ResourceId, IResource] pairs
|
|
158
|
+
const resources = [];
|
|
159
|
+
for (const [id, resource] of this._builtResources.entries()) {
|
|
160
|
+
resources.push([id, resource]);
|
|
161
|
+
}
|
|
162
|
+
// Create the resource tree with lazy initialization
|
|
163
|
+
return ReadOnlyResourceTreeRoot.create(resources).onSuccess((tree) => {
|
|
164
|
+
this._cachedResourceTree = tree;
|
|
165
|
+
return succeed(tree);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Reconstructs a QualifierTypeCollector from compiled data.
|
|
170
|
+
* @param compiled - The compiled resource collection
|
|
171
|
+
* @param qualifierTypes - Map of qualifier type names to qualifier type objects
|
|
172
|
+
* @returns The reconstructed QualifierTypeCollector
|
|
173
|
+
* @internal
|
|
174
|
+
*/
|
|
175
|
+
_buildQualifierTypes(compiled, qualifierTypes) {
|
|
176
|
+
return mapResults(compiled.qualifierTypes.map((compiledQualifierType) => qualifierTypes.get(compiledQualifierType.name))).onSuccess((referencedQualifierTypes) => {
|
|
177
|
+
return QualifierTypeCollector.create({
|
|
178
|
+
qualifierTypes: referencedQualifierTypes
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Reconstructs a QualifierCollector from compiled data.
|
|
184
|
+
* @param compiled - The compiled resource collection
|
|
185
|
+
* @param qualifierTypes - The reconstructed QualifierTypeCollector
|
|
186
|
+
* @returns The reconstructed QualifierCollector
|
|
187
|
+
* @internal
|
|
188
|
+
*/
|
|
189
|
+
_buildQualifiers(compiled, qualifierTypes) {
|
|
190
|
+
return mapResults(compiled.qualifiers.map((compiledQualifier) => {
|
|
191
|
+
return qualifierTypes.getAt(compiledQualifier.type).onSuccess((qualifierType) => {
|
|
192
|
+
return succeed({
|
|
193
|
+
name: compiledQualifier.name,
|
|
194
|
+
typeName: qualifierType.name,
|
|
195
|
+
defaultPriority: compiledQualifier.defaultPriority
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
})).onSuccess((qualifierDecls) => {
|
|
199
|
+
return QualifierCollector.create({
|
|
200
|
+
qualifierTypes,
|
|
201
|
+
qualifiers: qualifierDecls
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Reconstructs a ResourceTypeCollector from compiled data.
|
|
207
|
+
* @param compiled - The compiled resource collection
|
|
208
|
+
* @param resourceTypes - Map of resource type names to resource type objects
|
|
209
|
+
* @returns The reconstructed ResourceTypeCollector
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
_buildResourceTypes(compiled, resourceTypes) {
|
|
213
|
+
return mapResults(compiled.resourceTypes.map((compiledResourceType) => resourceTypes.get(compiledResourceType.name))).onSuccess((referencedResourceTypes) => {
|
|
214
|
+
return ResourceTypeCollector.create({
|
|
215
|
+
resourceTypes: referencedResourceTypes
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Reconstructs a ConditionCollector from compiled data.
|
|
221
|
+
* @param compiled - The compiled resource collection
|
|
222
|
+
* @param qualifiers - The reconstructed QualifierCollector
|
|
223
|
+
* @returns The reconstructed ConditionCollector
|
|
224
|
+
* @internal
|
|
225
|
+
*/
|
|
226
|
+
_buildConditions(compiled, qualifiers) {
|
|
227
|
+
var _a;
|
|
228
|
+
const errors = new MessageAggregator();
|
|
229
|
+
const conditionCollectorResult = ConditionCollector.create({ qualifiers });
|
|
230
|
+
/* c8 ignore next 3 - defensive coding for ConditionCollector creation failure */
|
|
231
|
+
if (conditionCollectorResult.isFailure()) {
|
|
232
|
+
return conditionCollectorResult;
|
|
233
|
+
}
|
|
234
|
+
const conditionCollector = conditionCollectorResult.value;
|
|
235
|
+
for (const [index, compiledCondition] of compiled.conditions.entries()) {
|
|
236
|
+
const qualifierResult = qualifiers.getAt(compiledCondition.qualifierIndex);
|
|
237
|
+
/* c8 ignore next 5 - defensive coding: invalid qualifier index should not occur in compiled data */
|
|
238
|
+
if (qualifierResult.isFailure()) {
|
|
239
|
+
errors.addMessage(`Invalid qualifier index ${compiledCondition.qualifierIndex} at condition ${index}: ${qualifierResult.message}`);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const qualifier = qualifierResult.value;
|
|
243
|
+
/* c8 ignore next 1 - not really testable atm as "matches" is the only supported operator */
|
|
244
|
+
const operator = (_a = compiledCondition.operator) !== null && _a !== void 0 ? _a : 'matches'; // Default to 'matches' if not provided
|
|
245
|
+
const conditionDecl = {
|
|
246
|
+
qualifierName: qualifier.name,
|
|
247
|
+
value: compiledCondition.value,
|
|
248
|
+
operator,
|
|
249
|
+
priority: compiledCondition.priority,
|
|
250
|
+
scoreAsDefault: compiledCondition.scoreAsDefault
|
|
251
|
+
};
|
|
252
|
+
const conditionResult = conditionCollector.validating.add(conditionDecl);
|
|
253
|
+
/* c8 ignore next 4 - defensive coding for condition addition failure */
|
|
254
|
+
if (conditionResult.isFailure()) {
|
|
255
|
+
errors.addMessage(`Failed to add condition at index ${index}: ${conditionResult.message}`);
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const condition = conditionResult.value;
|
|
259
|
+
// Validate that the assigned index matches our expected index
|
|
260
|
+
const expectedIndexResult = Convert.conditionIndex.convert(index);
|
|
261
|
+
/* c8 ignore next 4 - defensive coding for invalid condition index conversion */
|
|
262
|
+
if (expectedIndexResult.isFailure()) {
|
|
263
|
+
errors.addMessage(`Invalid condition index ${index}: ${expectedIndexResult.message}`);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const expectedIndex = expectedIndexResult.value;
|
|
267
|
+
/* c8 ignore next 5 - defensive coding for condition index mismatch */
|
|
268
|
+
if (condition.index !== expectedIndex) {
|
|
269
|
+
errors.addMessage(`Index mismatch at condition ${index}: expected ${expectedIndex}, got ${condition.index}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return errors.hasMessages ? fail(errors.toString()) : succeed(conditionCollector);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Reconstructs a ConditionSetCollector from compiled data.
|
|
276
|
+
* @param compiled - The compiled resource collection
|
|
277
|
+
* @param conditions - The reconstructed ConditionCollector
|
|
278
|
+
* @returns The reconstructed ConditionSetCollector
|
|
279
|
+
* @internal
|
|
280
|
+
*/
|
|
281
|
+
_buildConditionSets(compiled, conditions) {
|
|
282
|
+
const errors = new MessageAggregator();
|
|
283
|
+
const conditionSetCollectorResult = ConditionSetCollector.create({ conditions });
|
|
284
|
+
/* c8 ignore next 3 - defensive coding for ConditionSetCollector creation failure */
|
|
285
|
+
if (conditionSetCollectorResult.isFailure()) {
|
|
286
|
+
return conditionSetCollectorResult;
|
|
287
|
+
}
|
|
288
|
+
const conditionSetCollector = conditionSetCollectorResult.value;
|
|
289
|
+
for (const [index, compiledConditionSet] of compiled.conditionSets.entries()) {
|
|
290
|
+
const referencedConditionsResult = mapResults(compiledConditionSet.conditions.map((conditionIndex) => conditions.getAt(conditionIndex)));
|
|
291
|
+
if (referencedConditionsResult.isFailure()) {
|
|
292
|
+
errors.addMessage(`Failed to resolve conditions for condition set ${index}: ${referencedConditionsResult.message}`);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const referencedConditions = referencedConditionsResult.value;
|
|
296
|
+
const conditionSetResult = conditionSetCollector.validating.getOrAdd(referencedConditions);
|
|
297
|
+
/* c8 ignore next 4 - defensive coding for condition set addition failure */
|
|
298
|
+
if (conditionSetResult.isFailure()) {
|
|
299
|
+
errors.addMessage(`Failed to add condition set at index ${index}: ${conditionSetResult.message}`);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const conditionSet = conditionSetResult.value;
|
|
303
|
+
// Validate that the assigned index matches our expected index
|
|
304
|
+
const expectedIndexResult = Convert.conditionSetIndex.convert(index);
|
|
305
|
+
/* c8 ignore next 4 - defensive coding for invalid condition set index conversion */
|
|
306
|
+
if (expectedIndexResult.isFailure()) {
|
|
307
|
+
errors.addMessage(`Invalid condition set index ${index}: ${expectedIndexResult.message}`);
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
const expectedIndex = expectedIndexResult.value;
|
|
311
|
+
/* c8 ignore next 5 - defensive coding for condition set index mismatch */
|
|
312
|
+
if (conditionSet.index !== expectedIndex) {
|
|
313
|
+
errors.addMessage(`Index mismatch at condition set ${index}: expected ${expectedIndex}, got ${conditionSet.index}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return errors.hasMessages ? fail(errors.toString()) : succeed(conditionSetCollector);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Reconstructs an AbstractDecisionCollector from compiled data.
|
|
320
|
+
* @param compiled - The compiled resource collection
|
|
321
|
+
* @param conditionSets - The reconstructed ConditionSetCollector
|
|
322
|
+
* @returns The reconstructed AbstractDecisionCollector
|
|
323
|
+
* @internal
|
|
324
|
+
*/
|
|
325
|
+
_buildDecisions(compiled, conditionSets) {
|
|
326
|
+
const errors = new MessageAggregator();
|
|
327
|
+
const decisionCollectorResult = AbstractDecisionCollector.create({ conditionSets });
|
|
328
|
+
/* c8 ignore next 3 - defensive coding for AbstractDecisionCollector creation failure */
|
|
329
|
+
if (decisionCollectorResult.isFailure()) {
|
|
330
|
+
return decisionCollectorResult;
|
|
331
|
+
}
|
|
332
|
+
const decisionCollector = decisionCollectorResult.value;
|
|
333
|
+
for (const [index, compiledDecision] of compiled.decisions.entries()) {
|
|
334
|
+
const referencedConditionSetsResult = mapResults(compiledDecision.conditionSets.map((conditionSetIndex) => conditionSets.getAt(conditionSetIndex)));
|
|
335
|
+
/* c8 ignore next 5 - defensive coding: condition set resolution should not fail with valid compiled data */
|
|
336
|
+
if (referencedConditionSetsResult.isFailure()) {
|
|
337
|
+
errors.addMessage(`Failed to resolve condition sets for decision ${index}: ${referencedConditionSetsResult.message}`);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const referencedConditionSets = referencedConditionSetsResult.value;
|
|
341
|
+
const decisionResult = decisionCollector.validating.getOrAdd(referencedConditionSets);
|
|
342
|
+
/* c8 ignore next 4 - defensive coding for decision addition failure */
|
|
343
|
+
if (decisionResult.isFailure()) {
|
|
344
|
+
errors.addMessage(`Failed to add decision at index ${index}: ${decisionResult.message}`);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const decision = decisionResult.value;
|
|
348
|
+
// Validate that the assigned index matches our expected index
|
|
349
|
+
const expectedIndexResult = Convert.decisionIndex.convert(index);
|
|
350
|
+
/* c8 ignore next 4 - defensive coding for invalid decision index conversion */
|
|
351
|
+
if (expectedIndexResult.isFailure()) {
|
|
352
|
+
errors.addMessage(`Invalid decision index ${index}: ${expectedIndexResult.message}`);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const expectedIndex = expectedIndexResult.value;
|
|
356
|
+
/* c8 ignore next 5 - defensive coding for decision index mismatch */
|
|
357
|
+
if (decision.index !== expectedIndex) {
|
|
358
|
+
errors.addMessage(`Index mismatch at decision ${index}: expected ${expectedIndex}, got ${decision.index}`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return errors.hasMessages ? fail(errors.toString()) : succeed(decisionCollector);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Gets a candidate value from the collection.
|
|
365
|
+
* @param valueIndex - The index of the candidate value to get.
|
|
366
|
+
* @returns
|
|
367
|
+
*/
|
|
368
|
+
_getCandidateValue(valueIndex) {
|
|
369
|
+
/* c8 ignore next 3 - defensive coding: candidate value index should be valid in compiled data */
|
|
370
|
+
if (valueIndex < 0 || valueIndex >= this._candidateValues.length) {
|
|
371
|
+
return fail(`Invalid candidate value index ${valueIndex}`);
|
|
372
|
+
}
|
|
373
|
+
/* c8 ignore next 3 - defensive coding for invalid candidate value conversion */
|
|
374
|
+
if (!isJsonObject(this._candidateValues[valueIndex])) {
|
|
375
|
+
return fail(`${valueIndex}: candidate value not a JSON object.`);
|
|
376
|
+
}
|
|
377
|
+
return succeed(this._candidateValues[valueIndex]);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Reconstructs a ValidatingResultMap of resources from compiled data.
|
|
381
|
+
* @param compiled - The compiled resource collection
|
|
382
|
+
* @param resourceTypes - The reconstructed ResourceTypeCollector
|
|
383
|
+
* @param decisions - The reconstructed AbstractDecisionCollector
|
|
384
|
+
* @returns The reconstructed ValidatingResultMap of resources
|
|
385
|
+
* @internal
|
|
386
|
+
*/
|
|
387
|
+
_buildResources(compiled, resourceTypes, decisions) {
|
|
388
|
+
const errors = new MessageAggregator();
|
|
389
|
+
const resourceMap = new ValidatingResultMap({
|
|
390
|
+
converters: new Collections.KeyValueConverters({
|
|
391
|
+
key: Convert.resourceId,
|
|
392
|
+
/* c8 ignore next 3 - covered but coverage is confused */
|
|
393
|
+
value: (from) => {
|
|
394
|
+
return Validate.resource.validate(from);
|
|
395
|
+
}
|
|
396
|
+
})
|
|
397
|
+
});
|
|
398
|
+
for (const compiledResource of compiled.resources) {
|
|
399
|
+
const resourceType = resourceTypes
|
|
400
|
+
.getAt(compiledResource.type)
|
|
401
|
+
.aggregateError(errors, (message) => `Invalid resource type index ${compiledResource.type} for resource ${compiledResource.id}: ${message}`)
|
|
402
|
+
.orDefault();
|
|
403
|
+
/* c8 ignore next 3 - defensive coding: resource type index should be valid in compiled data */
|
|
404
|
+
if (resourceType === undefined) {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
const decision = decisions
|
|
408
|
+
.getAt(compiledResource.decision)
|
|
409
|
+
.aggregateError(errors, (message) => `Invalid decision index ${compiledResource.decision} for resource ${compiledResource.id}: ${message}`)
|
|
410
|
+
.orDefault();
|
|
411
|
+
/* c8 ignore next 3 - defensive coding: decision index should be valid in compiled data */
|
|
412
|
+
if (decision === undefined) {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
// Build candidates from compiled data
|
|
416
|
+
const candidateDecls = mapResults(compiledResource.candidates.map((compiledCandidate) => this._getCandidateValue(compiledCandidate.valueIndex)
|
|
417
|
+
.aggregateError(errors, (message) => `Failed to convert candidate JSON for resource ${compiledResource.id}: ${message}`)
|
|
418
|
+
.onSuccess((json) => {
|
|
419
|
+
return succeed({
|
|
420
|
+
json,
|
|
421
|
+
isPartial: compiledCandidate.isPartial,
|
|
422
|
+
mergeMethod: compiledCandidate.mergeMethod
|
|
423
|
+
});
|
|
424
|
+
})))
|
|
425
|
+
.aggregateError(errors, (message) => `Failed to convert candidate JSON for resource ${compiledResource.id}: ${message}`)
|
|
426
|
+
.orDefault();
|
|
427
|
+
/* c8 ignore next 3 - defensive coding: candidate JSON conversion should not fail with valid compiled data */
|
|
428
|
+
if (candidateDecls === undefined) {
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
// Create minimal candidates that implement IResourceCandidate
|
|
432
|
+
const candidates = candidateDecls.map((candidateDecl) => ({
|
|
433
|
+
json: candidateDecl.json,
|
|
434
|
+
isPartial: candidateDecl.isPartial,
|
|
435
|
+
mergeMethod: candidateDecl.mergeMethod
|
|
436
|
+
}));
|
|
437
|
+
const candidatesWithConditionSets = decision.candidates.map((baseCandidate, idx) => ({
|
|
438
|
+
conditionSet: baseCandidate.conditionSet,
|
|
439
|
+
value: candidates[idx].json
|
|
440
|
+
}));
|
|
441
|
+
const concreteDecision = ConcreteDecision.create({
|
|
442
|
+
decisions,
|
|
443
|
+
candidates: candidatesWithConditionSets
|
|
444
|
+
})
|
|
445
|
+
.aggregateError(errors, (message) => `Failed to create concrete decision for resource ${compiledResource.id}: ${message}`)
|
|
446
|
+
.orDefault();
|
|
447
|
+
/* c8 ignore next 3 - defensive coding for ConcreteDecision creation failure */
|
|
448
|
+
if (concreteDecision === undefined) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
const name = Helpers.getNameForResourceId(compiledResource.id)
|
|
452
|
+
.aggregateError(errors, (message) => `Failed to get name for resource ${compiledResource.id}: ${message}`)
|
|
453
|
+
.orDefault();
|
|
454
|
+
/* c8 ignore next 3 - defensive coding: name should be valid in compiled data */
|
|
455
|
+
if (name === undefined) {
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
// Create minimal resource that implements IResource
|
|
459
|
+
const resource = {
|
|
460
|
+
id: compiledResource.id,
|
|
461
|
+
name,
|
|
462
|
+
resourceType,
|
|
463
|
+
decision: concreteDecision,
|
|
464
|
+
candidates
|
|
465
|
+
};
|
|
466
|
+
resourceMap
|
|
467
|
+
.set(compiledResource.id, resource)
|
|
468
|
+
.aggregateError(errors, (message) => `Failed to add resource ${compiledResource.id}: ${message}`);
|
|
469
|
+
}
|
|
470
|
+
return errors.returnOrReport(succeed(resourceMap));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
//# sourceMappingURL=compiledResourceCollection.js.map
|