@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.
- package/README.md +794 -137
- package/dist/ts-res.d.ts +506 -33
- package/lib/packlets/common/convert.d.ts +10 -0
- package/lib/packlets/common/convert.js +11 -1
- package/lib/packlets/common/resources.d.ts +40 -1
- package/lib/packlets/common/validate/regularExpressions.d.ts +4 -0
- package/lib/packlets/common/validate/regularExpressions.js +5 -1
- package/lib/packlets/common/validate/resources.d.ts +35 -1
- package/lib/packlets/common/validate/resources.js +54 -0
- package/lib/packlets/config/convert.js +1 -1
- package/lib/packlets/config/json.d.ts +1 -1
- package/lib/packlets/qualifier-types/config/convert.d.ts +22 -1
- package/lib/packlets/qualifier-types/config/convert.js +67 -2
- package/lib/packlets/qualifier-types/config/json.d.ts +11 -2
- package/lib/packlets/qualifier-types/helpers.d.ts +1 -2
- package/lib/packlets/qualifier-types/languageQualifierType.d.ts +27 -1
- package/lib/packlets/qualifier-types/languageQualifierType.js +75 -0
- package/lib/packlets/qualifier-types/literalQualifierType.d.ts +30 -4
- package/lib/packlets/qualifier-types/literalQualifierType.js +77 -0
- package/lib/packlets/qualifier-types/literalValueHierarchy.d.ts +6 -10
- package/lib/packlets/qualifier-types/literalValueHierarchy.js +10 -0
- package/lib/packlets/qualifier-types/qualifierType.d.ts +28 -0
- package/lib/packlets/qualifier-types/territoryQualifierType.d.ts +28 -4
- package/lib/packlets/qualifier-types/territoryQualifierType.js +77 -0
- package/lib/packlets/resource-json/compiled/convert.js +2 -1
- package/lib/packlets/resource-json/compiled/json.d.ts +6 -2
- package/lib/packlets/resource-types/config/convert.js +3 -1
- package/lib/packlets/resource-types/config/json.d.ts +2 -0
- package/lib/packlets/resource-types/jsonResourceType.d.ts +10 -6
- package/lib/packlets/resource-types/jsonResourceType.js +7 -10
- package/lib/packlets/resource-types/resourceType.d.ts +20 -8
- package/lib/packlets/resource-types/resourceType.js +20 -16
- package/lib/packlets/resources/candidateValue.d.ts +92 -0
- package/lib/packlets/resources/candidateValue.js +128 -0
- package/lib/packlets/resources/candidateValueCollector.d.ts +55 -0
- package/lib/packlets/resources/candidateValueCollector.js +96 -0
- package/lib/packlets/resources/index.d.ts +2 -0
- package/lib/packlets/resources/index.js +2 -0
- package/lib/packlets/resources/resource.js +2 -2
- package/lib/packlets/resources/resourceBuilder.d.ts +6 -0
- package/lib/packlets/resources/resourceBuilder.js +3 -1
- package/lib/packlets/resources/resourceCandidate.d.ts +10 -1
- package/lib/packlets/resources/resourceCandidate.js +11 -5
- package/lib/packlets/resources/resourceManagerBuilder.d.ts +7 -0
- package/lib/packlets/resources/resourceManagerBuilder.js +13 -6
- package/lib/packlets/runtime/compiledResourceCollection.d.ts +20 -8
- package/lib/packlets/runtime/compiledResourceCollection.js +25 -4
- package/lib/packlets/runtime/resource-tree/common.d.ts +1 -1
- package/lib/packlets/runtime/resourceResolver.d.ts +39 -1
- package/lib/packlets/runtime/resourceResolver.js +45 -28
- package/lib/test/unit/qualifier-types/testQualifierType.d.ts +4 -0
- package/lib/test/unit/qualifier-types/testQualifierType.js +24 -0
- package/lib/test/unit/resource-types/derivedResourceType.test.d.ts +2 -0
- package/lib/test/unit/resource-types/testDerivedResourceType.d.ts +79 -0
- package/lib/test/unit/resource-types/testDerivedResourceType.js +113 -0
- package/lib/test/unit/resources/candidateValue.test.d.ts +2 -0
- package/lib/test/unit/resources/candidateValueCollector.test.d.ts +2 -0
- package/lib/test/unit/runtime/resourceResolver.coverage.test.d.ts +2 -0
- package/lib/test/unit/runtime/resourceResolver.newFeatures.test.d.ts +2 -0
- package/package.json +7 -7
- package/src/index.ts +0 -75
- package/src/packlets/bundle/bundleBuilder.ts +0 -159
- package/src/packlets/bundle/bundleLoader.ts +0 -157
- package/src/packlets/bundle/bundleNormalizer.ts +0 -173
- package/src/packlets/bundle/bundleUtils.ts +0 -142
- package/src/packlets/bundle/convert.ts +0 -87
- package/src/packlets/bundle/index.ts +0 -52
- package/src/packlets/bundle/model.ts +0 -138
- package/src/packlets/common/conditions.ts +0 -226
- package/src/packlets/common/convert.ts +0 -184
- package/src/packlets/common/helpers/conditions.ts +0 -84
- package/src/packlets/common/helpers/context.ts +0 -88
- package/src/packlets/common/helpers/index.ts +0 -26
- package/src/packlets/common/helpers/qualifierDefaultValues.ts +0 -91
- package/src/packlets/common/helpers/resources.ts +0 -96
- package/src/packlets/common/index.ts +0 -30
- package/src/packlets/common/resources.ts +0 -78
- package/src/packlets/common/validate/conditions.ts +0 -595
- package/src/packlets/common/validate/index.ts +0 -27
- package/src/packlets/common/validate/regularExpressions.ts +0 -92
- package/src/packlets/common/validate/resources.ts +0 -170
- package/src/packlets/conditions/condition.ts +0 -351
- package/src/packlets/conditions/conditionCollector.ts +0 -103
- package/src/packlets/conditions/conditionDecls.ts +0 -50
- package/src/packlets/conditions/conditionSet.ts +0 -318
- package/src/packlets/conditions/conditionSetCollector.ts +0 -127
- package/src/packlets/conditions/conditionSetDecls.ts +0 -41
- package/src/packlets/conditions/conditionToken.ts +0 -180
- package/src/packlets/conditions/convert/conditionSetDecls.ts +0 -72
- package/src/packlets/conditions/convert/decls.ts +0 -95
- package/src/packlets/conditions/convert/index.ts +0 -24
- package/src/packlets/conditions/index.ts +0 -33
- package/src/packlets/config/common.ts +0 -107
- package/src/packlets/config/configInitFactory.ts +0 -142
- package/src/packlets/config/convert.ts +0 -61
- package/src/packlets/config/index.ts +0 -30
- package/src/packlets/config/json.ts +0 -43
- package/src/packlets/config/predefined/default.ts +0 -150
- package/src/packlets/config/predefined/extended.ts +0 -198
- package/src/packlets/config/predefined/index.ts +0 -26
- package/src/packlets/config/systemConfiguration.ts +0 -217
- package/src/packlets/context/contextDecls.ts +0 -76
- package/src/packlets/context/contextToken.ts +0 -252
- package/src/packlets/context/convert/decls.ts +0 -126
- package/src/packlets/context/convert/index.ts +0 -23
- package/src/packlets/context/index.ts +0 -27
- package/src/packlets/decisions/abstractDecision.ts +0 -87
- package/src/packlets/decisions/abstractDecisionCollector.ts +0 -122
- package/src/packlets/decisions/candidate.ts +0 -101
- package/src/packlets/decisions/common.ts +0 -37
- package/src/packlets/decisions/concreteDecision.ts +0 -157
- package/src/packlets/decisions/decision.ts +0 -172
- package/src/packlets/decisions/index.ts +0 -28
- package/src/packlets/import/fsItem.ts +0 -212
- package/src/packlets/import/importContext.ts +0 -183
- package/src/packlets/import/importManager.ts +0 -187
- package/src/packlets/import/importable.ts +0 -107
- package/src/packlets/import/importers/collectionImporter.ts +0 -153
- package/src/packlets/import/importers/fsItemImporter.ts +0 -154
- package/src/packlets/import/importers/importer.ts +0 -58
- package/src/packlets/import/importers/index.ts +0 -27
- package/src/packlets/import/importers/jsonImporter.ts +0 -133
- package/src/packlets/import/importers/pathImporter.ts +0 -134
- package/src/packlets/import/index.ts +0 -29
- package/src/packlets/qualifier-types/config/convert.ts +0 -108
- package/src/packlets/qualifier-types/config/index.ts +0 -27
- package/src/packlets/qualifier-types/config/json.ts +0 -126
- package/src/packlets/qualifier-types/convert.ts +0 -77
- package/src/packlets/qualifier-types/helpers.ts +0 -94
- package/src/packlets/qualifier-types/index.ts +0 -34
- package/src/packlets/qualifier-types/languageQualifierType.ts +0 -143
- package/src/packlets/qualifier-types/literalQualifierType.ts +0 -244
- package/src/packlets/qualifier-types/literalValueHierarchy.ts +0 -332
- package/src/packlets/qualifier-types/qualifierType.ts +0 -360
- package/src/packlets/qualifier-types/qualifierTypeCollector.ts +0 -102
- package/src/packlets/qualifier-types/territoryQualifierType.ts +0 -255
- package/src/packlets/qualifiers/convert/decls.ts +0 -90
- package/src/packlets/qualifiers/convert/index.ts +0 -24
- package/src/packlets/qualifiers/convert/qualifier.ts +0 -55
- package/src/packlets/qualifiers/index.ts +0 -31
- package/src/packlets/qualifiers/qualifier.ts +0 -169
- package/src/packlets/qualifiers/qualifierCollector.ts +0 -194
- package/src/packlets/qualifiers/qualifierDecl.ts +0 -80
- package/src/packlets/qualifiers/qualifierDefaultValueDecls.ts +0 -57
- package/src/packlets/qualifiers/qualifierDefaultValueToken.ts +0 -230
- package/src/packlets/resource-json/compiled/common.ts +0 -35
- package/src/packlets/resource-json/compiled/convert.ts +0 -143
- package/src/packlets/resource-json/compiled/index.ts +0 -27
- package/src/packlets/resource-json/compiled/json.ts +0 -239
- package/src/packlets/resource-json/convert.ts +0 -236
- package/src/packlets/resource-json/helpers.ts +0 -284
- package/src/packlets/resource-json/index.ts +0 -33
- package/src/packlets/resource-json/json.ts +0 -295
- package/src/packlets/resource-json/normalized.ts +0 -211
- package/src/packlets/resource-json/resourceDeclCollection.ts +0 -117
- package/src/packlets/resource-json/resourceDeclContainer.ts +0 -48
- package/src/packlets/resource-json/resourceDeclTree.ts +0 -116
- package/src/packlets/resource-types/config/convert.ts +0 -36
- package/src/packlets/resource-types/config/index.ts +0 -27
- package/src/packlets/resource-types/config/json.ts +0 -30
- package/src/packlets/resource-types/helpers.ts +0 -44
- package/src/packlets/resource-types/index.ts +0 -30
- package/src/packlets/resource-types/jsonResourceType.ts +0 -104
- package/src/packlets/resource-types/resourceType.ts +0 -259
- package/src/packlets/resource-types/resourceTypeCollector.ts +0 -79
- package/src/packlets/resources/candidateReducer.ts +0 -331
- package/src/packlets/resources/common.ts +0 -114
- package/src/packlets/resources/index.ts +0 -28
- package/src/packlets/resources/resource.ts +0 -307
- package/src/packlets/resources/resourceBuilder.ts +0 -269
- package/src/packlets/resources/resourceCandidate.ts +0 -333
- package/src/packlets/resources/resourceManagerBuilder.ts +0 -1169
- package/src/packlets/runtime/cacheListener.ts +0 -113
- package/src/packlets/runtime/cacheMetrics.ts +0 -206
- package/src/packlets/runtime/compiledResourceCollection.ts +0 -607
- package/src/packlets/runtime/conditionSetResolutionResult.ts +0 -160
- package/src/packlets/runtime/context/contextQualifierProvider.ts +0 -117
- package/src/packlets/runtime/context/contextQualifierProviderValidator.ts +0 -281
- package/src/packlets/runtime/context/index.ts +0 -26
- package/src/packlets/runtime/context/simpleContextQualifierProvider.ts +0 -225
- package/src/packlets/runtime/context/validatingSimpleContextQualifierProvider.ts +0 -96
- package/src/packlets/runtime/iResourceManager.ts +0 -130
- package/src/packlets/runtime/index.ts +0 -35
- package/src/packlets/runtime/resource-tree/common.ts +0 -126
- package/src/packlets/runtime/resource-tree/index.ts +0 -26
- package/src/packlets/runtime/resource-tree/readOnlyResourceTree.ts +0 -342
- package/src/packlets/runtime/resource-tree/resourceTreeChildren.ts +0 -125
- package/src/packlets/runtime/resource-tree/resourceTreeChildrenValidator.ts +0 -187
- package/src/packlets/runtime/resourceResolver.ts +0 -626
- package/src/packlets/runtime/validate.ts +0 -63
- package/src/packlets/zip-archive/convert.ts +0 -121
- package/src/packlets/zip-archive/index.ts +0 -76
- package/src/packlets/zip-archive/json.ts +0 -91
- package/src/packlets/zip-archive/types.ts +0 -140
- package/src/packlets/zip-archive/zipArchiveCreator.ts +0 -229
- package/src/packlets/zip-archive/zipArchiveFormat.ts +0 -158
- package/src/packlets/zip-archive/zipArchiveLoader.ts +0 -370
|
@@ -1,217 +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 { captureResult, mapResults, Result, fail, succeed, omit } from '@fgv/ts-utils';
|
|
24
|
-
import * as QualifierTypes from '../qualifier-types';
|
|
25
|
-
import * as ResourceTypes from '../resource-types';
|
|
26
|
-
import { QualifierType, QualifierTypeCollector, ReadOnlyQualifierTypeCollector } from '../qualifier-types';
|
|
27
|
-
import { IReadOnlyQualifierCollector, QualifierCollector } from '../qualifiers';
|
|
28
|
-
import { ReadOnlyResourceTypeCollector, ResourceType, ResourceTypeCollector } from '../resource-types';
|
|
29
|
-
import { ISystemConfiguration } from './json';
|
|
30
|
-
import { systemConfiguration } from './convert';
|
|
31
|
-
import { JsonFile, sanitizeJsonObject } from '@fgv/ts-json-base';
|
|
32
|
-
import {
|
|
33
|
-
BuiltInQualifierTypeFactory,
|
|
34
|
-
BuiltInResourceTypeFactory,
|
|
35
|
-
IConfigInitFactory
|
|
36
|
-
} from './configInitFactory';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Parameters used to initialize a {@link Config.SystemConfiguration | SystemConfiguration}.
|
|
40
|
-
* @public
|
|
41
|
-
*/
|
|
42
|
-
export interface ISystemConfigurationInitParams {
|
|
43
|
-
/**
|
|
44
|
-
* Optional map of qualifier names to default values. If provided, qualifiers
|
|
45
|
-
* in the system configuration will be updated with these default values.
|
|
46
|
-
* Use `null` as the value to remove an existing default value.
|
|
47
|
-
*/
|
|
48
|
-
qualifierDefaultValues?: Record<string, string | null>; // eslint-disable-line @rushstack/no-new-null
|
|
49
|
-
qualifierTypeFactory?: IConfigInitFactory<QualifierTypes.Config.IAnyQualifierTypeConfig, QualifierType>;
|
|
50
|
-
resourceTypeFactory?: IConfigInitFactory<ResourceTypes.Config.IResourceTypeConfig, ResourceType>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Creates a copy of the provided {@link Config.Model.ISystemConfiguration | system configuration}
|
|
55
|
-
* with updated qualifier default values.
|
|
56
|
-
* @param config - The base {@link Config.Model.ISystemConfiguration | system configuration} to copy.
|
|
57
|
-
* @param qualifierDefaultValues - Map of qualifier names to default values. Use `null` to remove existing values.
|
|
58
|
-
* @returns `Success` with the updated {@link Config.Model.ISystemConfiguration | system configuration}
|
|
59
|
-
* if successful, `Failure` with an error message otherwise.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
export function updateSystemConfigurationQualifierDefaultValues(
|
|
63
|
-
config: ISystemConfiguration,
|
|
64
|
-
qualifierDefaultValues: Record<string, string | null> // eslint-disable-line @rushstack/no-new-null
|
|
65
|
-
): Result<ISystemConfiguration> {
|
|
66
|
-
// Create a copy of the config
|
|
67
|
-
return sanitizeJsonObject<ISystemConfiguration>(config).onSuccess((updatedConfig) => {
|
|
68
|
-
// Create a map of existing qualifier names for validation
|
|
69
|
-
const existingQualifierNames = new Set(updatedConfig.qualifiers.map((q) => q.name));
|
|
70
|
-
|
|
71
|
-
// Validate that all specified qualifiers exist in the configuration
|
|
72
|
-
for (const qualifierName of Object.keys(qualifierDefaultValues)) {
|
|
73
|
-
if (!existingQualifierNames.has(qualifierName)) {
|
|
74
|
-
return fail(`Qualifier '${qualifierName}' not found in system configuration`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Update qualifier default values
|
|
79
|
-
updatedConfig.qualifiers = updatedConfig.qualifiers.map((qualifier) => {
|
|
80
|
-
if (qualifier.name in qualifierDefaultValues) {
|
|
81
|
-
const newDefaultValue = qualifierDefaultValues[qualifier.name];
|
|
82
|
-
|
|
83
|
-
// Create a copy of the qualifier
|
|
84
|
-
const updatedQualifier = { ...qualifier };
|
|
85
|
-
|
|
86
|
-
if (newDefaultValue === null) {
|
|
87
|
-
// Remove the default value
|
|
88
|
-
delete updatedQualifier.defaultValue;
|
|
89
|
-
} else {
|
|
90
|
-
// Set the new default value
|
|
91
|
-
updatedQualifier.defaultValue = newDefaultValue;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return updatedQualifier;
|
|
95
|
-
}
|
|
96
|
-
return qualifier;
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
return succeed(updatedConfig);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* A system configuration for both runtime or build.
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
export class SystemConfiguration {
|
|
108
|
-
/**
|
|
109
|
-
* The name of this system configuration.
|
|
110
|
-
*/
|
|
111
|
-
public get name(): string | undefined {
|
|
112
|
-
return this._config.name;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* The description of this system configuration.
|
|
117
|
-
*/
|
|
118
|
-
public get description(): string | undefined {
|
|
119
|
-
return this._config.description;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* The {@link QualifierTypes.QualifierTypeCollector | qualifier types} that this system configuration uses.
|
|
124
|
-
*/
|
|
125
|
-
public readonly qualifierTypes: ReadOnlyQualifierTypeCollector;
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* The {@link QualifierTypes.QualifierTypeCollector | qualifier types} that this system configuration uses.
|
|
129
|
-
* @public
|
|
130
|
-
*/
|
|
131
|
-
public readonly qualifiers: IReadOnlyQualifierCollector;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* The {@link ResourceTypes.ResourceTypeCollector | resource types} that this system configuration uses.
|
|
135
|
-
*/
|
|
136
|
-
public readonly resourceTypes: ReadOnlyResourceTypeCollector;
|
|
137
|
-
|
|
138
|
-
private readonly _config: ISystemConfiguration;
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Constructs a new instance of a {@link Config.SystemConfiguration | SystemConfiguration} from the
|
|
142
|
-
* supplied {@link Config.Model.ISystemConfiguration | system configuration}.
|
|
143
|
-
* @param config - The {@link Config.Model.ISystemConfiguration | system configuration} to use.
|
|
144
|
-
* @public
|
|
145
|
-
*/
|
|
146
|
-
protected constructor(config: ISystemConfiguration, initParams?: ISystemConfigurationInitParams) {
|
|
147
|
-
this._config = config;
|
|
148
|
-
const qualifierTypeFactory = initParams?.qualifierTypeFactory ?? new BuiltInQualifierTypeFactory();
|
|
149
|
-
const resourceTypeFactory = initParams?.resourceTypeFactory ?? new BuiltInResourceTypeFactory();
|
|
150
|
-
|
|
151
|
-
this.qualifierTypes = QualifierTypeCollector.create({
|
|
152
|
-
qualifierTypes: mapResults(config.qualifierTypes.map((tc) => qualifierTypeFactory.create(tc))).orThrow()
|
|
153
|
-
}).orThrow();
|
|
154
|
-
|
|
155
|
-
this.qualifiers = QualifierCollector.create({
|
|
156
|
-
qualifierTypes: this.qualifierTypes,
|
|
157
|
-
qualifiers: config.qualifiers
|
|
158
|
-
}).orThrow();
|
|
159
|
-
|
|
160
|
-
this.resourceTypes = ResourceTypeCollector.create({
|
|
161
|
-
resourceTypes: mapResults(config.resourceTypes.map((rt) => resourceTypeFactory.create(rt))).orThrow()
|
|
162
|
-
}).orThrow();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Creates a new {@link Config.SystemConfiguration | SystemConfiguration} from the supplied
|
|
167
|
-
* {@link Config.Model.ISystemConfiguration | system configuration}.
|
|
168
|
-
* @param config - The {@link Config.Model.ISystemConfiguration | system configuration} to use.
|
|
169
|
-
* @param initParams - Optional {@link Config.ISystemConfigurationInitParams | initialization parameters}.
|
|
170
|
-
* @returns `Success` with the new {@link Config.SystemConfiguration | SystemConfiguration}
|
|
171
|
-
* if successful, `Failure` with an error message otherwise.
|
|
172
|
-
* @public
|
|
173
|
-
*/
|
|
174
|
-
public static create(
|
|
175
|
-
config: ISystemConfiguration,
|
|
176
|
-
initParams?: ISystemConfigurationInitParams
|
|
177
|
-
): Result<SystemConfiguration> {
|
|
178
|
-
if (initParams?.qualifierDefaultValues) {
|
|
179
|
-
return updateSystemConfigurationQualifierDefaultValues(
|
|
180
|
-
config,
|
|
181
|
-
initParams.qualifierDefaultValues
|
|
182
|
-
).onSuccess((updatedConfig) =>
|
|
183
|
-
captureResult(
|
|
184
|
-
() => new SystemConfiguration(updatedConfig, omit(initParams, ['qualifierDefaultValues']))
|
|
185
|
-
)
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
return captureResult(() => new SystemConfiguration(config, initParams));
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Loads a {@link Config.SystemConfiguration | SystemConfiguration} from a file.
|
|
193
|
-
* @param path - The path to the file to load.
|
|
194
|
-
* @returns `Success` with the {@link Config.SystemConfiguration | SystemConfiguration}
|
|
195
|
-
* if successful, `Failure` with an error message otherwise.
|
|
196
|
-
* @public
|
|
197
|
-
*/
|
|
198
|
-
public static loadFromFile(
|
|
199
|
-
path: string,
|
|
200
|
-
initParams?: ISystemConfigurationInitParams
|
|
201
|
-
): Result<SystemConfiguration> {
|
|
202
|
-
return JsonFile.convertJsonFileSync(path, systemConfiguration).onSuccess((config) =>
|
|
203
|
-
SystemConfiguration.create(config, initParams)
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Returns the {@link Config.Model.ISystemConfiguration | system configuration} that this
|
|
209
|
-
* {@link Config.SystemConfiguration | SystemConfiguration} was created from.
|
|
210
|
-
* @returns `Success` with the {@link Config.Model.ISystemConfiguration | system configuration}
|
|
211
|
-
* if successful, `Failure` with an error message otherwise.
|
|
212
|
-
* @public
|
|
213
|
-
*/
|
|
214
|
-
public getConfig(): Result<ISystemConfiguration> {
|
|
215
|
-
return systemConfiguration.convert(this._config);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
@@ -1,76 +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 { QualifierContextValue, QualifierName } from '../common';
|
|
24
|
-
import { Qualifier } from '../qualifiers';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Options to control matching of conditions against a context.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export interface IContextMatchOptions {
|
|
31
|
-
/**
|
|
32
|
-
* If true, then conditions which would otherwise yield
|
|
33
|
-
* {@link NoMatch | NoMatch} but have a defined {@link Conditions.Condition.scoreAsDefault | scoreAsDefault}
|
|
34
|
-
* will yield `scoreAsDefault`instead of `NoMatch`.
|
|
35
|
-
*/
|
|
36
|
-
acceptDefaultScore?: boolean;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* If true, then conditions for which a corresponding values is not present in the
|
|
40
|
-
* context being matched will yield `undefined` instead of `NoMatch`.
|
|
41
|
-
*/
|
|
42
|
-
partialContextMatch?: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Non-validated declaration of a single context qualifier value.
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
export interface IContextQualifierValueDecl {
|
|
50
|
-
qualifier: string;
|
|
51
|
-
value: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Non-validated declaration of a context, consisting of named
|
|
56
|
-
* values.
|
|
57
|
-
* @public
|
|
58
|
-
*/
|
|
59
|
-
export type IContextDecl = Record<string, string>;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Validated declaration of a single context qualifier value.
|
|
63
|
-
* @public
|
|
64
|
-
*/
|
|
65
|
-
export interface IValidatedContextQualifierValueDecl {
|
|
66
|
-
qualifier: Qualifier;
|
|
67
|
-
value: QualifierContextValue;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Validated declaration of a context, a record with strongly-typed
|
|
72
|
-
* {@link QualifierTypeName | qualifier type names} as keys and
|
|
73
|
-
* {@link QualifierContextValue | qualifier context values} as values.
|
|
74
|
-
* @public
|
|
75
|
-
*/
|
|
76
|
-
export type IValidatedContextDecl = Record<QualifierName, QualifierContextValue>;
|
|
@@ -1,252 +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 { mapResults, Result, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
-
import { Helpers as CommonHelpers, QualifierName, QualifierContextValue } from '../common';
|
|
25
|
-
import { IValidatedContextQualifierValueDecl, IValidatedContextDecl } from './contextDecls';
|
|
26
|
-
import { IReadOnlyQualifierCollector, Qualifier } from '../qualifiers';
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Helper class to parse and validate context tokens.
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
export class ContextTokens {
|
|
33
|
-
/**
|
|
34
|
-
* The {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} used to validate
|
|
35
|
-
* qualifier names and values.
|
|
36
|
-
*/
|
|
37
|
-
public readonly qualifiers: IReadOnlyQualifierCollector;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Constructs a new {@link Context.ContextTokens | ContextTokens } instance.
|
|
41
|
-
* @param qualifiers - the {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} to use
|
|
42
|
-
*/
|
|
43
|
-
public constructor(qualifiers: IReadOnlyQualifierCollector) {
|
|
44
|
-
this.qualifiers = qualifiers;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Parses a {@link ContextQualifierToken | context qualifier token} string and validates the parts
|
|
49
|
-
* against the qualifiers present in the {@link Context.ContextTokens.qualifiers | qualifier collector}.
|
|
50
|
-
* @param token - the token string to parse.
|
|
51
|
-
* @returns `Success` with the {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declaration}
|
|
52
|
-
* if successful, `Failure` with an error message if not.
|
|
53
|
-
*/
|
|
54
|
-
public parseContextQualifierToken(token: string): Result<IValidatedContextQualifierValueDecl> {
|
|
55
|
-
return ContextTokens.parseContextQualifierToken(token, this.qualifiers);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Parses a {@link ContextToken | context token} string and validates the parts
|
|
60
|
-
* against the qualifiers present in the {@link Context.ContextTokens.qualifiers | qualifier collector}.
|
|
61
|
-
* @param token - the token string to parse.
|
|
62
|
-
* @returns `Success` with the array of {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declarations}
|
|
63
|
-
* if successful, `Failure` with an error message if not.
|
|
64
|
-
*/
|
|
65
|
-
public parseContextToken(token: string): Result<IValidatedContextQualifierValueDecl[]> {
|
|
66
|
-
return ContextTokens.parseContextToken(token, this.qualifiers);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Validates the {@link Helpers.IContextTokenParts | parts} of a {@link ContextToken | context token}.
|
|
71
|
-
* @param parts - the parts to validate
|
|
72
|
-
* @returns `Success` with the validated declaration if successful, `Failure` with an error message if not.
|
|
73
|
-
*/
|
|
74
|
-
public validateContextTokenParts(
|
|
75
|
-
parts: CommonHelpers.IContextTokenParts
|
|
76
|
-
): Result<IValidatedContextQualifierValueDecl> {
|
|
77
|
-
return ContextTokens.validateContextTokenParts(parts, this.qualifiers);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Given a value, finds a single token-optional qualifier that matches the value.
|
|
82
|
-
* Fails if no qualifiers match, or if more than one qualifier matches.
|
|
83
|
-
* @param value - the value to match.
|
|
84
|
-
* @returns `Success` with the matching qualifier if successful, `Failure` with an error message if not.
|
|
85
|
-
*/
|
|
86
|
-
public findQualifierForValue(value: string): Result<Qualifier> {
|
|
87
|
-
return ContextTokens.findQualifierForValue(value, this.qualifiers);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Converts a {@link ContextToken | context token} to a validated partial context.
|
|
92
|
-
* @param token - the context token to convert
|
|
93
|
-
* @returns `Success` with the validated partial context if successful, `Failure` with an error message if not.
|
|
94
|
-
*/
|
|
95
|
-
public contextTokenToPartialContext(token: string): Result<IValidatedContextDecl> {
|
|
96
|
-
return ContextTokens.contextTokenToPartialContext(token, this.qualifiers);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Converts a validated partial context to a {@link ContextToken | context token}.
|
|
101
|
-
* @param context - the validated partial context to convert
|
|
102
|
-
* @returns `Success` with the context token if successful, `Failure` with an error message if not.
|
|
103
|
-
*/
|
|
104
|
-
public partialContextToContextToken(context: IValidatedContextDecl): Result<string> {
|
|
105
|
-
return ContextTokens.partialContextToContextToken(context);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Parses a {@link ContextQualifierToken | context qualifier token} and validates it against the qualifiers
|
|
110
|
-
* present in the supplied {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector}.
|
|
111
|
-
* @param token - the token string to parse.
|
|
112
|
-
* @param qualifiers - the {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} to use
|
|
113
|
-
* @returns `Success` with a {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declaration} if successful,
|
|
114
|
-
* `Failure` with an error message if not.
|
|
115
|
-
*/
|
|
116
|
-
public static parseContextQualifierToken(
|
|
117
|
-
token: string,
|
|
118
|
-
qualifiers: IReadOnlyQualifierCollector
|
|
119
|
-
): Result<IValidatedContextQualifierValueDecl> {
|
|
120
|
-
return CommonHelpers.parseContextQualifierTokenParts(token).onSuccess((parts) => {
|
|
121
|
-
return ContextTokens.validateContextTokenParts(parts, qualifiers);
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Parses a {@link ContextToken | context token} and validates it against the qualifiers
|
|
127
|
-
* present in the supplied {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector}.
|
|
128
|
-
* @param token - the token string to parse.
|
|
129
|
-
* @param qualifiers - the {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} to use
|
|
130
|
-
* @returns `Success` with an array of {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declarations}
|
|
131
|
-
* if successful, `Failure` with an error message if not
|
|
132
|
-
*/
|
|
133
|
-
public static parseContextToken(
|
|
134
|
-
token: string,
|
|
135
|
-
qualifiers: IReadOnlyQualifierCollector
|
|
136
|
-
): Result<IValidatedContextQualifierValueDecl[]> {
|
|
137
|
-
return CommonHelpers.parseContextTokenParts(token).onSuccess((parts) => {
|
|
138
|
-
return mapResults(parts.map((part) => ContextTokens.validateContextTokenParts(part, qualifiers)));
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Validates the parts of a context token against the qualifiers present in the supplied
|
|
144
|
-
* {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector}.
|
|
145
|
-
* @param parts - the {@link Helpers.IContextTokenParts | context token parts} to validate.
|
|
146
|
-
* @param qualifiers - the {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} used to
|
|
147
|
-
* validate qualifier names and values.
|
|
148
|
-
* @returns `Success` with a {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declaration} if successful,
|
|
149
|
-
* `Failure` with an error message if not.
|
|
150
|
-
*/
|
|
151
|
-
public static validateContextTokenParts(
|
|
152
|
-
parts: CommonHelpers.IContextTokenParts,
|
|
153
|
-
qualifiers: IReadOnlyQualifierCollector
|
|
154
|
-
): Result<IValidatedContextQualifierValueDecl> {
|
|
155
|
-
const qualifierLookup =
|
|
156
|
-
parts.qualifier === undefined
|
|
157
|
-
? ContextTokens.findQualifierForValue(parts.value, qualifiers)
|
|
158
|
-
: qualifiers.getByNameOrToken(parts.qualifier);
|
|
159
|
-
|
|
160
|
-
return qualifierLookup.onSuccess((qualifier) => {
|
|
161
|
-
return qualifier.type
|
|
162
|
-
.validateContextValue(parts.value)
|
|
163
|
-
.onSuccess((value) => {
|
|
164
|
-
return succeed({ qualifier, value });
|
|
165
|
-
})
|
|
166
|
-
.withDetail('failure', 'success');
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Given a value and a set of qualifiers, finds a single token-optional qualifier that matches the value.
|
|
172
|
-
* Fails if no qualifiers match, or if more than one qualifier matches.
|
|
173
|
-
* @param value - the value to match.
|
|
174
|
-
* @param qualifiers - the qualifiers to match against.
|
|
175
|
-
* @returns `Success` with the matching qualifier if successful, `Failure` with an error message if not.
|
|
176
|
-
*/
|
|
177
|
-
public static findQualifierForValue(
|
|
178
|
-
value: string,
|
|
179
|
-
qualifiers: IReadOnlyQualifierCollector
|
|
180
|
-
): Result<Qualifier> {
|
|
181
|
-
const matched: Qualifier[] = [];
|
|
182
|
-
for (const qualifier of qualifiers.values()) {
|
|
183
|
-
if (qualifier.tokenIsOptional && qualifier.validateContextValue(value).isSuccess()) {
|
|
184
|
-
matched.push(qualifier);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
if (matched.length === 0) {
|
|
188
|
-
return fail(`${value}: does not match any qualifier`);
|
|
189
|
-
}
|
|
190
|
-
if (matched.length > 1) {
|
|
191
|
-
return fail(`${value}: matches multiple qualifiers (${matched.map((q) => q.name).join(', ')})`);
|
|
192
|
-
}
|
|
193
|
-
return succeed(matched[0]);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Converts a {@link ContextToken | context token} to a validated partial context.
|
|
198
|
-
* @param token - the context token to convert
|
|
199
|
-
* @param qualifiers - the {@link Qualifiers.IReadOnlyQualifierCollector | qualifier collector} to use
|
|
200
|
-
* @returns `Success` with the validated partial context if successful, `Failure` with an error message if not.
|
|
201
|
-
*/
|
|
202
|
-
public static contextTokenToPartialContext(
|
|
203
|
-
token: string,
|
|
204
|
-
qualifiers: IReadOnlyQualifierCollector
|
|
205
|
-
): Result<IValidatedContextDecl> {
|
|
206
|
-
return ContextTokens.parseContextToken(token, qualifiers).onSuccess((qualifierValues) => {
|
|
207
|
-
const context: IValidatedContextDecl = {};
|
|
208
|
-
|
|
209
|
-
for (const { qualifier, value } of qualifierValues) {
|
|
210
|
-
const qualifierName = qualifier.name as QualifierName;
|
|
211
|
-
const contextValue = value as QualifierContextValue;
|
|
212
|
-
|
|
213
|
-
// Check for duplicate qualifiers
|
|
214
|
-
if (qualifierName in context) {
|
|
215
|
-
return fail(`${qualifier.name}: duplicate qualifier in context token`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
context[qualifierName] = contextValue;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return succeed(context);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Converts a validated partial context to a {@link ContextToken | context token}.
|
|
227
|
-
* @param context - the validated partial context to convert
|
|
228
|
-
* @returns `Success` with the context token if successful, `Failure` with an error message if not.
|
|
229
|
-
*/
|
|
230
|
-
public static partialContextToContextToken(context: IValidatedContextDecl): Result<string> {
|
|
231
|
-
const tokens: string[] = [];
|
|
232
|
-
|
|
233
|
-
for (const [qualifierName, contextValue] of Object.entries(context)) {
|
|
234
|
-
// Build token as "qualifier=value"
|
|
235
|
-
tokens.push(`${qualifierName}=${contextValue}`);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (tokens.length === 0) {
|
|
239
|
-
return succeed('');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const tokenString = tokens.join('|');
|
|
243
|
-
|
|
244
|
-
// Validate the generated token
|
|
245
|
-
return CommonHelpers.buildContextToken(
|
|
246
|
-
tokens.map((token) => {
|
|
247
|
-
const [qualifier, value] = token.split('=');
|
|
248
|
-
return { qualifier, value };
|
|
249
|
-
})
|
|
250
|
-
).onSuccess(() => succeed(tokenString));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
@@ -1,126 +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 { Converters, Failure, MessageAggregator, Result, succeed, Success } from '@fgv/ts-utils';
|
|
24
|
-
import {
|
|
25
|
-
IContextQualifierValueDecl,
|
|
26
|
-
IValidatedContextDecl,
|
|
27
|
-
IValidatedContextQualifierValueDecl
|
|
28
|
-
} from '../contextDecls';
|
|
29
|
-
import { IReadOnlyQualifierCollector } from '../../qualifiers';
|
|
30
|
-
|
|
31
|
-
/* eslint-disable @rushstack/typedef-var */
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Converter for a {@link Context.IContextQualifierValueDecl | context qualifier value declaration}.
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
export const contextQualifierValueDecl = Converters.strictObject<IContextQualifierValueDecl>({
|
|
38
|
-
qualifier: Converters.string,
|
|
39
|
-
value: Converters.string
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Converter for a {@link Context.IContextDecl | context declaration}.
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
export const contextDecl = Converters.recordOf(Converters.string);
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Context necessary to convert a {@link Context.IContextQualifierValueDecl | context qualifier value declaration}
|
|
50
|
-
* or {@link Context.IContextDecl | context declaration} to their
|
|
51
|
-
* equivalent - {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declaration}
|
|
52
|
-
* or {@link Context.IValidatedContextDecl | validated context declaration}, respectively.
|
|
53
|
-
* @public
|
|
54
|
-
*/
|
|
55
|
-
export interface IContextDeclConvertContext {
|
|
56
|
-
readonly qualifiers: IReadOnlyQualifierCollector;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Converter which constructs a {@link Context.IValidatedContextQualifierValueDecl | validated context qualifier value declaration}
|
|
61
|
-
* from a {@link Context.IContextQualifierValueDecl | context qualifier value declaration}, instantiating qualifiers by name
|
|
62
|
-
* from a supplied {@link Context.Convert.IContextDeclConvertContext | conversion context}.
|
|
63
|
-
* @public
|
|
64
|
-
*/
|
|
65
|
-
export const validatedContextQualifierValueDecl = Converters.generic<
|
|
66
|
-
IValidatedContextQualifierValueDecl,
|
|
67
|
-
IContextDeclConvertContext
|
|
68
|
-
>(
|
|
69
|
-
(
|
|
70
|
-
from: unknown,
|
|
71
|
-
__self,
|
|
72
|
-
context?: IContextDeclConvertContext
|
|
73
|
-
): Result<IValidatedContextQualifierValueDecl> => {
|
|
74
|
-
if (!context) {
|
|
75
|
-
return Failure.with('validatedContextQualifierValueDecl converter requires a context');
|
|
76
|
-
}
|
|
77
|
-
return contextQualifierValueDecl.convert(from).onSuccess((decl) => {
|
|
78
|
-
const { value: qualifier, message } = context.qualifiers.validating.get(decl.qualifier);
|
|
79
|
-
if (message !== undefined) {
|
|
80
|
-
return Failure.with(message);
|
|
81
|
-
}
|
|
82
|
-
return qualifier.validateContextValue(decl.value).onSuccess((value) => {
|
|
83
|
-
return Success.with({
|
|
84
|
-
qualifier,
|
|
85
|
-
value
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Converter which constructs a {@link Context.IValidatedContextDecl | validated context declaration}
|
|
94
|
-
* from a {@link Context.IContextDecl | context declaration}, instantiating qualifiers by name
|
|
95
|
-
* from a supplied {@link Context.Convert.IContextDeclConvertContext | conversion context}.
|
|
96
|
-
* @public
|
|
97
|
-
*/
|
|
98
|
-
export const validatedContextDecl = Converters.generic<IValidatedContextDecl, IContextDeclConvertContext>(
|
|
99
|
-
(from: unknown, __self, context?: IContextDeclConvertContext): Result<IValidatedContextDecl> => {
|
|
100
|
-
if (!context) {
|
|
101
|
-
/* c8 ignore next 2 - functional code path tested but coverage intermittently missed */
|
|
102
|
-
return Failure.with('validatedContextDecl converter requires a context');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return contextDecl.convert(from).onSuccess((decl) => {
|
|
106
|
-
const errors = new MessageAggregator();
|
|
107
|
-
const result: IValidatedContextDecl = {};
|
|
108
|
-
|
|
109
|
-
for (const key in decl) {
|
|
110
|
-
if (key in decl) {
|
|
111
|
-
const { value: qualifier } = context.qualifiers.validating.get(key).aggregateError(errors);
|
|
112
|
-
if (qualifier !== undefined) {
|
|
113
|
-
qualifier
|
|
114
|
-
.validateContextValue(decl[key])
|
|
115
|
-
.onSuccess((value) => {
|
|
116
|
-
result[qualifier.name] = value;
|
|
117
|
-
return succeed(value);
|
|
118
|
-
})
|
|
119
|
-
.aggregateError(errors);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return errors.returnOrReport(Success.with(result));
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
);
|