@fgv/ts-json 1.9.6 → 2.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ts-json.d.ts +1826 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +42 -0
- package/lib/index.js.map +1 -0
- package/lib/packlets/context/compositeJsonMap.d.ts +60 -0
- package/lib/packlets/context/compositeJsonMap.d.ts.map +1 -0
- package/lib/packlets/context/compositeJsonMap.js +105 -0
- package/lib/packlets/context/compositeJsonMap.js.map +1 -0
- package/lib/packlets/context/contextHelpers.d.ts +116 -0
- package/lib/packlets/context/contextHelpers.d.ts.map +1 -0
- package/lib/packlets/context/contextHelpers.js +191 -0
- package/lib/packlets/context/contextHelpers.js.map +1 -0
- package/lib/packlets/context/index.d.ts +4 -0
- package/lib/packlets/context/index.d.ts.map +1 -0
- package/lib/packlets/context/index.js +31 -0
- package/lib/packlets/context/index.js.map +1 -0
- package/lib/packlets/context/jsonContext.d.ts +84 -0
- package/lib/packlets/context/jsonContext.d.ts.map +1 -0
- package/lib/packlets/context/jsonContext.js +41 -0
- package/lib/packlets/context/jsonContext.js.map +1 -0
- package/lib/packlets/converters/converters.d.ts +54 -0
- package/lib/packlets/converters/converters.d.ts.map +1 -0
- package/lib/packlets/converters/converters.js +105 -0
- package/lib/packlets/converters/converters.js.map +1 -0
- package/lib/packlets/converters/file.d.ts +83 -0
- package/lib/packlets/converters/file.d.ts.map +1 -0
- package/lib/packlets/converters/file.js +162 -0
- package/lib/packlets/converters/file.js.map +1 -0
- package/lib/packlets/converters/index.d.ts +5 -0
- package/lib/packlets/converters/index.d.ts.map +1 -0
- package/{jsonEditor → lib/packlets/converters}/index.js +10 -10
- package/lib/packlets/converters/index.js.map +1 -0
- package/lib/packlets/converters/jsonConverter.d.ts +276 -0
- package/lib/packlets/converters/jsonConverter.d.ts.map +1 -0
- package/lib/packlets/converters/jsonConverter.js +310 -0
- package/lib/packlets/converters/jsonConverter.js.map +1 -0
- package/lib/packlets/editor/common.d.ts +67 -0
- package/lib/packlets/editor/common.d.ts.map +1 -0
- package/lib/packlets/editor/common.js +3 -0
- package/lib/packlets/editor/common.js.map +1 -0
- package/lib/packlets/editor/index.d.ts +8 -0
- package/lib/packlets/editor/index.d.ts.map +1 -0
- package/lib/packlets/editor/index.js +58 -0
- package/lib/packlets/editor/index.js.map +1 -0
- package/lib/packlets/editor/jsonEditor.d.ts +155 -0
- package/lib/packlets/editor/jsonEditor.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditor.js +333 -0
- package/lib/packlets/editor/jsonEditor.js.map +1 -0
- package/lib/packlets/editor/jsonEditorRule.d.ts +64 -0
- package/lib/packlets/editor/jsonEditorRule.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditorRule.js +54 -0
- package/lib/packlets/editor/jsonEditorRule.js.map +1 -0
- package/lib/packlets/editor/jsonEditorState.d.ts +125 -0
- package/lib/packlets/editor/jsonEditorState.d.ts.map +1 -0
- package/lib/packlets/editor/jsonEditorState.js +177 -0
- package/lib/packlets/editor/jsonEditorState.js.map +1 -0
- package/lib/packlets/editor/jsonReferenceMap.d.ts +303 -0
- package/lib/packlets/editor/jsonReferenceMap.d.ts.map +1 -0
- package/lib/packlets/editor/jsonReferenceMap.js +324 -0
- package/lib/packlets/editor/jsonReferenceMap.js.map +1 -0
- package/lib/packlets/editor/rules/conditional.d.ts +107 -0
- package/lib/packlets/editor/rules/conditional.d.ts.map +1 -0
- package/lib/packlets/editor/rules/conditional.js +167 -0
- package/lib/packlets/editor/rules/conditional.js.map +1 -0
- package/{jsonEditor → lib/packlets/editor}/rules/index.d.ts +1 -0
- package/lib/packlets/editor/rules/index.d.ts.map +1 -0
- package/lib/packlets/editor/rules/index.js +42 -0
- package/lib/packlets/editor/rules/index.js.map +1 -0
- package/lib/packlets/editor/rules/multivalue.d.ts +94 -0
- package/lib/packlets/editor/rules/multivalue.d.ts.map +1 -0
- package/lib/packlets/editor/rules/multivalue.js +141 -0
- package/lib/packlets/editor/rules/multivalue.js.map +1 -0
- package/lib/packlets/editor/rules/references.d.ts +69 -0
- package/lib/packlets/editor/rules/references.d.ts.map +1 -0
- package/lib/packlets/editor/rules/references.js +160 -0
- package/lib/packlets/editor/rules/references.js.map +1 -0
- package/lib/packlets/editor/rules/templates.d.ts +74 -0
- package/lib/packlets/editor/rules/templates.d.ts.map +1 -0
- package/lib/packlets/editor/rules/templates.js +127 -0
- package/lib/packlets/editor/rules/templates.js.map +1 -0
- package/lib/packlets/json/common.d.ts +85 -0
- package/lib/packlets/json/common.d.ts.map +1 -0
- package/lib/packlets/json/common.js +123 -0
- package/lib/packlets/json/common.js.map +1 -0
- package/lib/packlets/json/index.d.ts +2 -0
- package/lib/packlets/json/index.d.ts.map +1 -0
- package/lib/packlets/json/index.js +39 -0
- package/lib/packlets/json/index.js.map +1 -0
- package/package.json +57 -49
- package/common.d.ts +0 -58
- package/common.js +0 -116
- package/compositeJsonMap.d.ts +0 -47
- package/compositeJsonMap.js +0 -97
- package/contextHelpers.d.ts +0 -21
- package/contextHelpers.js +0 -101
- package/converters.d.ts +0 -37
- package/converters.js +0 -92
- package/file.d.ts +0 -60
- package/file.js +0 -140
- package/index.d.ts +0 -10
- package/index.js +0 -62
- package/jsonContext.d.ts +0 -74
- package/jsonContext.js +0 -40
- package/jsonConverter.d.ts +0 -229
- package/jsonConverter.js +0 -292
- package/jsonEditor/common.d.ts +0 -36
- package/jsonEditor/common.js +0 -3
- package/jsonEditor/index.d.ts +0 -5
- package/jsonEditor/jsonEditor.d.ts +0 -74
- package/jsonEditor/jsonEditor.js +0 -259
- package/jsonEditor/jsonEditorRule.d.ts +0 -47
- package/jsonEditor/jsonEditorRule.js +0 -44
- package/jsonEditor/jsonEditorState.d.ts +0 -24
- package/jsonEditor/jsonEditorState.js +0 -86
- package/jsonEditor/rules/conditional.d.ts +0 -84
- package/jsonEditor/rules/conditional.js +0 -146
- package/jsonEditor/rules/index.js +0 -21
- package/jsonEditor/rules/multivalue.d.ts +0 -78
- package/jsonEditor/rules/multivalue.js +0 -129
- package/jsonEditor/rules/references.d.ts +0 -62
- package/jsonEditor/rules/references.js +0 -158
- package/jsonEditor/rules/templates.d.ts +0 -56
- package/jsonEditor/rules/templates.js +0 -115
- package/jsonReferenceMap.d.ts +0 -138
- package/jsonReferenceMap.js +0 -202
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyG;AACzG,qCAAiE;AAEjE,sDAAuD;AAkCvD;;;;;;;;;;GAUG;AACH,MAAa,yBAA0B,SAAQ,mCAAkB;IAQ/D;;;;OAIG;IACH,YAAmB,OAAqC;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,OAAqC;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;;;OAUG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxE,IAAI,IAAA,mBAAY,EAAC,KAAK,CAAC,EAAE;gBACvB,MAAM,IAAI,mCAAwC,QAAQ,KAAE,KAAK,GAAE,CAAC;gBACpE,OAAO,IAAA,4BAAiB,EAAC,IAAI,EAAE,UAAU,CAAC,CAAC;aAC5C;YACD,OAAO,IAAA,yBAAc,EACnB,GAAG,GAAG,mCAAmC,EACzC,OAA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Ac,EAAC,cAAc,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;;OAOG;IACO,QAAQ,CAAC,IAAY,EAAE,KAAa,EAAE,QAAgB;QAC9D,QAAQ,QAAQ,EAAE;YAChB,KAAK,GAAG;gBACN,OAAO,IAAI,KAAK,KAAK,CAAC;YACxB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,GAAG;gBACN,OAAO,IAAI,GAAG,KAAK,CAAC;YACtB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,IAAI,KAAK,CAAC;YACvB,KAAK,IAAI;gBACP,OAAO,IAAI,KAAK,KAAK,CAAC;SACzB;QACD,oCAAoC;QACpC,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAvJD,8DAuJC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { DetailedResult, Result, captureResult, failWithDetail, succeedWithDetail } from '@fgv/ts-utils';\nimport { JsonObject, JsonValue, isJsonObject } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Returned by {@link Editor.Rules.ConditionalJsonEditorRule._tryParseCondition | ConditionalJsonEditorRule._tryParseCondition}\n * to indicate whether a successful match was due to a matching condition or a default value.\n * @public\n */\nexport interface IConditionalJsonKeyResult extends JsonObject {\n matchType: 'default' | 'match' | 'unconditional';\n}\n\n/**\n * On a successful match, the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}\n * stores a {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject} describing the\n * matching result, to be resolved at finalization time.\n * @public\n */\nexport interface IConditionalJsonDeferredObject extends IConditionalJsonKeyResult {\n value: JsonValue;\n}\n\n/**\n * Configuration options for the {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @public\n */\nexport interface IConditionalJsonRuleOptions extends Partial<IJsonEditorOptions> {\n /**\n * If true (default) then properties with unconditional names\n * (which start with !) are flattened.\n */\n flattenUnconditionalValues?: boolean;\n}\n\n/**\n * The {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule} evaluates\n * properties with conditional keys, omitting non-matching keys and merging keys that match,\n * or default keys only if no other keys match.\n *\n * The default syntax for a conditional key is:\n * \"?value1=value2\" - matches if value1 and value2 are the same, is ignored otherwise.\n * \"?value\" - matches if value is a non-empty, non-whitespace string. Is ignored otherwise.\n * \"?default\" - matches only if no other conditional blocks in the same object were matched.\n * @public\n */\nexport class ConditionalJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.Rules.IConditionalJsonRuleOptions | options} for this\n * rule.\n * @public\n */\n protected _options?: IConditionalJsonRuleOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public constructor(options?: IConditionalJsonRuleOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ConditionalJsonEditorRule | ConditionalJsonEditorRule}.\n * @param options - Optional {@link Editor.Rules.IConditionalJsonRuleOptions | configuration options}\n * used for this rule.\n */\n public static create(options?: IConditionalJsonRuleOptions): Result<ConditionalJsonEditorRule> {\n return captureResult(() => new ConditionalJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for conditional application.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns Returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonDeferredObject | IConditionalJsonDeferredObject}.\n * for a matching, default or unconditional key. Returns `Failure` with detail `'ignore'` for\n * a non-matching conditional, or with detail `'error'` if an error occurs. Otherwise\n * fails with detail `'inapplicable'`.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const result = this._tryParseCondition(key, state).onSuccess((deferred) => {\n if (isJsonObject(value)) {\n const rtrn: IConditionalJsonDeferredObject = { ...deferred, value };\n return succeedWithDetail(rtrn, 'deferred');\n }\n return failWithDetail<JsonObject, JsonPropertyEditFailureReason>(\n `${key}: conditional body must be object`,\n 'error'\n );\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message, this._options?.validation);\n }\n\n return result;\n }\n\n /**\n * Finalizes any deferred conditional properties. If the only deferred property is\n * default, that property is emitted. Otherwise all matching properties are emitted.\n * @param finalized - The deferred properties to be considered for merge.\n * @param __state - The {@link Editor.JsonEditorState | editor state} for the object\n * being edited.\n */\n public finalizeProperties(\n finalized: JsonObject[],\n __state: JsonEditorState\n ): DetailedResult<JsonObject[], JsonEditFailureReason> {\n let toMerge = finalized;\n if (finalized.length > 1) {\n if (finalized.find((o) => o.matchType === 'match') !== undefined) {\n toMerge = finalized.filter((o) => o.matchType === 'match' || o.matchType === 'unconditional');\n }\n }\n return succeedWithDetail(toMerge.map((o) => o.value).filter(isJsonObject), 'edited');\n }\n\n /**\n * Determines if a given property key is conditional. Derived classes can override this\n * method to use a different format for conditional properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and a\n * {@link Editor.Rules.IConditionalJsonKeyResult | IConditionalJsonKeyResult} describing the\n * match for a default or matching conditional property. Returns `Failure` with detail `'ignore'`\n * for a non-matching conditional property. Fails with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a conditional property.\n * @public\n */\n protected _tryParseCondition(\n key: string,\n state: JsonEditorState\n ): DetailedResult<IConditionalJsonKeyResult, JsonPropertyEditFailureReason> {\n if (key.startsWith('?')) {\n // ignore everything after any #\n key = key.split('#')[0].trim();\n\n if (key === '?default') {\n return succeedWithDetail({ matchType: 'default' }, 'deferred');\n }\n\n const parts = key.substring(1).split(/(=|>=|<=|>|<|!=)/);\n if (parts.length === 3) {\n if (!this._compare(parts[0].trim(), parts[2].trim(), parts[1])) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n } else if (parts.length === 1) {\n if (parts[0].trim().length === 0) {\n return failWithDetail(`Condition ${key} does not match`, 'ignore');\n }\n return succeedWithDetail({ matchType: 'match' }, 'deferred');\n }\n const message = `Malformed condition token ${key}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n } else if (this._options?.flattenUnconditionalValues !== false && key.startsWith('!')) {\n return succeedWithDetail({ matchType: 'unconditional' }, 'deferred');\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Compares two strings using a supplied operator.\n * @param left - The first string to be compared.\n * @param right - The second string to be compared.\n * @param operator - The operator to be applied.\n * @returns `true` if the condition is met, `false` otherwise.\n * @internal\n */\n protected _compare(left: string, right: string, operator: string): boolean {\n switch (operator) {\n case '=':\n return left === right;\n case '>':\n return left > right;\n case '<':\n return left < right;\n case '>=':\n return left >= right;\n case '<=':\n return left <= right;\n case '!=':\n return left !== right;\n }\n // istanbul ignore next: unreachable\n return false;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/index.ts"],"names":[],"mappings":"AAqBA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/*
|
|
18
|
+
* Copyright (c) 2020 Erik Fortune
|
|
19
|
+
*
|
|
20
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
* in the Software without restriction, including without limitation the rights
|
|
23
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
24
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
25
|
+
* furnished to do so, subject to the following conditions:
|
|
26
|
+
*
|
|
27
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
28
|
+
* copies or substantial portions of the Software.
|
|
29
|
+
*
|
|
30
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
31
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
32
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
33
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
34
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
35
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
36
|
+
* SOFTWARE.
|
|
37
|
+
*/
|
|
38
|
+
__exportStar(require("./conditional"), exports);
|
|
39
|
+
__exportStar(require("./multivalue"), exports);
|
|
40
|
+
__exportStar(require("./references"), exports);
|
|
41
|
+
__exportStar(require("./templates"), exports);
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,gDAA8B;AAC9B,+CAA6B;AAC7B,+CAA6B;AAC7B,8CAA4B","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\nexport * from './conditional';\nexport * from './multivalue';\nexport * from './references';\nexport * from './templates';\n"]}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { DetailedResult, Result } from '@fgv/ts-utils';
|
|
2
|
+
import { IJsonContext, VariableValue } from '../../context';
|
|
3
|
+
import { JsonObject, JsonValue } from '../../json';
|
|
4
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
5
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
6
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
7
|
+
/**
|
|
8
|
+
* Represents the parts of a multi-value property key.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export interface IMultiValuePropertyParts {
|
|
12
|
+
/**
|
|
13
|
+
* The original matched token.
|
|
14
|
+
*/
|
|
15
|
+
readonly token: string;
|
|
16
|
+
/**
|
|
17
|
+
* The name of the variable used to project each possible
|
|
18
|
+
* property value into the child values or objects being
|
|
19
|
+
* resolved.
|
|
20
|
+
*/
|
|
21
|
+
readonly propertyVariable: string;
|
|
22
|
+
/**
|
|
23
|
+
* The set of property values to be expanded.
|
|
24
|
+
*/
|
|
25
|
+
readonly propertyValues: string[];
|
|
26
|
+
/**
|
|
27
|
+
* If `true`, the resolved values are added as an array
|
|
28
|
+
* with the name of the {@link Editor.Rules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.
|
|
29
|
+
* If false, values are added as individual properties with names that correspond the value.
|
|
30
|
+
*/
|
|
31
|
+
readonly asArray: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
|
|
35
|
+
* expands matching keys multiple times, projecting the value into the template
|
|
36
|
+
* context for any child objects rendered by the rule.
|
|
37
|
+
*
|
|
38
|
+
* The default syntax for a multi-value key is:
|
|
39
|
+
* "[[var]]=value1,value2,value3"
|
|
40
|
+
* Where "var" is the name of the variable that will be passed to
|
|
41
|
+
* child template resolution, and "value1,value2,value3" is a
|
|
42
|
+
* comma-separated list of values to be expanded.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare class MultiValueJsonEditorRule extends JsonEditorRuleBase {
|
|
46
|
+
/**
|
|
47
|
+
* Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options}
|
|
48
|
+
* for this rule.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
protected _options?: IJsonEditorOptions;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
54
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.
|
|
55
|
+
*/
|
|
56
|
+
constructor(options?: IJsonEditorOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
59
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.
|
|
60
|
+
*/
|
|
61
|
+
static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule>;
|
|
62
|
+
/**
|
|
63
|
+
* Evaluates a property for multi-value expansion.
|
|
64
|
+
* @param key - The key of the property to be considered
|
|
65
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
66
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
67
|
+
* @returns `Success` with an object containing the fully-resolved child values to be merged for
|
|
68
|
+
* matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
|
|
69
|
+
* with detail `'inapplicable'` if the property key is not a conditional property.
|
|
70
|
+
*/
|
|
71
|
+
editProperty(key: string, value: JsonValue, state: JsonEditorState): DetailedResult<JsonObject, JsonPropertyEditFailureReason>;
|
|
72
|
+
/**
|
|
73
|
+
* Extends the {@link IJsonContext | current context} with a supplied state and values.
|
|
74
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
75
|
+
* @param values - An array of {@link VariableValue | VariableValue} to be added to the
|
|
76
|
+
* context.
|
|
77
|
+
* @returns The extended {@link IJsonContext | context}.
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
protected _deriveContext(state: JsonEditorState, ...values: VariableValue[]): Result<IJsonContext | undefined>;
|
|
81
|
+
/**
|
|
82
|
+
* Determines if a given property key is multi-value. Derived classes can override this
|
|
83
|
+
* method to use a different format for multi-value properties.
|
|
84
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
85
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
86
|
+
* @returns `Success` with detail `'deferred'` and an
|
|
87
|
+
* {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}
|
|
88
|
+
* describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
|
|
89
|
+
* or with detail `'inapplicable'` if the key does not represent a multi-value property.
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
protected _tryParse(token: string, state: JsonEditorState): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason>;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=multivalue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multivalue.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,cAAc,EACd,MAAM,EAMP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,WAAW,CAAC;AAErG,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;GAGG;AACH,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAElC;;OAEG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC;IAElC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,wBAAyB,SAAQ,kBAAkB;IAC9D;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAExC;;;OAGG;gBACgB,OAAO,CAAC,EAAE,kBAAkB;IAK/C;;;OAGG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAAC,wBAAwB,CAAC;IAIpF;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IA+B5D;;;;;;;OAOG;IACH,SAAS,CAAC,cAAc,CACtB,KAAK,EAAE,eAAe,EACtB,GAAG,MAAM,EAAE,aAAa,EAAE,GACzB,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;IAInC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,SAAS,CACjB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,wBAAwB,EAAE,qBAAqB,CAAC;CA2BnE"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.MultiValueJsonEditorRule = void 0;
|
|
25
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
|
+
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
27
|
+
/**
|
|
28
|
+
* The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}
|
|
29
|
+
* expands matching keys multiple times, projecting the value into the template
|
|
30
|
+
* context for any child objects rendered by the rule.
|
|
31
|
+
*
|
|
32
|
+
* The default syntax for a multi-value key is:
|
|
33
|
+
* "[[var]]=value1,value2,value3"
|
|
34
|
+
* Where "var" is the name of the variable that will be passed to
|
|
35
|
+
* child template resolution, and "value1,value2,value3" is a
|
|
36
|
+
* comma-separated list of values to be expanded.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
class MultiValueJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
42
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.
|
|
43
|
+
*/
|
|
44
|
+
constructor(options) {
|
|
45
|
+
super();
|
|
46
|
+
this._options = options;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.
|
|
50
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.
|
|
51
|
+
*/
|
|
52
|
+
static create(options) {
|
|
53
|
+
return (0, ts_utils_1.captureResult)(() => new MultiValueJsonEditorRule(options));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Evaluates a property for multi-value expansion.
|
|
57
|
+
* @param key - The key of the property to be considered
|
|
58
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
59
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
60
|
+
* @returns `Success` with an object containing the fully-resolved child values to be merged for
|
|
61
|
+
* matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or
|
|
62
|
+
* with detail `'inapplicable'` if the property key is not a conditional property.
|
|
63
|
+
*/
|
|
64
|
+
editProperty(key, value, state) {
|
|
65
|
+
const json = {};
|
|
66
|
+
const result = this._tryParse(key, state).onSuccess((parts) => {
|
|
67
|
+
return (0, ts_utils_1.allSucceed)(parts.propertyValues.map((pv) => {
|
|
68
|
+
return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {
|
|
69
|
+
return state.editor.clone(value, ctx).onSuccess((cloned) => {
|
|
70
|
+
json[pv] = cloned;
|
|
71
|
+
return (0, ts_utils_1.succeedWithDetail)(cloned);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}), json)
|
|
75
|
+
.onSuccess(() => {
|
|
76
|
+
if (parts.asArray) {
|
|
77
|
+
const arrayRtrn = {};
|
|
78
|
+
arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));
|
|
79
|
+
return (0, ts_utils_1.succeed)(arrayRtrn);
|
|
80
|
+
}
|
|
81
|
+
return (0, ts_utils_1.succeed)(json);
|
|
82
|
+
})
|
|
83
|
+
.withFailureDetail('error');
|
|
84
|
+
});
|
|
85
|
+
if (result.isFailure() && result.detail === 'error') {
|
|
86
|
+
return state.failValidation('invalidPropertyName', result.message);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Extends the {@link IJsonContext | current context} with a supplied state and values.
|
|
92
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
93
|
+
* @param values - An array of {@link VariableValue | VariableValue} to be added to the
|
|
94
|
+
* context.
|
|
95
|
+
* @returns The extended {@link IJsonContext | context}.
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
_deriveContext(state, ...values) {
|
|
99
|
+
var _a;
|
|
100
|
+
return state.extendContext((_a = this._options) === null || _a === void 0 ? void 0 : _a.context, { vars: values });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Determines if a given property key is multi-value. Derived classes can override this
|
|
104
|
+
* method to use a different format for multi-value properties.
|
|
105
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
106
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
107
|
+
* @returns `Success` with detail `'deferred'` and an
|
|
108
|
+
* {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}
|
|
109
|
+
* describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs
|
|
110
|
+
* or with detail `'inapplicable'` if the key does not represent a multi-value property.
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
_tryParse(token, state) {
|
|
114
|
+
var _a;
|
|
115
|
+
let parts = [];
|
|
116
|
+
let asArray = false;
|
|
117
|
+
if (token.startsWith('[[')) {
|
|
118
|
+
parts = token.substring(2).split(']]=');
|
|
119
|
+
asArray = true;
|
|
120
|
+
}
|
|
121
|
+
else if (token.startsWith('*')) {
|
|
122
|
+
parts = token.substring(1).split('=');
|
|
123
|
+
asArray = false;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
return (0, ts_utils_1.failWithDetail)(token, 'inapplicable');
|
|
127
|
+
}
|
|
128
|
+
if (parts.length !== 2) {
|
|
129
|
+
const message = `Malformed multi-value property: ${token}`;
|
|
130
|
+
return state.failValidation('invalidPropertyName', message, (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation);
|
|
131
|
+
}
|
|
132
|
+
if (parts[1].includes('{{')) {
|
|
133
|
+
return (0, ts_utils_1.failWithDetail)('unresolved template', 'inapplicable');
|
|
134
|
+
}
|
|
135
|
+
const propertyVariable = parts[0];
|
|
136
|
+
const propertyValues = parts[1].split(',');
|
|
137
|
+
return (0, ts_utils_1.succeedWithDetail)({ token, propertyVariable, propertyValues, asArray });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.MultiValueJsonEditorRule = MultiValueJsonEditorRule;
|
|
141
|
+
//# sourceMappingURL=multivalue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multivalue.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/multivalue.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAQuB;AAKvB,sDAAuD;AAiCvD;;;;;;;;;;;GAWG;AACH,MAAa,wBAAyB,SAAQ,mCAAkB;IAQ9D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,wBAAwB,CAAC,OAAO,CAAC,CAAC,CAAC;IACpE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;QAEtB,MAAM,IAAI,GAAe,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;YAC5D,OAAO,IAAA,qBAAU,EACf,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE;gBAC9B,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE;oBAChF,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,SAAS,CAAC,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,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACxC,OAAO,GAAG,IAAI,CAAC;SAChB;aAAM,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YAChC,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACtC,OAAO,GAAG,KAAK,CAAC;SACjB;aAAM;YACL,OAAO,IAAA,yBAAc,EAAC,KAAK,EAAE,cAAc,CAAC,CAAC;SAC9C;QAED,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;YACtB,MAAM,OAAO,GAAG,mCAAmC,KAAK,EAAE,CAAC;YAC3D,OAAO,KAAK,CAAC,cAAc,CAAC,qBAAqB,EAAE,OAAO,EAAE,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC,CAAC;SACxF;QAED,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC3B,OAAO,IAAA,yBAAc,EAAC,qBAAqB,EAAE,cAAc,CAAC,CAAC;SAC9D;QAED,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,MAAM,cAAc,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,OAAO,IAAA,4BAAiB,EAAC,EAAE,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC;IACjF,CAAC;CACF;AA7HD,4DA6HC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport {\n DetailedResult,\n Result,\n allSucceed,\n captureResult,\n failWithDetail,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext, VariableValue } from '../../context';\nimport { JsonObject, JsonValue } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\n\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * Represents the parts of a multi-value property key.\n * @public\n */\nexport interface IMultiValuePropertyParts {\n /**\n * The original matched token.\n */\n readonly token: string;\n\n /**\n * The name of the variable used to project each possible\n * property value into the child values or objects being\n * resolved.\n */\n readonly propertyVariable: string;\n\n /**\n * The set of property values to be expanded.\n */\n readonly propertyValues: string[];\n\n /**\n * If `true`, the resolved values are added as an array\n * with the name of the {@link Editor.Rules.IMultiValuePropertyParts.propertyVariable | propertyVariable}.\n * If false, values are added as individual properties with names that correspond the value.\n */\n readonly asArray: boolean;\n}\n\n/**\n * The {@link Editor.Rules.MultiValueJsonEditorRule | Multi-Value JSON editor rule}\n * expands matching keys multiple times, projecting the value into the template\n * context for any child objects rendered by the rule.\n *\n * The default syntax for a multi-value key is:\n * \"[[var]]=value1,value2,value3\"\n * Where \"var\" is the name of the variable that will be passed to\n * child template resolution, and \"value1,value2,value3\" is a\n * comma-separated list of values to be expanded.\n * @public\n */\nexport class MultiValueJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options}\n * for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.MultiValueJsonEditorRule | MultiValueJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options}.\n */\n public static create(options?: IJsonEditorOptions): Result<MultiValueJsonEditorRule> {\n return captureResult(() => new MultiValueJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for multi-value expansion.\n * @param key - The key of the property to be considered\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with an object containing the fully-resolved child values to be merged for\n * matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs or\n * with detail `'inapplicable'` if the property key is not a conditional property.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n const json: JsonObject = {};\n const result = this._tryParse(key, state).onSuccess((parts) => {\n return allSucceed(\n parts.propertyValues.map((pv) => {\n return this._deriveContext(state, [parts.propertyVariable, pv]).onSuccess((ctx) => {\n return state.editor.clone(value, ctx).onSuccess((cloned) => {\n json[pv] = cloned;\n return succeedWithDetail(cloned);\n });\n });\n }),\n json\n )\n .onSuccess(() => {\n if (parts.asArray) {\n const arrayRtrn: JsonObject = {};\n arrayRtrn[parts.propertyVariable] = Array.from(Object.values(json));\n return succeed(arrayRtrn);\n }\n return succeed(json);\n })\n .withFailureDetail('error');\n });\n\n if (result.isFailure() && result.detail === 'error') {\n return state.failValidation('invalidPropertyName', result.message);\n }\n return result;\n }\n\n /**\n * Extends the {@link IJsonContext | current context} with a supplied state and values.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @param values - An array of {@link VariableValue | VariableValue} to be added to the\n * context.\n * @returns The extended {@link IJsonContext | context}.\n * @public\n */\n protected _deriveContext(\n state: JsonEditorState,\n ...values: VariableValue[]\n ): Result<IJsonContext | undefined> {\n return state.extendContext(this._options?.context, { vars: values });\n }\n\n /**\n * Determines if a given property key is multi-value. Derived classes can override this\n * method to use a different format for multi-value properties.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns `Success` with detail `'deferred'` and an\n * {@link Editor.Rules.IMultiValuePropertyParts | IMultiValuePropertyParts}\n * describing the match for matching multi-value property. Returns `Failure` with detail `'error'` if an error occurs\n * or with detail `'inapplicable'` if the key does not represent a multi-value property.\n * @public\n */\n protected _tryParse(\n token: string,\n state: JsonEditorState\n ): DetailedResult<IMultiValuePropertyParts, JsonEditFailureReason> {\n let parts: string[] = [];\n let asArray = false;\n\n if (token.startsWith('[[')) {\n parts = token.substring(2).split(']]=');\n asArray = true;\n } else if (token.startsWith('*')) {\n parts = token.substring(1).split('=');\n asArray = false;\n } else {\n return failWithDetail(token, 'inapplicable');\n }\n\n if (parts.length !== 2) {\n const message = `Malformed multi-value property: ${token}`;\n return state.failValidation('invalidPropertyName', message, this._options?.validation);\n }\n\n if (parts[1].includes('{{')) {\n return failWithDetail('unresolved template', 'inapplicable');\n }\n\n const propertyVariable = parts[0];\n const propertyValues = parts[1].split(',');\n return succeedWithDetail({ token, propertyVariable, propertyValues, asArray });\n }\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DetailedResult, Result } from '@fgv/ts-utils';
|
|
2
|
+
import { IJsonContext } from '../../context';
|
|
3
|
+
import { JsonObject, JsonValue } from '../../json';
|
|
4
|
+
import { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';
|
|
5
|
+
import { JsonEditorRuleBase } from '../jsonEditorRule';
|
|
6
|
+
import { JsonEditorState } from '../jsonEditorState';
|
|
7
|
+
/**
|
|
8
|
+
* The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
|
|
9
|
+
* keys or values that match some known object with a copy of that referenced object, formatted
|
|
10
|
+
* according to the current context.
|
|
11
|
+
*
|
|
12
|
+
* A property key is matched if it matches any known referenced value.
|
|
13
|
+
* - If the value of the matched key is `'default'`, then the entire object is formatted
|
|
14
|
+
* with the current context, flattened and merged into the current object.
|
|
15
|
+
* - If the value of the matched key is some other string, then the entire
|
|
16
|
+
* object is formatted with the current context, and the child of the resulting
|
|
17
|
+
* object at the specified path is flattened and merged into the current object.
|
|
18
|
+
* - If the value of the matched key is an object, then the entire object is
|
|
19
|
+
* formatted with the current context extended to include any properties of
|
|
20
|
+
* that object, flattened, and merged into the current object.
|
|
21
|
+
* - It is an error if the referenced value is not an object.
|
|
22
|
+
*
|
|
23
|
+
* Any property, array or literal value is matched if it matches any known
|
|
24
|
+
* value reference. The referenced value is replaced by the referenced
|
|
25
|
+
* value, formatted using the current editor context.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare class ReferenceJsonEditorRule extends JsonEditorRuleBase {
|
|
29
|
+
/**
|
|
30
|
+
* Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options} for this rule.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
protected _options?: IJsonEditorOptions;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
36
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.
|
|
37
|
+
*/
|
|
38
|
+
constructor(options?: IJsonEditorOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
41
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.
|
|
42
|
+
*/
|
|
43
|
+
static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule>;
|
|
44
|
+
/**
|
|
45
|
+
* Evaluates a property for reference expansion.
|
|
46
|
+
* @param key - The key of the property to be considered.
|
|
47
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
48
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
49
|
+
* @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}
|
|
50
|
+
* to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`
|
|
51
|
+
* for non-reference keys or with detail `'error'` if an error occurs.
|
|
52
|
+
*/
|
|
53
|
+
editProperty(key: string, value: JsonValue, state: JsonEditorState): DetailedResult<JsonObject, JsonPropertyEditFailureReason>;
|
|
54
|
+
/**
|
|
55
|
+
* Evaluates a property, array or literal value for reference replacement.
|
|
56
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
57
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
58
|
+
*/
|
|
59
|
+
editValue(value: JsonValue, state: JsonEditorState): DetailedResult<JsonValue, JsonEditFailureReason>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the template variables to use given the value of some property whose name matched a
|
|
62
|
+
* resource plus the base template context.
|
|
63
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} to be extended.
|
|
64
|
+
* @param supplied - The string or object supplied in the source json.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined>;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=references.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"references.d.ts","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,cAAc,EACd,MAAM,EAKP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAgC,MAAM,YAAY,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,EAAE,MAAM,WAAW,CAAC;AACrG,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,uBAAwB,SAAQ,kBAAkB;IAC7D;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAExC;;;OAGG;gBACgB,OAAO,CAAC,EAAE,kBAAkB;IAK/C;;;OAGG;WACW,MAAM,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAAC,uBAAuB,CAAC;IAInF;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,UAAU,EAAE,6BAA6B,CAAC;IA0C5D;;;;OAIG;IACI,SAAS,CACd,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,eAAe,GACrB,cAAc,CAAC,SAAS,EAAE,qBAAqB,CAAC;IAiBnD;;;;;;OAMG;IACH,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,SAAS,GAAG,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC;CASxG"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ReferenceJsonEditorRule = void 0;
|
|
25
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
|
+
const json_1 = require("../../json");
|
|
27
|
+
const jsonEditorRule_1 = require("../jsonEditorRule");
|
|
28
|
+
/**
|
|
29
|
+
* The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property
|
|
30
|
+
* keys or values that match some known object with a copy of that referenced object, formatted
|
|
31
|
+
* according to the current context.
|
|
32
|
+
*
|
|
33
|
+
* A property key is matched if it matches any known referenced value.
|
|
34
|
+
* - If the value of the matched key is `'default'`, then the entire object is formatted
|
|
35
|
+
* with the current context, flattened and merged into the current object.
|
|
36
|
+
* - If the value of the matched key is some other string, then the entire
|
|
37
|
+
* object is formatted with the current context, and the child of the resulting
|
|
38
|
+
* object at the specified path is flattened and merged into the current object.
|
|
39
|
+
* - If the value of the matched key is an object, then the entire object is
|
|
40
|
+
* formatted with the current context extended to include any properties of
|
|
41
|
+
* that object, flattened, and merged into the current object.
|
|
42
|
+
* - It is an error if the referenced value is not an object.
|
|
43
|
+
*
|
|
44
|
+
* Any property, array or literal value is matched if it matches any known
|
|
45
|
+
* value reference. The referenced value is replaced by the referenced
|
|
46
|
+
* value, formatted using the current editor context.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
class ReferenceJsonEditorRule extends jsonEditorRule_1.JsonEditorRuleBase {
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
52
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.
|
|
53
|
+
*/
|
|
54
|
+
constructor(options) {
|
|
55
|
+
super();
|
|
56
|
+
this._options = options;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.
|
|
60
|
+
* @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.
|
|
61
|
+
*/
|
|
62
|
+
static create(options) {
|
|
63
|
+
return (0, ts_utils_1.captureResult)(() => new ReferenceJsonEditorRule(options));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Evaluates a property for reference expansion.
|
|
67
|
+
* @param key - The key of the property to be considered.
|
|
68
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
69
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
70
|
+
* @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}
|
|
71
|
+
* to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`
|
|
72
|
+
* for non-reference keys or with detail `'error'` if an error occurs.
|
|
73
|
+
*/
|
|
74
|
+
editProperty(key, value, state) {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
// istanbul ignore next
|
|
77
|
+
const validation = (_a = this._options) === null || _a === void 0 ? void 0 : _a.validation;
|
|
78
|
+
// istanbul ignore next
|
|
79
|
+
const refs = state.getRefs((_b = this._options) === null || _b === void 0 ? void 0 : _b.context);
|
|
80
|
+
if (refs === null || refs === void 0 ? void 0 : refs.has(key)) {
|
|
81
|
+
// need to apply any rules to the value before we evaluate it
|
|
82
|
+
const cloneResult = state.editor.clone(value, state.context);
|
|
83
|
+
if (cloneResult.isSuccess()) {
|
|
84
|
+
value = cloneResult.value;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const message = `${key}: ${cloneResult.message}`;
|
|
88
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
89
|
+
}
|
|
90
|
+
const contextResult = this._extendContext(state, value);
|
|
91
|
+
if (contextResult.isSuccess()) {
|
|
92
|
+
const objResult = refs.getJsonObject(key, contextResult.value);
|
|
93
|
+
// guarded by the has above so should never happen
|
|
94
|
+
// istanbul ignore else
|
|
95
|
+
if (objResult.isSuccess()) {
|
|
96
|
+
if (typeof value !== 'string' || value === 'default') {
|
|
97
|
+
return (0, ts_utils_1.succeedWithDetail)(objResult.value, 'edited');
|
|
98
|
+
}
|
|
99
|
+
const pickResult = (0, json_1.pickJsonObject)(objResult.value, value);
|
|
100
|
+
if (pickResult.isFailure()) {
|
|
101
|
+
const message = `${key}: ${pickResult.message}`;
|
|
102
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
103
|
+
}
|
|
104
|
+
return pickResult.withDetail('edited');
|
|
105
|
+
}
|
|
106
|
+
else if (objResult.detail !== 'unknown') {
|
|
107
|
+
const message = `${key}: ${objResult.message}`;
|
|
108
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const message = `${key}: ${contextResult.message}`;
|
|
113
|
+
return state.failValidation('invalidPropertyName', message, validation);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return (0, ts_utils_1.failWithDetail)('inapplicable', 'inapplicable');
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Evaluates a property, array or literal value for reference replacement.
|
|
120
|
+
* @param value - The {@link JsonValue | value} of the property to be considered.
|
|
121
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.
|
|
122
|
+
*/
|
|
123
|
+
editValue(value, state) {
|
|
124
|
+
var _a, _b, _c;
|
|
125
|
+
// istanbul ignore next
|
|
126
|
+
const refs = state.getRefs((_a = this._options) === null || _a === void 0 ? void 0 : _a.context);
|
|
127
|
+
if (refs && typeof value === 'string') {
|
|
128
|
+
// istanbul ignore next
|
|
129
|
+
const context = state.getContext((_b = this._options) === null || _b === void 0 ? void 0 : _b.context);
|
|
130
|
+
const result = refs.getJsonValue(value, context);
|
|
131
|
+
if (result.isSuccess()) {
|
|
132
|
+
return (0, ts_utils_1.succeedWithDetail)(result.value, 'edited');
|
|
133
|
+
}
|
|
134
|
+
else if (result.detail === 'error') {
|
|
135
|
+
return state.failValidation('invalidPropertyValue', result.message, (_c = this._options) === null || _c === void 0 ? void 0 : _c.validation);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return (0, ts_utils_1.failWithDetail)('inapplicable', 'inapplicable');
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Gets the template variables to use given the value of some property whose name matched a
|
|
142
|
+
* resource plus the base template context.
|
|
143
|
+
* @param state - The {@link Editor.JsonEditorState | editor state} to be extended.
|
|
144
|
+
* @param supplied - The string or object supplied in the source json.
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
_extendContext(state, supplied) {
|
|
148
|
+
var _a;
|
|
149
|
+
const add = {};
|
|
150
|
+
if ((0, json_1.isJsonObject)(supplied)) {
|
|
151
|
+
add.vars = Object.entries(supplied);
|
|
152
|
+
}
|
|
153
|
+
else if (typeof supplied !== 'string') {
|
|
154
|
+
return (0, ts_utils_1.fail)(`Invalid template path or context: "${JSON.stringify(supplied)}"`);
|
|
155
|
+
}
|
|
156
|
+
return state.extendContext((_a = this._options) === null || _a === void 0 ? void 0 : _a.context, add);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
exports.ReferenceJsonEditorRule = ReferenceJsonEditorRule;
|
|
160
|
+
//# sourceMappingURL=references.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"references.js","sourceRoot":"","sources":["../../../../src/packlets/editor/rules/references.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAOuB;AAEvB,qCAAiF;AAEjF,sDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,uBAAwB,SAAQ,mCAAkB;IAO7D;;;OAGG;IACH,YAAmB,OAA4B;QAC7C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,OAA4B;QAC/C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;;OAQG;IACI,YAAY,CACjB,GAAW,EACX,KAAgB,EAChB,KAAsB;;QAEtB,uBAAuB;QACvB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,QAAQ,0CAAE,UAAU,CAAC;QAC7C,uBAAuB;QACvB,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,CAAC,CAAC;QACnD,IAAI,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,6DAA6D;YAC7D,MAAM,WAAW,GAAG,KAAK,CAAC,MAA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AAE,CAAC;QACxC,IAAI,IAAA,mBAAY,EAAC,QAAQ,CAAC,EAAE;YAC1B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;SACrC;aAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;YACvC,OAAO,IAAA,eAAI,EAAC,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;SAChF;QACD,OAAO,KAAK,CAAC,aAAa,CAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1D,CAAC;CACF;AAxHD,0DAwHC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport {\n DetailedResult,\n Result,\n captureResult,\n fail,\n failWithDetail,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { IJsonContext } from '../../context';\nimport { JsonObject, JsonValue, isJsonObject, pickJsonObject } from '../../json';\nimport { IJsonEditorOptions, JsonEditFailureReason, JsonPropertyEditFailureReason } from '../common';\nimport { JsonEditorRuleBase } from '../jsonEditorRule';\nimport { JsonEditorState } from '../jsonEditorState';\n\n/**\n * The {@link Editor.Rules.ReferenceJsonEditorRule | Reference JSON editor rule} replaces property\n * keys or values that match some known object with a copy of that referenced object, formatted\n * according to the current context.\n *\n * A property key is matched if it matches any known referenced value.\n * - If the value of the matched key is `'default'`, then the entire object is formatted\n * with the current context, flattened and merged into the current object.\n * - If the value of the matched key is some other string, then the entire\n * object is formatted with the current context, and the child of the resulting\n * object at the specified path is flattened and merged into the current object.\n * - If the value of the matched key is an object, then the entire object is\n * formatted with the current context extended to include any properties of\n * that object, flattened, and merged into the current object.\n * - It is an error if the referenced value is not an object.\n *\n * Any property, array or literal value is matched if it matches any known\n * value reference. The referenced value is replaced by the referenced\n * value, formatted using the current editor context.\n * @public\n */\nexport class ReferenceJsonEditorRule extends JsonEditorRuleBase {\n /**\n * Stored fully-resolved {@link Editor.IJsonEditorOptions | editor options} for this rule.\n * @public\n */\n protected _options?: IJsonEditorOptions;\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public constructor(options?: IJsonEditorOptions) {\n super();\n this._options = options;\n }\n\n /**\n * Creates a new {@link Editor.Rules.ReferenceJsonEditorRule | ReferenceJsonEditorRule}.\n * @param options - Optional {@link Editor.IJsonEditorOptions | configuration options} for this rule.\n */\n public static create(options?: IJsonEditorOptions): Result<ReferenceJsonEditorRule> {\n return captureResult(() => new ReferenceJsonEditorRule(options));\n }\n\n /**\n * Evaluates a property for reference expansion.\n * @param key - The key of the property to be considered.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n * @returns If the reference is successful, returns `Success` with a {@link JsonObject | JsonObject}\n * to be flattened and merged into the current object. Returns `Failure` with detail `'inapplicable'`\n * for non-reference keys or with detail `'error'` if an error occurs.\n */\n public editProperty(\n key: string,\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonObject, JsonPropertyEditFailureReason> {\n // istanbul ignore next\n const validation = this._options?.validation;\n // istanbul ignore next\n const refs = state.getRefs(this._options?.context);\n if (refs?.has(key)) {\n // need to apply any rules to the value before we evaluate it\n const cloneResult = state.editor.clone(value, state.context);\n if (cloneResult.isSuccess()) {\n value = cloneResult.value;\n } else {\n const message = `${key}: ${cloneResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n\n const contextResult = this._extendContext(state, value);\n if (contextResult.isSuccess()) {\n const objResult = refs.getJsonObject(key, contextResult.value);\n // guarded by the has above so should never happen\n // istanbul ignore else\n if (objResult.isSuccess()) {\n if (typeof value !== 'string' || value === 'default') {\n return succeedWithDetail<JsonObject, JsonEditFailureReason>(objResult.value, 'edited');\n }\n const pickResult = pickJsonObject(objResult.value, value);\n if (pickResult.isFailure()) {\n const message = `${key}: ${pickResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n return pickResult.withDetail('edited');\n } else if (objResult.detail !== 'unknown') {\n const message = `${key}: ${objResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n } else {\n const message = `${key}: ${contextResult.message}`;\n return state.failValidation('invalidPropertyName', message, validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Evaluates a property, array or literal value for reference replacement.\n * @param value - The {@link JsonValue | value} of the property to be considered.\n * @param state - The {@link Editor.JsonEditorState | editor state} for the object being edited.\n */\n public editValue(\n value: JsonValue,\n state: JsonEditorState\n ): DetailedResult<JsonValue, JsonEditFailureReason> {\n // istanbul ignore next\n const refs = state.getRefs(this._options?.context);\n\n if (refs && typeof value === 'string') {\n // istanbul ignore next\n const context = state.getContext(this._options?.context);\n const result = refs.getJsonValue(value, context);\n if (result.isSuccess()) {\n return succeedWithDetail(result.value, 'edited');\n } else if (result.detail === 'error') {\n return state.failValidation('invalidPropertyValue', result.message, this._options?.validation);\n }\n }\n return failWithDetail('inapplicable', 'inapplicable');\n }\n\n /**\n * Gets the template variables to use given the value of some property whose name matched a\n * resource plus the base template context.\n * @param state - The {@link Editor.JsonEditorState | editor state} to be extended.\n * @param supplied - The string or object supplied in the source json.\n * @internal\n */\n protected _extendContext(state: JsonEditorState, supplied: JsonValue): Result<IJsonContext | undefined> {\n const add: Record<string, unknown> = {};\n if (isJsonObject(supplied)) {\n add.vars = Object.entries(supplied);\n } else if (typeof supplied !== 'string') {\n return fail(`Invalid template path or context: \"${JSON.stringify(supplied)}\"`);\n }\n return state.extendContext(this._options?.context, add);\n }\n}\n"]}
|