@fgv/ts-json 5.0.0-2 → 5.0.0-21
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/.vscode/launch.json +16 -0
- package/.vscode/settings.json +32 -0
- package/config/api-extractor.json +343 -0
- package/config/rig.json +16 -0
- package/dist/ts-json.d.ts +719 -28
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +25 -0
- package/lib/packlets/diff/detailedDiff.d.ts +375 -0
- package/lib/packlets/diff/detailedDiff.js +342 -0
- package/lib/packlets/diff/index.d.ts +3 -0
- package/lib/packlets/diff/index.js +40 -0
- package/lib/packlets/diff/threeWayDiff.d.ts +263 -0
- package/lib/packlets/diff/threeWayDiff.js +261 -0
- package/lib/packlets/diff/utils.d.ts +6 -0
- package/lib/packlets/diff/utils.js +62 -0
- package/lib/packlets/editor/common.d.ts +6 -0
- package/lib/packlets/editor/jsonEditor.d.ts +57 -28
- package/lib/packlets/editor/jsonEditor.js +101 -32
- package/lib/test/legacy/jsonConverter.conditional.test.d.ts +2 -0
- package/lib/test/legacy/jsonEditor/rules/conditional.test.d.ts +2 -0
- package/lib/test/legacy/jsonEditor/rules/multivalue.test.d.ts +2 -0
- package/lib/test/legacy/jsonEditor/rules/references.test.d.ts +2 -0
- package/lib/test/legacy/jsonEditor/rules/templates.test.d.ts +2 -0
- package/lib/test/unit/contextHelpers.test.d.ts +2 -0
- package/lib/test/unit/converters.test.d.ts +2 -0
- package/lib/test/unit/diff/jsonDiff.test.d.ts +2 -0
- package/lib/test/unit/jsonConverter.test.d.ts +2 -0
- package/lib/test/unit/jsonEditor/jsonEditor.test.d.ts +2 -0
- package/lib/test/unit/jsonEditor/templateContext.test.d.ts +2 -0
- package/lib/test/unit/jsonReferenceMap.test.d.ts +2 -0
- package/package.json +11 -11
- package/src/index.ts +29 -0
- package/src/packlets/context/compositeJsonMap.ts +120 -0
- package/src/packlets/context/contextHelpers.ts +221 -0
- package/src/packlets/context/index.ts +33 -0
- package/src/packlets/context/jsonContext.ts +133 -0
- package/src/packlets/converters/converters.ts +117 -0
- package/src/packlets/converters/index.ts +26 -0
- package/src/packlets/converters/jsonConverter.ts +476 -0
- package/src/packlets/diff/detailedDiff.ts +585 -0
- package/src/packlets/diff/index.ts +24 -0
- package/src/packlets/diff/threeWayDiff.ts +420 -0
- package/src/packlets/diff/utils.ts +66 -0
- package/src/packlets/editor/common.ts +125 -0
- package/src/packlets/editor/index.ts +36 -0
- package/src/packlets/editor/jsonEditor.ts +523 -0
- package/src/packlets/editor/jsonEditorRule.ts +117 -0
- package/src/packlets/editor/jsonEditorState.ts +225 -0
- package/src/packlets/editor/jsonReferenceMap.ts +516 -0
- package/src/packlets/editor/rules/conditional.ts +222 -0
- package/src/packlets/editor/rules/index.ts +25 -0
- package/src/packlets/editor/rules/multivalue.ts +206 -0
- package/src/packlets/editor/rules/references.ts +177 -0
- package/src/packlets/editor/rules/templates.ts +159 -0
- package/CHANGELOG.md +0 -115
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/context/compositeJsonMap.d.ts.map +0 -1
- package/lib/packlets/context/compositeJsonMap.js.map +0 -1
- package/lib/packlets/context/contextHelpers.d.ts.map +0 -1
- package/lib/packlets/context/contextHelpers.js.map +0 -1
- package/lib/packlets/context/index.d.ts.map +0 -1
- package/lib/packlets/context/index.js.map +0 -1
- package/lib/packlets/context/jsonContext.d.ts.map +0 -1
- package/lib/packlets/context/jsonContext.js.map +0 -1
- package/lib/packlets/converters/converters.d.ts.map +0 -1
- package/lib/packlets/converters/converters.js.map +0 -1
- package/lib/packlets/converters/index.d.ts.map +0 -1
- package/lib/packlets/converters/index.js.map +0 -1
- package/lib/packlets/converters/jsonConverter.d.ts.map +0 -1
- package/lib/packlets/converters/jsonConverter.js.map +0 -1
- package/lib/packlets/editor/common.d.ts.map +0 -1
- package/lib/packlets/editor/common.js.map +0 -1
- package/lib/packlets/editor/index.d.ts.map +0 -1
- package/lib/packlets/editor/index.js.map +0 -1
- package/lib/packlets/editor/jsonEditor.d.ts.map +0 -1
- package/lib/packlets/editor/jsonEditor.js.map +0 -1
- package/lib/packlets/editor/jsonEditorRule.d.ts.map +0 -1
- package/lib/packlets/editor/jsonEditorRule.js.map +0 -1
- package/lib/packlets/editor/jsonEditorState.d.ts.map +0 -1
- package/lib/packlets/editor/jsonEditorState.js.map +0 -1
- package/lib/packlets/editor/jsonReferenceMap.d.ts.map +0 -1
- package/lib/packlets/editor/jsonReferenceMap.js.map +0 -1
- package/lib/packlets/editor/rules/conditional.d.ts.map +0 -1
- package/lib/packlets/editor/rules/conditional.js.map +0 -1
- package/lib/packlets/editor/rules/index.d.ts.map +0 -1
- package/lib/packlets/editor/rules/index.js.map +0 -1
- package/lib/packlets/editor/rules/multivalue.d.ts.map +0 -1
- package/lib/packlets/editor/rules/multivalue.js.map +0 -1
- package/lib/packlets/editor/rules/references.d.ts.map +0 -1
- package/lib/packlets/editor/rules/references.js.map +0 -1
- package/lib/packlets/editor/rules/templates.d.ts.map +0 -1
- package/lib/packlets/editor/rules/templates.js.map +0 -1
|
@@ -0,0 +1,222 @@
|
|
|
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 { JsonObject, JsonValue, isJsonObject } from '@fgv/ts-json-base';
|
|
24
|
+
import { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';
|
|
25
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
26
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
27
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returned by {@link EditorRules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}
|
|
31
|
+
* to indicate whether a successful match was due to a matching condition or a default value.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export interface IConditionalJsonKeyResult extends JsonObject {
|
|
35
|
+
matchType: 'default' | 'match' | 'unconditional';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* On a successful match, the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}
|
|
40
|
+
* stores a {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the
|
|
41
|
+
* matching result, to be resolved at finalization time.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {
|
|
45
|
+
value: JsonValue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Configuration options for the {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {
|
|
53
|
+
/**
|
|
54
|
+
* If true (default) then properties with unconditional names
|
|
55
|
+
* (which start with !) are flattened.
|
|
56
|
+
*/
|
|
57
|
+
flattenUnconditionalValues?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates
|
|
62
|
+
* properties with conditional keys, omitting non-matching keys and merging keys that match,
|
|
63
|
+
* or default keys only if no other keys match.
|
|
64
|
+
*
|
|
65
|
+
* The default syntax for a conditional key is:
|
|
66
|
+
* "?value1=value2" - matches if value1 and value2 are the same, is ignored otherwise.
|
|
67
|
+
* "?value" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.
|
|
68
|
+
* "?default" - matches only if no other conditional blocks in the same object were matched.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export class ConditionalJsonEditorRule extends JsonEditorRuleBase {
|
|
72
|
+
/**
|
|
73
|
+
* Stored fully-resolved {@link EditorRules.IConditionalJsonRuleOptions | options} for this
|
|
74
|
+
* rule.
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
protected _options?: IConditionalJsonRuleOptions;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
81
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
82
|
+
* used for this rule.
|
|
83
|
+
*/
|
|
84
|
+
public constructor(options?: IConditionalJsonRuleOptions) {
|
|
85
|
+
super();
|
|
86
|
+
this._options = options;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a new {@link EditorRules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.
|
|
91
|
+
* @param options - Optional {@link EditorRules.IConditionalJsonRuleOptions | configuration options}
|
|
92
|
+
* used for this rule.
|
|
93
|
+
*/
|
|
94
|
+
public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {
|
|
95
|
+
return captureResult(() => new ConditionalJsonEditorRule(options));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Evaluates a property for conditional application.
|
|
100
|
+
* @param key - The key of the property to be considered
|
|
101
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
102
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
103
|
+
* @returns Returns `Success` with detail `'deferred'` and a
|
|
104
|
+
* {@link EditorRules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.
|
|
105
|
+
* for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for
|
|
106
|
+
* a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise
|
|
107
|
+
* fails with detail `'inapplicable'`.
|
|
108
|
+
*/
|
|
109
|
+
public editProperty(
|
|
110
|
+
key: string,
|
|
111
|
+
value: JsonValue,
|
|
112
|
+
state: JsonEditorState
|
|
113
|
+
): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {
|
|
114
|
+
const result = this._tryParseCondition(key, state).onSuccess((deferred) => {
|
|
115
|
+
if (isJsonObject(value)) {
|
|
116
|
+
const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };
|
|
117
|
+
return succeedWithDetail(rtrn, 'deferred');
|
|
118
|
+
}
|
|
119
|
+
return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(
|
|
120
|
+
`${key}: conditional body must be object`,
|
|
121
|
+
'error'
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (result.isFailure() && result.detail === 'error') {
|
|
126
|
+
return state.failValidation('invalidPropertyName', result.message, this._options?.validation);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Finalizes any deferred conditional properties. If the only deferred property is
|
|
134
|
+
* default, that property is emitted. Otherwise all matching properties are emitted.
|
|
135
|
+
* @param finalized - The deferred properties to be considered for merge.
|
|
136
|
+
* @param __state - The {@link JsonEditorState | editor state} for the object
|
|
137
|
+
* being edited.
|
|
138
|
+
*/
|
|
139
|
+
public finalizeProperties(
|
|
140
|
+
finalized: JsonObject[],
|
|
141
|
+
__state: JsonEditorState
|
|
142
|
+
): DetailedResult<JsonObject[], JsonEditFailureReason> {
|
|
143
|
+
let toMerge = finalized;
|
|
144
|
+
if (finalized.length > 1) {
|
|
145
|
+
if (finalized.find((o) => o.matchType === 'match') !== undefined) {
|
|
146
|
+
toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Determines if a given property key is conditional. Derived classes can override this
|
|
154
|
+
* method to use a different format for conditional properties.
|
|
155
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
156
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
157
|
+
* @returns `Success` with detail `'deferred'` and a
|
|
158
|
+
* {@link EditorRules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the
|
|
159
|
+
* match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`
|
|
160
|
+
* for a non-matching conditional property. Fails with detail `'error'` if an error occurs
|
|
161
|
+
* or with detail `'inapplicable'` if the key does not represent a conditional property.
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
protected _tryParseCondition(
|
|
165
|
+
key: string,
|
|
166
|
+
state: JsonEditorState
|
|
167
|
+
): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {
|
|
168
|
+
if (key.startsWith('?')) {
|
|
169
|
+
// ignore everything after any #
|
|
170
|
+
key = key.split('#')[0].trim();
|
|
171
|
+
|
|
172
|
+
if (key === '?default') {
|
|
173
|
+
return succeedWithDetail({ matchType: 'default' }, 'deferred');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);
|
|
177
|
+
if (parts.length === 3) {
|
|
178
|
+
if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {
|
|
179
|
+
return failWithDetail(`Condition ${key} does not match`, 'ignore');
|
|
180
|
+
}
|
|
181
|
+
return succeedWithDetail({ matchType: 'match' }, 'deferred');
|
|
182
|
+
} else if (parts.length === 1) {
|
|
183
|
+
if (parts[0].trim().length === 0) {
|
|
184
|
+
return failWithDetail(`Condition ${key} does not match`, 'ignore');
|
|
185
|
+
}
|
|
186
|
+
return succeedWithDetail({ matchType: 'match' }, 'deferred');
|
|
187
|
+
}
|
|
188
|
+
const message = `Malformed condition token ${key}`;
|
|
189
|
+
return state.failValidation('invalidPropertyName', message, this._options?.validation);
|
|
190
|
+
} else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {
|
|
191
|
+
return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');
|
|
192
|
+
}
|
|
193
|
+
return failWithDetail('inapplicable', 'inapplicable');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Compares two strings using a supplied operator.
|
|
198
|
+
* @param left - The first string to be compared.
|
|
199
|
+
* @param right - The second string to be compared.
|
|
200
|
+
* @param operator - The operator to be applied.
|
|
201
|
+
* @returns `true` if the condition is met, `false` otherwise.
|
|
202
|
+
* @internal
|
|
203
|
+
*/
|
|
204
|
+
protected _compare(left: string, right: string, operator: string): boolean {
|
|
205
|
+
switch (operator) {
|
|
206
|
+
case '=':
|
|
207
|
+
return left === right;
|
|
208
|
+
case '>':
|
|
209
|
+
return left > right;
|
|
210
|
+
case '<':
|
|
211
|
+
return left < right;
|
|
212
|
+
case '>=':
|
|
213
|
+
return left >= right;
|
|
214
|
+
case '<=':
|
|
215
|
+
return left <= right;
|
|
216
|
+
case '!=':
|
|
217
|
+
return left !== right;
|
|
218
|
+
}
|
|
219
|
+
/* c8 ignore next 2 - unreachable */
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export * from './conditional';
|
|
23
|
+
export * from './multivalue';
|
|
24
|
+
export * from './references';
|
|
25
|
+
export * from './templates';
|
|
@@ -0,0 +1,206 @@
|
|
|
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 { JsonObject, JsonValue } from '@fgv/ts-json-base';
|
|
24
|
+
import {
|
|
25
|
+
DetailedResult,
|
|
26
|
+
Result,
|
|
27
|
+
allSucceed,
|
|
28
|
+
captureResult,
|
|
29
|
+
failWithDetail,
|
|
30
|
+
succeed,
|
|
31
|
+
succeedWithDetail
|
|
32
|
+
} from '@fgv/ts-utils';
|
|
33
|
+
import { IJsonContext, VariableValue } from '../../context';
|
|
34
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
35
|
+
|
|
36
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
37
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Represents the parts of a multi-value property key.
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export interface IMultiValuePropertyParts {
|
|
44
|
+
/**
|
|
45
|
+
* The original matched token.
|
|
46
|
+
*/
|
|
47
|
+
readonly token: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The name of the variable used to project each possible
|
|
51
|
+
* property value into the child values or objects being
|
|
52
|
+
* resolved.
|
|
53
|
+
*/
|
|
54
|
+
readonly propertyVariable: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The set of property values to be expanded.
|
|
58
|
+
*/
|
|
59
|
+
readonly propertyValues: string[];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* If `true`, the resolved values are added as an array
|
|
63
|
+
* with the name of the {@link EditorRules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.
|
|
64
|
+
* If false, values are added as individual properties with names that correspond the value.
|
|
65
|
+
*/
|
|
66
|
+
readonly asArray: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The {@link EditorRules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
|
|
71
|
+
* expands matching keys multiple times, projecting the value into the template
|
|
72
|
+
* context for any child objects rendered by the rule.
|
|
73
|
+
*
|
|
74
|
+
* The default syntax for a multi-value key is:
|
|
75
|
+
* "[[var]]=value1,value2,value3"
|
|
76
|
+
* Where "var" is the name of the variable that will be passed to
|
|
77
|
+
* child template resolution, and "value1,value2,value3" is a
|
|
78
|
+
* comma-separated list of values to be expanded.
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
export class MultiValueJsonEditorRule extends JsonEditorRuleBase {
|
|
82
|
+
/**
|
|
83
|
+
* Stored fully-resolved {@link IJsonEditorOptions | editor options}
|
|
84
|
+
* for this rule.
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
protected _options?: IJsonEditorOptions;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
91
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
92
|
+
*/
|
|
93
|
+
public constructor(options?: IJsonEditorOptions) {
|
|
94
|
+
super();
|
|
95
|
+
this._options = options;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new {@link EditorRules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
100
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options}.
|
|
101
|
+
*/
|
|
102
|
+
public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {
|
|
103
|
+
return captureResult(() => new MultiValueJsonEditorRule(options));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Evaluates a property for multi-value expansion.
|
|
108
|
+
* @param key - The key of the property to be considered
|
|
109
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
110
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
111
|
+
* @returns `Success` with an object containing the fully-resolved child values to be merged for
|
|
112
|
+
* matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
|
|
113
|
+
* with detail `'inapplicable'` if the property key is not a conditional property.
|
|
114
|
+
*/
|
|
115
|
+
public editProperty(
|
|
116
|
+
key: string,
|
|
117
|
+
value: JsonValue,
|
|
118
|
+
state: JsonEditorState
|
|
119
|
+
): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {
|
|
120
|
+
const json: JsonObject = {};
|
|
121
|
+
const result = this._tryParse(key, state).onSuccess((parts) => {
|
|
122
|
+
return allSucceed(
|
|
123
|
+
parts.propertyValues.map((pv) => {
|
|
124
|
+
return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {
|
|
125
|
+
return state.editor.clone(value, ctx).onSuccess((cloned) => {
|
|
126
|
+
json[pv] = cloned;
|
|
127
|
+
return succeedWithDetail(cloned);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}),
|
|
131
|
+
json
|
|
132
|
+
)
|
|
133
|
+
.onSuccess(() => {
|
|
134
|
+
if (parts.asArray) {
|
|
135
|
+
const arrayRtrn: JsonObject = {};
|
|
136
|
+
arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));
|
|
137
|
+
return succeed(arrayRtrn);
|
|
138
|
+
}
|
|
139
|
+
return succeed(json);
|
|
140
|
+
})
|
|
141
|
+
.withFailureDetail('error');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (result.isFailure() && result.detail === 'error') {
|
|
145
|
+
return state.failValidation('invalidPropertyName', result.message);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Extends the {@link IJsonContext | current context} with a supplied state and values.
|
|
152
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
153
|
+
* @param values - An array of {@link VariableValue | VariableValue} to be added to the
|
|
154
|
+
* context.
|
|
155
|
+
* @returns The extended {@link IJsonContext | context}.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
protected _deriveContext(
|
|
159
|
+
state: JsonEditorState,
|
|
160
|
+
...values: VariableValue[]
|
|
161
|
+
): Result<IJsonContext | undefined> {
|
|
162
|
+
return state.extendContext(this._options?.context, { vars: values });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Determines if a given property key is multi-value. Derived classes can override this
|
|
167
|
+
* method to use a different format for multi-value properties.
|
|
168
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
169
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
170
|
+
* @returns `Success` with detail `'deferred'` and an
|
|
171
|
+
* {@link EditorRules.IMultiValuePropertyParts | IMultiValuePropertyParts}
|
|
172
|
+
* describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
|
|
173
|
+
* or with detail `'inapplicable'` if the key does not represent a multi-value property.
|
|
174
|
+
* @public
|
|
175
|
+
*/
|
|
176
|
+
protected _tryParse(
|
|
177
|
+
token: string,
|
|
178
|
+
state: JsonEditorState
|
|
179
|
+
): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {
|
|
180
|
+
let parts: string[] = [];
|
|
181
|
+
let asArray = false;
|
|
182
|
+
|
|
183
|
+
if (token.startsWith('[[')) {
|
|
184
|
+
parts = token.substring(2).split(']]=');
|
|
185
|
+
asArray = true;
|
|
186
|
+
} else if (token.startsWith('*')) {
|
|
187
|
+
parts = token.substring(1).split('=');
|
|
188
|
+
asArray = false;
|
|
189
|
+
} else {
|
|
190
|
+
return failWithDetail(token, 'inapplicable');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (parts.length !== 2) {
|
|
194
|
+
const message = `Malformed multi-value property: ${token}`;
|
|
195
|
+
return state.failValidation('invalidPropertyName', message, this._options?.validation);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (parts[1].includes('{{')) {
|
|
199
|
+
return failWithDetail('unresolved template', 'inapplicable');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const propertyVariable = parts[0];
|
|
203
|
+
const propertyValues = parts[1].split(',');
|
|
204
|
+
return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
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 { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '@fgv/ts-json-base';
|
|
24
|
+
import {
|
|
25
|
+
DetailedResult,
|
|
26
|
+
Result,
|
|
27
|
+
captureResult,
|
|
28
|
+
fail,
|
|
29
|
+
failWithDetail,
|
|
30
|
+
succeedWithDetail
|
|
31
|
+
} from '@fgv/ts-utils';
|
|
32
|
+
import { IJsonContext } from '../../context';
|
|
33
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
34
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
35
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The {@link EditorRules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
|
|
39
|
+
* keys or values that match some known object with a copy of that referenced object, formatted
|
|
40
|
+
* according to the current context.
|
|
41
|
+
*
|
|
42
|
+
* A property key is matched if it matches any known referenced value.
|
|
43
|
+
* - If the value of the matched key is `'default'`, then the entire object is formatted
|
|
44
|
+
* with the current context, flattened and merged into the current object.
|
|
45
|
+
* - If the value of the matched key is some other string, then the entire
|
|
46
|
+
* object is formatted with the current context, and the child of the resulting
|
|
47
|
+
* object at the specified path is flattened and merged into the current object.
|
|
48
|
+
* - If the value of the matched key is an object, then the entire object is
|
|
49
|
+
* formatted with the current context extended to include any properties of
|
|
50
|
+
* that object, flattened, and merged into the current object.
|
|
51
|
+
* - It is an error if the referenced value is not an object.
|
|
52
|
+
*
|
|
53
|
+
* Any property, array or literal value is matched if it matches any known
|
|
54
|
+
* value reference. The referenced value is replaced by the referenced
|
|
55
|
+
* value, formatted using the current editor context.
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export class ReferenceJsonEditorRule extends JsonEditorRuleBase {
|
|
59
|
+
/**
|
|
60
|
+
* Stored fully-resolved {@link IJsonEditorOptions | editor options} for this rule.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
protected _options?: IJsonEditorOptions;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
67
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
68
|
+
*/
|
|
69
|
+
public constructor(options?: IJsonEditorOptions) {
|
|
70
|
+
super();
|
|
71
|
+
this._options = options;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new {@link EditorRules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
76
|
+
* @param options - Optional {@link IJsonEditorOptions | configuration options} for this rule.
|
|
77
|
+
*/
|
|
78
|
+
public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {
|
|
79
|
+
return captureResult(() => new ReferenceJsonEditorRule(options));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Evaluates a property for reference expansion.
|
|
84
|
+
* @param key - The key of the property to be considered.
|
|
85
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
86
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
87
|
+
* @returns If the reference is successful, returns `Success` with a `JsonObject`
|
|
88
|
+
* to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`
|
|
89
|
+
* for non-reference keys or with detail `'error'` if an error occurs.
|
|
90
|
+
*/
|
|
91
|
+
public editProperty(
|
|
92
|
+
key: string,
|
|
93
|
+
value: JsonValue,
|
|
94
|
+
state: JsonEditorState
|
|
95
|
+
): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {
|
|
96
|
+
/* c8 ignore next 2 */
|
|
97
|
+
const validation = this._options?.validation;
|
|
98
|
+
const refs = state.getRefs(this._options?.context);
|
|
99
|
+
if (refs?.has(key)) {
|
|
100
|
+
// need to apply any rules to the value before we evaluate it
|
|
101
|
+
const cloneResult = state.editor.clone(value, state.context);
|
|
102
|
+
if (cloneResult.isSuccess()) {
|
|
103
|
+
value = cloneResult.value;
|
|
104
|
+
} else {
|
|
105
|
+
const message = `${key}: ${cloneResult.message}`;
|
|
106
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const contextResult = this._extendContext(state, value);
|
|
110
|
+
if (contextResult.isSuccess()) {
|
|
111
|
+
const objResult = refs.getJsonObject(key, contextResult.value);
|
|
112
|
+
// guarded by the has above so should never happen
|
|
113
|
+
/* c8 ignore else */
|
|
114
|
+
if (objResult.isSuccess()) {
|
|
115
|
+
if (typeof value !== 'string' || value === 'default') {
|
|
116
|
+
return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');
|
|
117
|
+
}
|
|
118
|
+
const pickResult = pickJsonObject(objResult.value, value);
|
|
119
|
+
if (pickResult.isFailure()) {
|
|
120
|
+
const message = `${key}: ${pickResult.message}`;
|
|
121
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
122
|
+
}
|
|
123
|
+
return pickResult.withDetail('edited');
|
|
124
|
+
} else if (objResult.detail !== 'unknown') {
|
|
125
|
+
const message = `${key}: ${objResult.message}`;
|
|
126
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
const message = `${key}: ${contextResult.message}`;
|
|
130
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return failWithDetail('inapplicable', 'inapplicable');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Evaluates a property, array or literal value for reference replacement.
|
|
138
|
+
* @param value - The `JsonValue` of the property to be considered.
|
|
139
|
+
* @param state - The {@link JsonEditorState | editor state} for the object being edited.
|
|
140
|
+
*/
|
|
141
|
+
public editValue(
|
|
142
|
+
value: JsonValue,
|
|
143
|
+
state: JsonEditorState
|
|
144
|
+
): DetailedResult<JsonValue, JsonEditFailureReason> {
|
|
145
|
+
/* c8 ignore next */
|
|
146
|
+
const refs = state.getRefs(this._options?.context);
|
|
147
|
+
|
|
148
|
+
if (refs && typeof value === 'string') {
|
|
149
|
+
/* c8 ignore next */
|
|
150
|
+
const context = state.getContext(this._options?.context);
|
|
151
|
+
const result = refs.getJsonValue(value, context);
|
|
152
|
+
if (result.isSuccess()) {
|
|
153
|
+
return succeedWithDetail(result.value, 'edited');
|
|
154
|
+
} else if (result.detail === 'error') {
|
|
155
|
+
return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return failWithDetail('inapplicable', 'inapplicable');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Gets the template variables to use given the value of some property whose name matched a
|
|
163
|
+
* resource plus the base template context.
|
|
164
|
+
* @param state - The {@link JsonEditorState | editor state} to be extended.
|
|
165
|
+
* @param supplied - The string or object supplied in the source json.
|
|
166
|
+
* @internal
|
|
167
|
+
*/
|
|
168
|
+
protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {
|
|
169
|
+
const add: Record<string, unknown> = {};
|
|
170
|
+
if (isJsonObject(supplied)) {
|
|
171
|
+
add.vars = Object.entries(supplied);
|
|
172
|
+
} else if (typeof supplied !== 'string') {
|
|
173
|
+
return fail(`Invalid template path or context: "${JSON.stringify(supplied)}"`);
|
|
174
|
+
}
|
|
175
|
+
return state.extendContext(this._options?.context, add);
|
|
176
|
+
}
|
|
177
|
+
}
|