@fgv/ts-json 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/package.json +6 -6
- package/src/index.ts +0 -29
- package/src/packlets/context/compositeJsonMap.ts +0 -120
- package/src/packlets/context/contextHelpers.ts +0 -221
- package/src/packlets/context/index.ts +0 -33
- package/src/packlets/context/jsonContext.ts +0 -133
- package/src/packlets/converters/converters.ts +0 -117
- package/src/packlets/converters/index.ts +0 -26
- package/src/packlets/converters/jsonConverter.ts +0 -476
- package/src/packlets/diff/detailedDiff.ts +0 -585
- package/src/packlets/diff/index.ts +0 -24
- package/src/packlets/diff/threeWayDiff.ts +0 -420
- package/src/packlets/diff/utils.ts +0 -66
- package/src/packlets/editor/common.ts +0 -125
- package/src/packlets/editor/index.ts +0 -36
- package/src/packlets/editor/jsonEditor.ts +0 -523
- package/src/packlets/editor/jsonEditorRule.ts +0 -117
- package/src/packlets/editor/jsonEditorState.ts +0 -225
- package/src/packlets/editor/jsonReferenceMap.ts +0 -516
- package/src/packlets/editor/rules/conditional.ts +0 -222
- package/src/packlets/editor/rules/index.ts +0 -25
- package/src/packlets/editor/rules/multivalue.ts +0 -206
- package/src/packlets/editor/rules/references.ts +0 -177
- package/src/packlets/editor/rules/templates.ts +0 -159
|
@@ -1,516 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 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
|
-
DetailedResult,
|
|
25
|
-
Result,
|
|
26
|
-
captureResult,
|
|
27
|
-
fail,
|
|
28
|
-
failWithDetail,
|
|
29
|
-
mapResults,
|
|
30
|
-
recordToMap,
|
|
31
|
-
succeed,
|
|
32
|
-
succeedWithDetail
|
|
33
|
-
} from '@fgv/ts-utils';
|
|
34
|
-
|
|
35
|
-
import { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';
|
|
36
|
-
import { IJsonContext, IJsonReferenceMap, JsonReferenceMapFailureReason } from '../context';
|
|
37
|
-
import { JsonEditor } from './jsonEditor';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Options for creating a {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} object.
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
export interface IReferenceMapKeyPolicyValidateOptions {
|
|
44
|
-
/**
|
|
45
|
-
* If `true`, the validator coerces keys to some valid value.
|
|
46
|
-
* If `false`, invalid keys cause an error.
|
|
47
|
-
*/
|
|
48
|
-
makeValid?: boolean;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Policy object responsible for validating or correcting
|
|
53
|
-
* keys in a {@link IJsonReferenceMap | reference map}.
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
export class ReferenceMapKeyPolicy<T> {
|
|
57
|
-
/**
|
|
58
|
-
* @internal
|
|
59
|
-
*/
|
|
60
|
-
protected readonly _defaultOptions?: IReferenceMapKeyPolicyValidateOptions;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @internal
|
|
64
|
-
*/
|
|
65
|
-
protected readonly _isValid: (key: string, item?: T) => boolean;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Constructs a new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy}.
|
|
69
|
-
* @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options}
|
|
70
|
-
* used to construct the {@link ReferenceMapKeyPolicy}.
|
|
71
|
-
* @param isValid - An optional predicate to test a supplied key for validity.
|
|
72
|
-
*/
|
|
73
|
-
public constructor(
|
|
74
|
-
options?: IReferenceMapKeyPolicyValidateOptions,
|
|
75
|
-
isValid?: (key: string, item?: T) => boolean
|
|
76
|
-
) {
|
|
77
|
-
this._defaultOptions = options;
|
|
78
|
-
this._isValid = isValid ?? ReferenceMapKeyPolicy.defaultKeyPredicate;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* The static default key name validation predicate rejects keys that contain
|
|
83
|
-
* mustache templates or which start with the default conditional prefix
|
|
84
|
-
* `'?'`.
|
|
85
|
-
* @param key - The key to test.
|
|
86
|
-
* @returns `true` if the key is valid, `false` otherwise.
|
|
87
|
-
*/
|
|
88
|
-
public static defaultKeyPredicate(key: string): boolean {
|
|
89
|
-
return key.length > 0 && !key.includes('{{') && !key.startsWith('?');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Determines if a supplied key and item are valid according to the current policy.
|
|
94
|
-
* @param key - The key to be tested.
|
|
95
|
-
* @param item - The item to be tested.
|
|
96
|
-
* @returns `true` if the key and value are valid, `false` otherwise.
|
|
97
|
-
*/
|
|
98
|
-
public isValid(key: string, item?: T): boolean {
|
|
99
|
-
return this._isValid(key, item);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Determines if a supplied key and item are valid according to the current policy.
|
|
104
|
-
* @param key - The key to be tested.
|
|
105
|
-
* @param item - The item to be tested.
|
|
106
|
-
* @returns `Success` with the key if valid, `Failure` with an error message if invalid.
|
|
107
|
-
*/
|
|
108
|
-
public validate(key: string, item?: T, __options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {
|
|
109
|
-
return this.isValid(key, item) ? succeed(key) : fail(`${key}: invalid key`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Validates an array of entries using the validation rules for this policy.
|
|
114
|
-
* @param items - The array of entries to be validated.
|
|
115
|
-
* @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control
|
|
116
|
-
* validation.
|
|
117
|
-
* @returns `Success` with an array of validated entries, or `Failure` with an error message
|
|
118
|
-
* if validation fails.
|
|
119
|
-
*/
|
|
120
|
-
public validateItems(
|
|
121
|
-
items: [string, T][],
|
|
122
|
-
options?: IReferenceMapKeyPolicyValidateOptions
|
|
123
|
-
): Result<[string, T][]> {
|
|
124
|
-
return mapResults(
|
|
125
|
-
items.map((item) => {
|
|
126
|
-
return this.validate(...item, options).onSuccess((valid) => {
|
|
127
|
-
return succeed([valid, item[1]]);
|
|
128
|
-
});
|
|
129
|
-
})
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Validates a `Map\<string, T\>` using the validation rules for this policy.
|
|
135
|
-
* @param items - The `Map\<string, T\>` to be validated.
|
|
136
|
-
* @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to control
|
|
137
|
-
* validation.
|
|
138
|
-
* @returns `Success` with a new `Map\<string, T\>`, or `Failure` with an error message
|
|
139
|
-
* if validation fails.
|
|
140
|
-
*/
|
|
141
|
-
public validateMap(
|
|
142
|
-
map: Map<string, T>,
|
|
143
|
-
options?: IReferenceMapKeyPolicyValidateOptions
|
|
144
|
-
): Result<Map<string, T>> {
|
|
145
|
-
return this.validateItems(Array.from(map.entries()), options).onSuccess((valid) => {
|
|
146
|
-
return captureResult(() => new Map(valid));
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* A {@link PrefixKeyPolicy | PrefixKeyPolicy} enforces that all keys start with a supplied
|
|
153
|
-
* prefix, optionally adding the prefix as necessary.
|
|
154
|
-
* @public
|
|
155
|
-
*/
|
|
156
|
-
export class PrefixKeyPolicy<T> extends ReferenceMapKeyPolicy<T> {
|
|
157
|
-
/**
|
|
158
|
-
* The string prefix to be enforced by this policy.
|
|
159
|
-
*/
|
|
160
|
-
public readonly prefix: string;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy}.
|
|
164
|
-
* @param prefix - The string prefix to be enforced or applied.
|
|
165
|
-
* @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to
|
|
166
|
-
* configure the policy.
|
|
167
|
-
*/
|
|
168
|
-
public constructor(prefix: string, options?: IReferenceMapKeyPolicyValidateOptions) {
|
|
169
|
-
super(options);
|
|
170
|
-
this.prefix = prefix;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Determines if a key is valid according to policy.
|
|
175
|
-
* @param key - The key to be tested.
|
|
176
|
-
* @param __item - The item to be tested.
|
|
177
|
-
* @returns `true` if the key starts with the expected prefix, `false` otherwise.
|
|
178
|
-
*/
|
|
179
|
-
public isValid(key: string, __item?: T): boolean {
|
|
180
|
-
return (
|
|
181
|
-
key.startsWith(this.prefix) && key !== this.prefix && ReferenceMapKeyPolicy.defaultKeyPredicate(key)
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Determines if a key is valid according to policy, optionally coercing to a valid value by
|
|
187
|
-
* adding the required prefix.
|
|
188
|
-
* @param key - The key to be tested.
|
|
189
|
-
* @param item - The item which corresponds to the key.
|
|
190
|
-
* @param options - Optional {@link IReferenceMapKeyPolicyValidateOptions | options} to guide
|
|
191
|
-
* validation.
|
|
192
|
-
* @returns `Success` with a valid key name if the supplied key is valid or if `makeValid` is set
|
|
193
|
-
* in the policy options. Returns `Failure` with an error message if an error occurs.
|
|
194
|
-
*/
|
|
195
|
-
public validate(key: string, item?: T, options?: IReferenceMapKeyPolicyValidateOptions): Result<string> {
|
|
196
|
-
/* c8 ignore next */
|
|
197
|
-
const makeValid = (options ?? this._defaultOptions)?.makeValid === true;
|
|
198
|
-
if (this.isValid(key, item)) {
|
|
199
|
-
return succeed(key);
|
|
200
|
-
} else if (makeValid && ReferenceMapKeyPolicy.defaultKeyPredicate(key)) {
|
|
201
|
-
return succeed(`${this.prefix}${key}`);
|
|
202
|
-
}
|
|
203
|
-
return fail(`${key}: invalid key`);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Type representing either a `Map\<string, T\>` or a `Record\<string, T\>`.
|
|
209
|
-
* @public
|
|
210
|
-
*/
|
|
211
|
-
export type MapOrRecord<T> = Map<string, T> | Record<string, T>;
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Abstract base class with common functionality for simple
|
|
215
|
-
* {@link IJsonReferenceMap | reference map} implementations.
|
|
216
|
-
* @public
|
|
217
|
-
*/
|
|
218
|
-
export abstract class SimpleJsonMapBase<T> implements IJsonReferenceMap {
|
|
219
|
-
/**
|
|
220
|
-
* The {@link ReferenceMapKeyPolicy | key policy} in effect for this map.
|
|
221
|
-
* @internal
|
|
222
|
-
*/
|
|
223
|
-
protected readonly _keyPolicy: ReferenceMapKeyPolicy<T>;
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* A map containing keys and values already present in this map.
|
|
227
|
-
* @internal
|
|
228
|
-
*/
|
|
229
|
-
protected readonly _values: Map<string, T>;
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* An optional {@link IJsonContext | IJsonContext} used for any conversions
|
|
233
|
-
* involving items in this map.
|
|
234
|
-
* @internal
|
|
235
|
-
*/
|
|
236
|
-
protected readonly _context?: IJsonContext;
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Constructs a new {@link SimpleJsonMap | SimpleJsonMap}.
|
|
240
|
-
* @param values - Initial values for the map.
|
|
241
|
-
* @param context - An optional {@link IJsonContext | IJsonContext} used for any conversions
|
|
242
|
-
* involving items in this map.
|
|
243
|
-
* @param keyPolicy - The {@link ReferenceMapKeyPolicy | key policy} to use for this map.
|
|
244
|
-
* @internal
|
|
245
|
-
*/
|
|
246
|
-
protected constructor(
|
|
247
|
-
values?: MapOrRecord<T>,
|
|
248
|
-
context?: IJsonContext,
|
|
249
|
-
keyPolicy?: ReferenceMapKeyPolicy<T>
|
|
250
|
-
) {
|
|
251
|
-
values = SimpleJsonMapBase._toMap(values).orThrow();
|
|
252
|
-
this._keyPolicy = keyPolicy ?? new ReferenceMapKeyPolicy();
|
|
253
|
-
this._values = this._keyPolicy.validateMap(values).orThrow();
|
|
254
|
-
this._context = context;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Returns a `Map\<string, T\>` derived from a supplied {@link MapOrRecord | MapOrRecord}
|
|
259
|
-
* @param values - The {@link MapOrRecord | MapOrRecord} to be returned as a map.
|
|
260
|
-
* @returns `Success` with the corresponding `Map\<string, T\>` or `Failure` with a
|
|
261
|
-
* message if an error occurs.
|
|
262
|
-
* @internal
|
|
263
|
-
*/
|
|
264
|
-
protected static _toMap<T>(values?: MapOrRecord<T>): Result<Map<string, T>> {
|
|
265
|
-
if (values === undefined) {
|
|
266
|
-
return captureResult(() => new Map<string, T>());
|
|
267
|
-
} else if (!(values instanceof Map)) {
|
|
268
|
-
return recordToMap(values, (__k, v) => succeed(v));
|
|
269
|
-
}
|
|
270
|
-
return succeed(values);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Determine if a key might be valid for this map but does not determine if key actually
|
|
275
|
-
* exists. Allows key range to be constrained.
|
|
276
|
-
* @param key - key to be tested
|
|
277
|
-
* @returns `true` if the key is in the valid range, `false` otherwise.
|
|
278
|
-
*/
|
|
279
|
-
public keyIsInRange(key: string): boolean {
|
|
280
|
-
return this._keyPolicy.isValid(key);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Determines if an entry with the specified key actually exists in the map.
|
|
285
|
-
* @param key - key to be tested
|
|
286
|
-
* @returns `true` if an object with the specified key exists, `false` otherwise.
|
|
287
|
-
*/
|
|
288
|
-
public has(key: string): boolean {
|
|
289
|
-
return this._values.has(key);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Gets a `JsonObject` specified by key.
|
|
294
|
-
* @param key - key of the object to be retrieved
|
|
295
|
-
* @param context - optional {@link IJsonContext | JSON context} used to format the
|
|
296
|
-
* returned object.
|
|
297
|
-
* @returns {@link ts-utils#Success | `Success`} with the formatted object if successful.
|
|
298
|
-
* {@link ts-utils#Failure | `Failure`} with detail 'unknown' if no such object exists,
|
|
299
|
-
* or {@link ts-utils#Failure | `Failure`} with detail 'error' if the object was found
|
|
300
|
-
* but could not be formatted.
|
|
301
|
-
*/
|
|
302
|
-
public getJsonObject(
|
|
303
|
-
key: string,
|
|
304
|
-
context?: IJsonContext
|
|
305
|
-
): DetailedResult<JsonObject, JsonReferenceMapFailureReason> {
|
|
306
|
-
return this.getJsonValue(key, context).onSuccess((jv) => {
|
|
307
|
-
if (!isJsonObject(jv)) {
|
|
308
|
-
return failWithDetail(`${key}: not an object`, 'error');
|
|
309
|
-
}
|
|
310
|
-
return succeedWithDetail(jv);
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Gets a `JsonValue` specified by key.
|
|
316
|
-
* @param key - key of the value to be retrieved
|
|
317
|
-
* @param context - Optional {@link IJsonContext | JSON context} used to format the value
|
|
318
|
-
* @returns Success with the formatted object if successful. Failure with detail 'unknown'
|
|
319
|
-
* if no such object exists, or failure with detail 'error' if the object was found but
|
|
320
|
-
* could not be formatted.
|
|
321
|
-
*/
|
|
322
|
-
// eslint-disable-next-line no-use-before-define
|
|
323
|
-
public abstract getJsonValue(
|
|
324
|
-
key: string,
|
|
325
|
-
context?: IJsonContext
|
|
326
|
-
): DetailedResult<JsonValue, JsonReferenceMapFailureReason>;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Initialization options for a {@link SimpleJsonMap | SimpleJsonMap}.
|
|
331
|
-
* @public
|
|
332
|
-
*/
|
|
333
|
-
export interface ISimpleJsonMapOptions {
|
|
334
|
-
keyPolicy?: ReferenceMapKeyPolicy<JsonValue>;
|
|
335
|
-
editor?: JsonEditor;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* A {@link SimpleJsonMap | SimpleJsonMap } presents a view of a simple map
|
|
340
|
-
* of JSON values.
|
|
341
|
-
* @public
|
|
342
|
-
*/
|
|
343
|
-
export class SimpleJsonMap extends SimpleJsonMapBase<JsonValue> {
|
|
344
|
-
/**
|
|
345
|
-
* @internal
|
|
346
|
-
*/
|
|
347
|
-
protected _editor?: JsonEditor;
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Constructs a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects
|
|
351
|
-
* @param values - A string-keyed `Map` or `Record` of the `JsonValue`
|
|
352
|
-
* to be returned.
|
|
353
|
-
* @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.
|
|
354
|
-
* @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.
|
|
355
|
-
* @public
|
|
356
|
-
*/
|
|
357
|
-
protected constructor(
|
|
358
|
-
values?: MapOrRecord<JsonValue>,
|
|
359
|
-
context?: IJsonContext,
|
|
360
|
-
options?: ISimpleJsonMapOptions
|
|
361
|
-
) {
|
|
362
|
-
super(values, context, options?.keyPolicy);
|
|
363
|
-
this._editor = options?.editor;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Creates a new {@link SimpleJsonMap | SimpleJsonMap} from the supplied objects
|
|
368
|
-
* @param values - A string-keyed `Map` or `Record` of the `JsonValue`
|
|
369
|
-
* to be returned.
|
|
370
|
-
* @param context - Optional {@link IJsonContext | IJsonContext} used to format returned values.
|
|
371
|
-
* @param options - Optional {@link ISimpleJsonMapOptions | ISimpleJsonMapOptions} for initialization.
|
|
372
|
-
* @returns `Success` with a {@link SimpleJsonMap | SimpleJsonMap} or `Failure` with a message if
|
|
373
|
-
* an error occurs.
|
|
374
|
-
*/
|
|
375
|
-
public static createSimple(
|
|
376
|
-
values?: MapOrRecord<JsonValue>,
|
|
377
|
-
context?: IJsonContext,
|
|
378
|
-
options?: ISimpleJsonMapOptions
|
|
379
|
-
): Result<SimpleJsonMap> {
|
|
380
|
-
return captureResult(() => new SimpleJsonMap(values, context, options));
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* Gets a `JsonValue` specified by key.
|
|
385
|
-
* @param key - key of the value to be retrieved
|
|
386
|
-
* @param context - Optional {@link IJsonContext | JSON context} used to format the value
|
|
387
|
-
* @returns Success with the formatted object if successful. Failure with detail 'unknown'
|
|
388
|
-
* if no such object exists, or failure with detail 'error' if the object was found but
|
|
389
|
-
* could not be formatted.
|
|
390
|
-
*/
|
|
391
|
-
public getJsonValue(
|
|
392
|
-
key: string,
|
|
393
|
-
context?: IJsonContext
|
|
394
|
-
): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {
|
|
395
|
-
context = context ?? this._context;
|
|
396
|
-
const value = this._values.get(key);
|
|
397
|
-
if (!value) {
|
|
398
|
-
return failWithDetail(`${key}: JSON value not found`, 'unknown');
|
|
399
|
-
}
|
|
400
|
-
return this._clone(value, context);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* @internal
|
|
405
|
-
*/
|
|
406
|
-
protected _clone(
|
|
407
|
-
value: JsonValue,
|
|
408
|
-
context?: IJsonContext
|
|
409
|
-
): DetailedResult<JsonValue, JsonReferenceMapFailureReason> {
|
|
410
|
-
if (!this._editor) {
|
|
411
|
-
const result = JsonEditor.create();
|
|
412
|
-
/* c8 ignore next 3 - nearly impossible to reproduce */
|
|
413
|
-
if (result.isFailure()) {
|
|
414
|
-
return failWithDetail(result.message, 'error');
|
|
415
|
-
}
|
|
416
|
-
this._editor = result.value;
|
|
417
|
-
}
|
|
418
|
-
return this._editor.clone(value, context).withFailureDetail('error');
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Initialization options for a {@link PrefixedJsonMap | PrefixedJsonMap}
|
|
424
|
-
* @public
|
|
425
|
-
*/
|
|
426
|
-
export interface IKeyPrefixOptions {
|
|
427
|
-
/**
|
|
428
|
-
* Indicates whether the prefix should be added automatically as needed (default true)
|
|
429
|
-
*/
|
|
430
|
-
addPrefix?: boolean;
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* The prefix to be enforced
|
|
434
|
-
*/
|
|
435
|
-
prefix: string;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* A {@link PrefixedJsonMap | PrefixedJsonMap} enforces a supplied prefix for all contained values,
|
|
440
|
-
* optionally adding the prefix as necessary (default `true`).
|
|
441
|
-
* @public
|
|
442
|
-
*/
|
|
443
|
-
export class PrefixedJsonMap extends SimpleJsonMap {
|
|
444
|
-
/**
|
|
445
|
-
* Constructs a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values
|
|
446
|
-
* @param prefix - A string prefix to be enforced for and added to key names as necessary
|
|
447
|
-
* @param values - A string-keyed Map or Record of the `JsonValue` to be returned
|
|
448
|
-
* @param context - Optional {@link IJsonContext | JSON Context} used to format returned values
|
|
449
|
-
* @param editor - Optional {@link JsonEditor | JsonEditor} used to format returned values
|
|
450
|
-
* @public
|
|
451
|
-
*/
|
|
452
|
-
protected constructor(
|
|
453
|
-
values?: MapOrRecord<JsonValue>,
|
|
454
|
-
context?: IJsonContext,
|
|
455
|
-
options?: ISimpleJsonMapOptions
|
|
456
|
-
) {
|
|
457
|
-
super(values, context, options);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values
|
|
462
|
-
* @param prefix - A string prefix to be enforced for and added to key names as necessary
|
|
463
|
-
* @param values - A string-keyed Map or Record of the `JsonValue` to be returned
|
|
464
|
-
* @param context - Optional {@link IJsonContext | JSON Context} used to format returned values
|
|
465
|
-
* @param editor - Optional {@link JsonEditor | JsonEditor} used to format returned values
|
|
466
|
-
* @returns `Success` with a {@link PrefixedJsonMap | PrefixedJsonMap} or `Failure` with a message
|
|
467
|
-
* if an error occurs.
|
|
468
|
-
*/
|
|
469
|
-
public static createPrefixed(
|
|
470
|
-
prefix: string,
|
|
471
|
-
values?: MapOrRecord<JsonValue>,
|
|
472
|
-
context?: IJsonContext,
|
|
473
|
-
editor?: JsonEditor
|
|
474
|
-
): Result<PrefixedJsonMap>;
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Creates a new {@link PrefixedJsonMap | PrefixedJsonMap} from the supplied values
|
|
478
|
-
* @param prefixOptions - A KeyPrefixOptions indicating the prefix to enforce and whether that prefix should
|
|
479
|
-
* be added automatically if necessary (default true)
|
|
480
|
-
* @param values - A string-keyed Map or record of the `JsonValue` to be returned
|
|
481
|
-
* @param context - Optional {@link IJsonContext | JSON Context} used to format returned values
|
|
482
|
-
* @param editor - Optional {@link JsonEditor | JsonEditor} used to format returned values
|
|
483
|
-
*/
|
|
484
|
-
public static createPrefixed(
|
|
485
|
-
prefixOptions: IKeyPrefixOptions,
|
|
486
|
-
values?: MapOrRecord<JsonValue>,
|
|
487
|
-
context?: IJsonContext,
|
|
488
|
-
editor?: JsonEditor
|
|
489
|
-
): Result<PrefixedJsonMap>;
|
|
490
|
-
public static createPrefixed(
|
|
491
|
-
prefixOptions: string | IKeyPrefixOptions,
|
|
492
|
-
values?: MapOrRecord<JsonValue>,
|
|
493
|
-
context?: IJsonContext,
|
|
494
|
-
editor?: JsonEditor
|
|
495
|
-
): Result<PrefixedJsonMap> {
|
|
496
|
-
return captureResult(
|
|
497
|
-
() => new PrefixedJsonMap(values, context, { keyPolicy: this._toPolicy(prefixOptions), editor })
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
/**
|
|
502
|
-
* Constructs a new {@link PrefixKeyPolicy | PrefixKeyPolicy} from a supplied prefix
|
|
503
|
-
* or set of {@link IKeyPrefixOptions | prefix options}.
|
|
504
|
-
* @param prefixOptions - The prefix or {@link IKeyPrefixOptions | prefix options} or options
|
|
505
|
-
* for the new policy.
|
|
506
|
-
* @returns A new {@link ReferenceMapKeyPolicy | ReferenceMapKeyPolicy} which enforces the
|
|
507
|
-
* supplied prefix or options.
|
|
508
|
-
* @internal
|
|
509
|
-
*/
|
|
510
|
-
protected static _toPolicy(prefixOptions: string | IKeyPrefixOptions): ReferenceMapKeyPolicy<JsonValue> {
|
|
511
|
-
if (typeof prefixOptions === 'string') {
|
|
512
|
-
return new PrefixKeyPolicy(prefixOptions, { makeValid: true });
|
|
513
|
-
}
|
|
514
|
-
return new PrefixKeyPolicy(prefixOptions.prefix, { makeValid: prefixOptions.addPrefix !== false });
|
|
515
|
-
}
|
|
516
|
-
}
|