@featurevisor/core 4.0.1 → 4.3.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/lib/builder/buildProject.js +24 -6
- package/lib/builder/buildProject.js.map +1 -1
- package/lib/builder/schemaV2.d.ts +47 -0
- package/lib/builder/schemaV2.js +529 -0
- package/lib/builder/schemaV2.js.map +1 -0
- package/lib/catalog.js +10 -2
- package/lib/catalog.js.map +1 -1
- package/lib/generate-code/typescript.js +26 -0
- package/lib/generate-code/typescript.js.map +1 -1
- package/lib/linter/lintProject.js +45 -0
- package/lib/linter/lintProject.js.map +1 -1
- package/lib/linter/schemaV2Targets.d.ts +16 -0
- package/lib/linter/schemaV2Targets.js +62 -0
- package/lib/linter/schemaV2Targets.js.map +1 -0
- package/lib/linter/targetSchema.d.ts +4 -0
- package/lib/linter/targetSchema.js +5 -0
- package/lib/linter/targetSchema.js.map +1 -1
- package/package.json +5 -5
|
@@ -41,6 +41,7 @@ const path = __importStar(require("path"));
|
|
|
41
41
|
const revision_1 = require("./revision");
|
|
42
42
|
const buildDatafile_1 = require("./buildDatafile");
|
|
43
43
|
const applyContextToDatafile_1 = require("./applyContextToDatafile");
|
|
44
|
+
const schemaV2_1 = require("./schemaV2");
|
|
44
45
|
const hashes_1 = require("./hashes");
|
|
45
46
|
const sets_1 = require("../sets");
|
|
46
47
|
const cliFormat_1 = require("../tester/cliFormat");
|
|
@@ -106,14 +107,30 @@ async function buildTargetDatafile({ projectConfig, datasource, target, environm
|
|
|
106
107
|
featurevisorVersion,
|
|
107
108
|
dependencyGraph,
|
|
108
109
|
}, existingState);
|
|
110
|
+
let result = datafileContent;
|
|
109
111
|
if (target.context) {
|
|
110
|
-
|
|
111
|
-
if (revisionFromHash) {
|
|
112
|
-
|
|
112
|
+
result = (0, applyContextToDatafile_1.applyContextToDatafile)(result, target.context);
|
|
113
|
+
if (revisionFromHash && target.schemaVersion !== schemaV2_1.SCHEMA_V2) {
|
|
114
|
+
result.revision = (0, hashes_1.generateHashForDatafile)(result);
|
|
113
115
|
}
|
|
114
|
-
return contextualDatafile;
|
|
115
116
|
}
|
|
116
|
-
return
|
|
117
|
+
return finishTargetDatafile(result, target, projectConfig, environment, revisionFromHash);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Write the schema the target asks for. Only targets with schemaVersion "2"
|
|
121
|
+
* pay for the conversion, and only they can fail it.
|
|
122
|
+
*/
|
|
123
|
+
function finishTargetDatafile(datafile, target, projectConfig, environment, revisionFromHash) {
|
|
124
|
+
if (target?.schemaVersion !== schemaV2_1.SCHEMA_V2)
|
|
125
|
+
return datafile;
|
|
126
|
+
const converted = (0, schemaV2_1.toSchemaV2)(datafile, {
|
|
127
|
+
stringify: projectConfig.stringify,
|
|
128
|
+
targetKey: target.key,
|
|
129
|
+
environment,
|
|
130
|
+
});
|
|
131
|
+
if (revisionFromHash)
|
|
132
|
+
converted.revision = (0, hashes_1.generateHashForDatafile)(converted);
|
|
133
|
+
return converted;
|
|
117
134
|
}
|
|
118
135
|
async function buildProject(deps, cliOptions = {}) {
|
|
119
136
|
if (cliOptions.inflate !== undefined) {
|
|
@@ -163,10 +180,11 @@ async function buildProject(deps, cliOptions = {}) {
|
|
|
163
180
|
});
|
|
164
181
|
if (target?.context) {
|
|
165
182
|
datafileContent = (0, applyContextToDatafile_1.applyContextToDatafile)(datafileContent, target.context);
|
|
166
|
-
if (cliOptions.revisionFromHash) {
|
|
183
|
+
if (cliOptions.revisionFromHash && target.schemaVersion !== schemaV2_1.SCHEMA_V2) {
|
|
167
184
|
datafileContent.revision = (0, hashes_1.generateHashForDatafile)(datafileContent);
|
|
168
185
|
}
|
|
169
186
|
}
|
|
187
|
+
datafileContent = finishTargetDatafile(datafileContent, target, projectConfig, environment, cliOptions.revisionFromHash);
|
|
170
188
|
if (cliOptions.pretty) {
|
|
171
189
|
console.log(JSON.stringify(datafileContent, null, 2));
|
|
172
190
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildProject.js","sourceRoot":"","sources":["../../src/builder/buildProject.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"buildProject.js","sourceRoot":"","sources":["../../src/builder/buildProject.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuGA,kDAqDC;AAuBD,oCAwGC;AAED,4CA2DC;AAxVD,2CAA6B;AAK7B,yCAA6C;AAC7C,mDAAmE;AACnE,qEAAkE;AAClE,yCAAmD;AACnD,qCAAmD;AAKnD,kCAAiG;AACjG,mDAAkG;AAClG,4CAA8C;AAC9C,iDAA0D;AAC1D,kDAA+D;AAoB/D,SAAS,sBAAsB;IAC7B,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,gCAAgC,CAAC,CAAC,CAAC;QAE9E,OAAO,UAAU,CAAC,OAAO,CAAC;QAC1B,2BAA2B;IAC7B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,KAAK,UAAU,mBAAmB,CAAC,EACjC,aAAa,EACb,UAAU,EACV,YAAY,EACZ,WAAW,EACX,OAAO,EACP,UAAU,GAQX;IACC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,IAAI,WAAW,KAAK,KAAK,EAAE,CAAC;QAC1B,OAAO,CAAC,GAAG,CAAC,2BAAe,EAAE,gBAAgB,WAAW,GAAG,CAAC,CAAC;IAC/D,CAAC;IAED,MAAM,aAAa,GAAG,MAAM,UAAU,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,mBAAmB,GAAG,sBAAsB,EAAE,CAAC;IAErD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,OAAO,CAAC,GAAG,CAAC,KAAK,IAAA,oBAAQ,EA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,CAAC;QACD,MAAM,UAAU,CAAC,aAAa,CAAC,YAAY,EAAE,UAAU,CAAC,YAAY,CAAC,CAAC;QACtE,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChB,OAAO,CAAC,GAAG,CAAC,4BAAgB,EAAE,yBAAyB,CAAC,CAAC;QACzD,OAAO,CAAC,GAAG,CAAC,2BAAe,EAAE,4BAA4B,YAAY,EAAE,CAAC,CAAC;IAC3E,CAAC;AACH,CAAC;AAEY,QAAA,WAAW,GAAW;IACjC,OAAO,EAAE,OAAO;IAChB,OAAO,EAAE,KAAK,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE;QAC/E,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;YACjB,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC;QACrB,CAAC;QAED,MAAM,gBAAgB,CACpB;YACE,iBAAiB;YACjB,aAAa;YACb,UAAU;YACV,OAAO,EAAE,MAAM;SAChB,EACD,MAAyB,CAC1B,CAAC;IACJ,CAAC;IACD,QAAQ,EAAE;QACR;YACE,OAAO,EAAE,OAAO;YAChB,WAAW,EAAE,mEAAmE;SACjF;QACD;YACE,OAAO,EAAE,sBAAsB;YAC/B,WAAW,EAAE,qDAAqD;SACnE;QACD;YACE,OAAO,EAAE,gCAAgC;YACzC,WAAW,EAAE,8DAA8D;SAC5E;QACD;YACE,OAAO,EAAE,uDAAuD;YAChE,WAAW,EAAE,gCAAgC;SAC9C;QACD;YACE,OAAO,EAAE,wBAAwB;YACjC,WAAW,EAAE,4BAA4B;SAC1C;QACD;YACE,OAAO,EAAE,wBAAwB;YACjC,WAAW,EAAE,6CAA6C;SAC3D;KACF;CACF,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { DatafileContent } from "@featurevisor/types";
|
|
2
|
+
import { FeaturevisorCLIError } from "../error";
|
|
3
|
+
/**
|
|
4
|
+
* Schema "2" is the datafile format of Featurevisor v2 and v3. Targets that
|
|
5
|
+
* set `schemaVersion: "2"` are converted for the latest v3 JavaScript SDK,
|
|
6
|
+
* @featurevisor/sdk 3.8, which reads:
|
|
7
|
+
*
|
|
8
|
+
* - rule and force `segments`, made of segment keys with `and`, `or` and `not`
|
|
9
|
+
* - force and override `conditions` of attributes only
|
|
10
|
+
* - flat `requiredFeatures` lists on features, global variables and overrides
|
|
11
|
+
* - segments whose conditions compare attributes only
|
|
12
|
+
*
|
|
13
|
+
* Anything else has no schema version "2" form and is reported as an issue, never
|
|
14
|
+
* dropped, so an older application cannot silently receive a different
|
|
15
|
+
* configuration.
|
|
16
|
+
*/
|
|
17
|
+
export declare const SCHEMA_V2 = "2";
|
|
18
|
+
export type SchemaV2EntityType = "feature" | "segment" | "variable";
|
|
19
|
+
export interface SchemaV2Issue {
|
|
20
|
+
entityType: SchemaV2EntityType;
|
|
21
|
+
key: string;
|
|
22
|
+
/** Where in the definition, such as `["rules", "everyone", "conditions"]`. */
|
|
23
|
+
path: (string | number)[];
|
|
24
|
+
/** Where in words, such as `rule "everyone"`. */
|
|
25
|
+
location: string;
|
|
26
|
+
reason: string;
|
|
27
|
+
}
|
|
28
|
+
export interface SchemaV2Result {
|
|
29
|
+
datafile: DatafileContent;
|
|
30
|
+
issues: SchemaV2Issue[];
|
|
31
|
+
}
|
|
32
|
+
export interface SchemaV2Options {
|
|
33
|
+
stringify?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** A CLI error, so `--json` output carries its code and every issue. */
|
|
36
|
+
export declare class SchemaV2ConversionError extends FeaturevisorCLIError {
|
|
37
|
+
readonly issues: SchemaV2Issue[];
|
|
38
|
+
readonly targetKey?: string;
|
|
39
|
+
constructor(issues: SchemaV2Issue[], targetKey?: string, environment?: string | false);
|
|
40
|
+
}
|
|
41
|
+
export declare function describeIssue(issue: SchemaV2Issue): string;
|
|
42
|
+
export declare function convertDatafileToSchemaV2(datafile: DatafileContent, options?: SchemaV2Options): SchemaV2Result;
|
|
43
|
+
/** Convert, or throw every issue at once. */
|
|
44
|
+
export declare function toSchemaV2(datafile: DatafileContent, options?: SchemaV2Options & {
|
|
45
|
+
targetKey?: string;
|
|
46
|
+
environment?: string | false;
|
|
47
|
+
}): DatafileContent;
|
|
@@ -0,0 +1,529 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.SchemaV2ConversionError = exports.SCHEMA_V2 = void 0;
|
|
37
|
+
exports.describeIssue = describeIssue;
|
|
38
|
+
exports.convertDatafileToSchemaV2 = convertDatafileToSchemaV2;
|
|
39
|
+
exports.toSchemaV2 = toSchemaV2;
|
|
40
|
+
const crypto = __importStar(require("crypto"));
|
|
41
|
+
const conditions_1 = require("../datasource/conditions");
|
|
42
|
+
const error_1 = require("../error");
|
|
43
|
+
/**
|
|
44
|
+
* Schema "2" is the datafile format of Featurevisor v2 and v3. Targets that
|
|
45
|
+
* set `schemaVersion: "2"` are converted for the latest v3 JavaScript SDK,
|
|
46
|
+
* @featurevisor/sdk 3.8, which reads:
|
|
47
|
+
*
|
|
48
|
+
* - rule and force `segments`, made of segment keys with `and`, `or` and `not`
|
|
49
|
+
* - force and override `conditions` of attributes only
|
|
50
|
+
* - flat `requiredFeatures` lists on features, global variables and overrides
|
|
51
|
+
* - segments whose conditions compare attributes only
|
|
52
|
+
*
|
|
53
|
+
* Anything else has no schema version "2" form and is reported as an issue, never
|
|
54
|
+
* dropped, so an older application cannot silently receive a different
|
|
55
|
+
* configuration.
|
|
56
|
+
*/
|
|
57
|
+
exports.SCHEMA_V2 = "2";
|
|
58
|
+
const V4_ONLY_OPERATORS = new Set([
|
|
59
|
+
"isNull",
|
|
60
|
+
"notNull",
|
|
61
|
+
"empty",
|
|
62
|
+
"notEmpty",
|
|
63
|
+
"includesAny",
|
|
64
|
+
"notIncludesAny",
|
|
65
|
+
]);
|
|
66
|
+
// Empty groups are false in v4. In schema version "2" an empty `and` is true, while an
|
|
67
|
+
// empty `or` is false in both, so it stands for false.
|
|
68
|
+
const NEVER = { or: [] };
|
|
69
|
+
/** A CLI error, so `--json` output carries its code and every issue. */
|
|
70
|
+
class SchemaV2ConversionError extends error_1.FeaturevisorCLIError {
|
|
71
|
+
constructor(issues, targetKey, environment) {
|
|
72
|
+
const where = [
|
|
73
|
+
targetKey ? `Target "${targetKey}"` : "This datafile",
|
|
74
|
+
environment ? `in environment "${environment}"` : "",
|
|
75
|
+
]
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.join(" ");
|
|
78
|
+
super(`${where} sets schemaVersion "2" for @featurevisor/sdk v3, which cannot express:\n` +
|
|
79
|
+
issues.map((issue) => ` - ${describeIssue(issue)}`).join("\n") +
|
|
80
|
+
`\nKeep these definitions out of the target, or write them in a form schema version "2" supports. Run featurevisor lint to see each file.`, {
|
|
81
|
+
code: "schema_v2_unsupported",
|
|
82
|
+
details: {
|
|
83
|
+
target: targetKey ?? null,
|
|
84
|
+
environment: environment || null,
|
|
85
|
+
issues: issues.map(({ entityType, key, path, reason }) => ({
|
|
86
|
+
entityType,
|
|
87
|
+
key,
|
|
88
|
+
path,
|
|
89
|
+
reason,
|
|
90
|
+
})),
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
this.name = "SchemaV2ConversionError";
|
|
94
|
+
this.issues = issues;
|
|
95
|
+
this.targetKey = targetKey;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.SchemaV2ConversionError = SchemaV2ConversionError;
|
|
99
|
+
function describeIssue(issue) {
|
|
100
|
+
return `${issue.entityType} "${issue.key}", ${issue.location}: ${issue.reason}`;
|
|
101
|
+
}
|
|
102
|
+
function isGroup(value) {
|
|
103
|
+
return (typeof value === "object" &&
|
|
104
|
+
value !== null &&
|
|
105
|
+
("and" in value || "or" in value || "not" in value));
|
|
106
|
+
}
|
|
107
|
+
function children(value) {
|
|
108
|
+
if (Array.isArray(value))
|
|
109
|
+
return value;
|
|
110
|
+
const group = value;
|
|
111
|
+
return group.and || group.or || group.not || [];
|
|
112
|
+
}
|
|
113
|
+
function kindOf(leaf) {
|
|
114
|
+
if (typeof leaf === "string")
|
|
115
|
+
return "segment";
|
|
116
|
+
if ("feature" in leaf && !("variable" in leaf))
|
|
117
|
+
return "feature";
|
|
118
|
+
if ("variable" in leaf)
|
|
119
|
+
return "variable";
|
|
120
|
+
return "attribute";
|
|
121
|
+
}
|
|
122
|
+
/** Every leaf kind under a node, so callers can choose a schema version "2" form. */
|
|
123
|
+
function kinds(value, out = new Set()) {
|
|
124
|
+
if (value === "*")
|
|
125
|
+
return out;
|
|
126
|
+
if (Array.isArray(value) || isGroup(value)) {
|
|
127
|
+
for (const child of children(value))
|
|
128
|
+
kinds(child, out);
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
out.add(kindOf(value));
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/** The first leaf of a kind, for naming it in an issue. */
|
|
135
|
+
function firstLeaf(value, kind) {
|
|
136
|
+
if (value === "*")
|
|
137
|
+
return undefined;
|
|
138
|
+
if (Array.isArray(value) || isGroup(value)) {
|
|
139
|
+
for (const child of children(value)) {
|
|
140
|
+
const found = firstLeaf(child, kind);
|
|
141
|
+
if (found !== undefined)
|
|
142
|
+
return found;
|
|
143
|
+
}
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
return kindOf(value) === kind ? value : undefined;
|
|
147
|
+
}
|
|
148
|
+
/** A copy without fields that schema version "2" writes differently. */
|
|
149
|
+
function without(value, keys) {
|
|
150
|
+
const result = { ...value };
|
|
151
|
+
for (const key of keys)
|
|
152
|
+
delete result[key];
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
function shortHash(value) {
|
|
156
|
+
return crypto.createHash("sha256").update(value).digest("hex").slice(0, 10);
|
|
157
|
+
}
|
|
158
|
+
function convertDatafileToSchemaV2(datafile, options = {}) {
|
|
159
|
+
const stringify = options.stringify === true;
|
|
160
|
+
const issues = [];
|
|
161
|
+
const v4Segments = datafile.segments || {};
|
|
162
|
+
const segments = {};
|
|
163
|
+
const synthesized = new Map();
|
|
164
|
+
const report = (scope, reason) => issues.push({ ...scope, reason });
|
|
165
|
+
const describeLeaf = (leaf) => {
|
|
166
|
+
if (typeof leaf === "string")
|
|
167
|
+
return `segment "${leaf}"`;
|
|
168
|
+
if ("variable" in leaf)
|
|
169
|
+
return leaf.feature
|
|
170
|
+
? `variable "${leaf.variable}" of feature "${leaf.feature}"`
|
|
171
|
+
: `variable "${leaf.variable}"`;
|
|
172
|
+
if ("feature" in leaf)
|
|
173
|
+
return `feature "${leaf.feature}"`;
|
|
174
|
+
return `attribute "${leaf.attribute}"`;
|
|
175
|
+
};
|
|
176
|
+
/** An attribute leaf, refusing operators the v3 SDK does not know. */
|
|
177
|
+
const attribute = (leaf, scope) => {
|
|
178
|
+
if (V4_ONLY_OPERATORS.has(leaf.operator)) {
|
|
179
|
+
report(scope, `uses operator "${leaf.operator}", which @featurevisor/sdk v3 does not support`);
|
|
180
|
+
}
|
|
181
|
+
return { ...leaf };
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Attribute conditions, the schema version "2" form for segments, force entries and
|
|
185
|
+
* overrides. Segment references are replaced by their own conditions.
|
|
186
|
+
*/
|
|
187
|
+
const toConditions = (value, scope, visiting = new Set()) => {
|
|
188
|
+
if (value === "*")
|
|
189
|
+
return "*";
|
|
190
|
+
if (typeof value === "string") {
|
|
191
|
+
const segment = v4Segments[value];
|
|
192
|
+
if (!segment || visiting.has(value))
|
|
193
|
+
return NEVER;
|
|
194
|
+
const next = new Set(visiting);
|
|
195
|
+
next.add(value);
|
|
196
|
+
return toConditions((0, conditions_1.parseConditions)(segment.conditions), scope, next);
|
|
197
|
+
}
|
|
198
|
+
if (Array.isArray(value)) {
|
|
199
|
+
return value.length
|
|
200
|
+
? value.map((child) => toConditions(child, scope, visiting))
|
|
201
|
+
: NEVER;
|
|
202
|
+
}
|
|
203
|
+
if (isGroup(value)) {
|
|
204
|
+
const list = children(value);
|
|
205
|
+
if (!list.length)
|
|
206
|
+
return NEVER;
|
|
207
|
+
const mapped = list.map((child) => toConditions(child, scope, visiting));
|
|
208
|
+
if ("and" in value)
|
|
209
|
+
return { and: mapped };
|
|
210
|
+
if ("or" in value)
|
|
211
|
+
return { or: mapped };
|
|
212
|
+
return { not: mapped };
|
|
213
|
+
}
|
|
214
|
+
const kind = kindOf(value);
|
|
215
|
+
if (kind === "attribute")
|
|
216
|
+
return attribute(value, scope);
|
|
217
|
+
report(scope, kind === "variable"
|
|
218
|
+
? `reads ${describeLeaf(value)}, and schema version "2" has no variable conditions`
|
|
219
|
+
: `requires ${describeLeaf(value)} where schema version "2" can only compare attributes`);
|
|
220
|
+
return NEVER;
|
|
221
|
+
};
|
|
222
|
+
/** A segment for an attribute only part of a rule or force entry. */
|
|
223
|
+
const synthesize = (value, scope) => {
|
|
224
|
+
const conditions = toConditions(value, scope);
|
|
225
|
+
const key = `~${shortHash(JSON.stringify(conditions))}`;
|
|
226
|
+
synthesized.set(key, conditions);
|
|
227
|
+
return key;
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Segment references with `and`, `or` and `not`, the schema version "2" form for
|
|
231
|
+
* rules. Attribute only parts become segments of their own.
|
|
232
|
+
*/
|
|
233
|
+
const toSegments = (value, scope) => {
|
|
234
|
+
if (value === "*")
|
|
235
|
+
return "*";
|
|
236
|
+
if (typeof value === "string")
|
|
237
|
+
return value;
|
|
238
|
+
const found = kinds(value);
|
|
239
|
+
if (found.size === 1 && found.has("attribute"))
|
|
240
|
+
return synthesize(value, scope);
|
|
241
|
+
if (Array.isArray(value)) {
|
|
242
|
+
return value.length
|
|
243
|
+
? value.map((child) => toSegments(child, scope))
|
|
244
|
+
: NEVER;
|
|
245
|
+
}
|
|
246
|
+
if (isGroup(value)) {
|
|
247
|
+
const list = children(value);
|
|
248
|
+
if (!list.length)
|
|
249
|
+
return NEVER;
|
|
250
|
+
const mapped = list.map((child) => toSegments(child, scope));
|
|
251
|
+
if ("and" in value)
|
|
252
|
+
return { and: mapped };
|
|
253
|
+
if ("or" in value)
|
|
254
|
+
return { or: mapped };
|
|
255
|
+
return { not: mapped };
|
|
256
|
+
}
|
|
257
|
+
const kind = kindOf(value);
|
|
258
|
+
report(scope, kind === "variable"
|
|
259
|
+
? `reads ${describeLeaf(value)}, and schema version "2" has no variable conditions`
|
|
260
|
+
: `requires ${describeLeaf(value)}, and schema version "2" rules and force entries cannot require features`);
|
|
261
|
+
return NEVER;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Top level `and` parts: feature leaves become a flat `requiredFeatures`
|
|
265
|
+
* list, the rest stays a selector. A feature leaf under `or` or `not` has
|
|
266
|
+
* no flat form.
|
|
267
|
+
*/
|
|
268
|
+
const split = (value) => {
|
|
269
|
+
const features = [];
|
|
270
|
+
const rest = [];
|
|
271
|
+
const visit = (entry) => {
|
|
272
|
+
if (entry === "*")
|
|
273
|
+
return;
|
|
274
|
+
if (Array.isArray(entry))
|
|
275
|
+
return entry.forEach(visit);
|
|
276
|
+
if (typeof entry === "object" && "and" in entry && entry.and.length)
|
|
277
|
+
return entry.and.forEach(visit);
|
|
278
|
+
if (typeof entry === "object" && kindOf(entry) === "feature") {
|
|
279
|
+
features.push(entry);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
rest.push(entry);
|
|
283
|
+
};
|
|
284
|
+
if (value !== undefined)
|
|
285
|
+
visit(value);
|
|
286
|
+
return { features, rest };
|
|
287
|
+
};
|
|
288
|
+
const required = (features) => features.map((leaf) => leaf.enabled === undefined && leaf.variation === undefined
|
|
289
|
+
? leaf.feature
|
|
290
|
+
: {
|
|
291
|
+
feature: leaf.feature,
|
|
292
|
+
...(leaf.enabled !== undefined ? { enabled: leaf.enabled } : {}),
|
|
293
|
+
...(leaf.variation !== undefined ? { variation: leaf.variation } : {}),
|
|
294
|
+
});
|
|
295
|
+
const featureUnderGroup = (rest, scope) => {
|
|
296
|
+
for (const entry of rest) {
|
|
297
|
+
const walk = (node) => {
|
|
298
|
+
if (node === "*" || typeof node === "string")
|
|
299
|
+
return;
|
|
300
|
+
if (Array.isArray(node) || isGroup(node)) {
|
|
301
|
+
children(node).forEach(walk);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (kindOf(node) === "feature") {
|
|
305
|
+
report(scope, `requires ${describeLeaf(node)} inside "or" or "not", while schema version "2" only has a flat list of required features`);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
walk(entry);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const withoutFeatures = (rest) => {
|
|
312
|
+
const strip = (node) => {
|
|
313
|
+
if (typeof node === "string" || !isGroup(node))
|
|
314
|
+
return kindOf(node) === "feature" ? NEVER : node;
|
|
315
|
+
const mapped = children(node).map(strip);
|
|
316
|
+
if ("and" in node)
|
|
317
|
+
return { and: mapped };
|
|
318
|
+
if ("or" in node)
|
|
319
|
+
return { or: mapped };
|
|
320
|
+
return { not: mapped };
|
|
321
|
+
};
|
|
322
|
+
return rest.map(strip);
|
|
323
|
+
};
|
|
324
|
+
const encode = (value) => (stringify && typeof value !== "string" ? JSON.stringify(value) : value);
|
|
325
|
+
/** A root gate: schema version "2" features and global variables only require features. */
|
|
326
|
+
const gate = (value, scope) => {
|
|
327
|
+
if (value === undefined)
|
|
328
|
+
return undefined;
|
|
329
|
+
const { features, rest } = split((0, conditions_1.parseConditions)(value));
|
|
330
|
+
featureUnderGroup(rest, scope);
|
|
331
|
+
for (const entry of rest) {
|
|
332
|
+
const variable = firstLeaf(entry, "variable");
|
|
333
|
+
if (variable)
|
|
334
|
+
report(scope, `reads ${describeLeaf(variable)}, and schema version "2" has no variable conditions`);
|
|
335
|
+
const other = firstLeaf(entry, "segment") || firstLeaf(entry, "attribute");
|
|
336
|
+
if (other)
|
|
337
|
+
report(scope, `compares ${describeLeaf(other)}, while schema version "2" root conditions can only require features`);
|
|
338
|
+
}
|
|
339
|
+
return features.length ? required(features) : undefined;
|
|
340
|
+
};
|
|
341
|
+
/**
|
|
342
|
+
* An override selector. Feature variable overrides read `conditions`
|
|
343
|
+
* before `segments`, and global variable overrides need both to match, so
|
|
344
|
+
* only one of them is ever written.
|
|
345
|
+
*/
|
|
346
|
+
const selector = (value, scope, whenEmpty) => {
|
|
347
|
+
const result = {};
|
|
348
|
+
const { features, rest } = split(value === undefined ? undefined : (0, conditions_1.parseConditions)(value));
|
|
349
|
+
featureUnderGroup(rest, scope);
|
|
350
|
+
const clean = withoutFeatures(rest);
|
|
351
|
+
if (features.length)
|
|
352
|
+
result.requiredFeatures = required(features);
|
|
353
|
+
if (!clean.length) {
|
|
354
|
+
if (!features.length && whenEmpty === "always")
|
|
355
|
+
result.conditions = "*";
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
const expression = clean.length === 1 ? clean[0] : clean;
|
|
359
|
+
if (kinds(expression).has("segment"))
|
|
360
|
+
result.segments = encode(toSegments(expression, scope));
|
|
361
|
+
else
|
|
362
|
+
result.conditions = encode(toConditions(expression, scope));
|
|
363
|
+
return result;
|
|
364
|
+
};
|
|
365
|
+
const schema = (variablesSchema) => {
|
|
366
|
+
const result = {};
|
|
367
|
+
for (const key of Object.keys(variablesSchema)) {
|
|
368
|
+
const entry = variablesSchema[key];
|
|
369
|
+
result[key] = entry.type === "number" ? { ...entry, type: "double" } : entry;
|
|
370
|
+
}
|
|
371
|
+
return result;
|
|
372
|
+
};
|
|
373
|
+
const featureOverrides = (overrides, owner, ownerPath, featureKey) => {
|
|
374
|
+
if (!overrides)
|
|
375
|
+
return undefined;
|
|
376
|
+
const result = {};
|
|
377
|
+
for (const variableKey of Object.keys(overrides)) {
|
|
378
|
+
result[variableKey] = overrides[variableKey].map((override, index) => {
|
|
379
|
+
const name = override.key ? `"${override.key}"` : String(index + 1);
|
|
380
|
+
return {
|
|
381
|
+
...without(override, ["conditions", "segments", "requiredFeatures"]),
|
|
382
|
+
...selector(override.conditions, {
|
|
383
|
+
entityType: "feature",
|
|
384
|
+
key: featureKey,
|
|
385
|
+
path: [...ownerPath, "variableOverrides", variableKey, index, "conditions"],
|
|
386
|
+
location: `override ${name} of variable "${variableKey}" in ${owner}`,
|
|
387
|
+
}, "always"),
|
|
388
|
+
};
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
};
|
|
393
|
+
// features
|
|
394
|
+
const features = {};
|
|
395
|
+
for (const featureKey of Object.keys(datafile.features || {})) {
|
|
396
|
+
const feature = datafile.features[featureKey];
|
|
397
|
+
const converted = without(feature, ["conditions", "requiredFeatures"]);
|
|
398
|
+
const requirements = gate(feature.conditions, {
|
|
399
|
+
entityType: "feature",
|
|
400
|
+
key: featureKey,
|
|
401
|
+
path: ["conditions"],
|
|
402
|
+
location: "the root gate",
|
|
403
|
+
});
|
|
404
|
+
if (requirements)
|
|
405
|
+
converted.requiredFeatures = requirements;
|
|
406
|
+
converted.traffic = (feature.traffic || []).map((traffic) => {
|
|
407
|
+
const ruleConditions = traffic.conditions;
|
|
408
|
+
const variableOverrides = traffic.variableOverrides;
|
|
409
|
+
const ruleRest = without(traffic, ["conditions", "segments", "variableOverrides"]);
|
|
410
|
+
const scope = {
|
|
411
|
+
entityType: "feature",
|
|
412
|
+
key: featureKey,
|
|
413
|
+
path: ["rules", traffic.key, "conditions"],
|
|
414
|
+
location: `rule "${traffic.key}"`,
|
|
415
|
+
};
|
|
416
|
+
const result = {
|
|
417
|
+
...ruleRest,
|
|
418
|
+
segments: encode(toSegments(ruleConditions === undefined ? "*" : (0, conditions_1.parseConditions)(ruleConditions), scope)),
|
|
419
|
+
};
|
|
420
|
+
const overrides = featureOverrides(variableOverrides, `rule "${traffic.key}"`, ["rules", traffic.key], featureKey);
|
|
421
|
+
if (overrides)
|
|
422
|
+
result.variableOverrides = overrides;
|
|
423
|
+
return result;
|
|
424
|
+
});
|
|
425
|
+
if (feature.force) {
|
|
426
|
+
converted.force = feature.force.map((force, index) => {
|
|
427
|
+
const forceConditions = force.conditions;
|
|
428
|
+
const forceRest = without(force, ["conditions", "segments"]);
|
|
429
|
+
const scope = {
|
|
430
|
+
entityType: "feature",
|
|
431
|
+
key: featureKey,
|
|
432
|
+
path: ["force", index, "conditions"],
|
|
433
|
+
location: `force entry ${index + 1}`,
|
|
434
|
+
};
|
|
435
|
+
const value = forceConditions === undefined ? "*" : (0, conditions_1.parseConditions)(forceConditions);
|
|
436
|
+
if (value !== "*" && kinds(value).has("segment"))
|
|
437
|
+
return { ...forceRest, segments: encode(toSegments(value, scope)) };
|
|
438
|
+
return { ...forceRest, conditions: encode(toConditions(value, scope)) };
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
if (feature.variations) {
|
|
442
|
+
converted.variations = feature.variations.map((variation) => {
|
|
443
|
+
const overrides = featureOverrides(variation.variableOverrides, `variation "${variation.value}"`, ["variations", variation.value], featureKey);
|
|
444
|
+
return overrides ? { ...variation, variableOverrides: overrides } : variation;
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
if (feature.variablesSchema)
|
|
448
|
+
converted.variablesSchema = schema(feature.variablesSchema);
|
|
449
|
+
features[featureKey] = converted;
|
|
450
|
+
}
|
|
451
|
+
// global variables
|
|
452
|
+
let variables;
|
|
453
|
+
if (datafile.variables) {
|
|
454
|
+
variables = {};
|
|
455
|
+
for (const variableKey of Object.keys(datafile.variables)) {
|
|
456
|
+
const variable = datafile.variables[variableKey];
|
|
457
|
+
const scope = (path, location) => ({
|
|
458
|
+
entityType: "variable",
|
|
459
|
+
key: variableKey,
|
|
460
|
+
path,
|
|
461
|
+
location,
|
|
462
|
+
});
|
|
463
|
+
if (variable.compose !== undefined) {
|
|
464
|
+
report(scope(["compose"], "the variable"), `is a composition, which @featurevisor/sdk v3 cannot evaluate`);
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (variable.overrideProgram !== undefined) {
|
|
468
|
+
report(scope(["overrides"], "the variable"), `needs runtime overrides, from "continue" or "precomputeMutations: false", which @featurevisor/sdk v3 cannot run`);
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
const { conditions, overrides, type } = variable;
|
|
472
|
+
const converted = {
|
|
473
|
+
...without(variable, ["conditions", "overrides", "requiredFeatures", "type"]),
|
|
474
|
+
type: type === "number" ? "double" : type,
|
|
475
|
+
};
|
|
476
|
+
const requirements = gate(conditions, scope(["conditions"], "the root gate"));
|
|
477
|
+
if (requirements)
|
|
478
|
+
converted.requiredFeatures = requirements;
|
|
479
|
+
if (overrides) {
|
|
480
|
+
converted.overrides = overrides.map((override, index) => {
|
|
481
|
+
const name = (override.keyPath || [override.key]).join(".");
|
|
482
|
+
return {
|
|
483
|
+
...without(override, ["conditions", "segments", "requiredFeatures"]),
|
|
484
|
+
...selector(override.conditions, scope(["overrides", index, "conditions"], `override "${name}"`), "omit"),
|
|
485
|
+
};
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
variables[variableKey] = converted;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// segments, with references to other segments replaced by their conditions
|
|
492
|
+
for (const segmentKey of Object.keys(v4Segments)) {
|
|
493
|
+
const segment = v4Segments[segmentKey];
|
|
494
|
+
segments[segmentKey] = {
|
|
495
|
+
...segment,
|
|
496
|
+
conditions: encode(toConditions((0, conditions_1.parseConditions)(segment.conditions), { entityType: "segment", key: segmentKey, path: ["conditions"], location: "the segment" }, new Set([segmentKey]))),
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
for (const [key, conditions] of synthesized) {
|
|
500
|
+
segments[key] = { conditions: encode(conditions) };
|
|
501
|
+
}
|
|
502
|
+
const result = {
|
|
503
|
+
schemaVersion: exports.SCHEMA_V2,
|
|
504
|
+
revision: datafile.revision,
|
|
505
|
+
...(datafile.featurevisorVersion ? { featurevisorVersion: datafile.featurevisorVersion } : {}),
|
|
506
|
+
segments,
|
|
507
|
+
features,
|
|
508
|
+
...(variables ? { variables } : {}),
|
|
509
|
+
};
|
|
510
|
+
return { datafile: result, issues: dedupe(issues) };
|
|
511
|
+
}
|
|
512
|
+
function dedupe(issues) {
|
|
513
|
+
const seen = new Set();
|
|
514
|
+
return issues.filter((issue) => {
|
|
515
|
+
const id = JSON.stringify([issue.entityType, issue.key, issue.location, issue.reason]);
|
|
516
|
+
if (seen.has(id))
|
|
517
|
+
return false;
|
|
518
|
+
seen.add(id);
|
|
519
|
+
return true;
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
/** Convert, or throw every issue at once. */
|
|
523
|
+
function toSchemaV2(datafile, options = {}) {
|
|
524
|
+
const { datafile: converted, issues } = convertDatafileToSchemaV2(datafile, options);
|
|
525
|
+
if (issues.length)
|
|
526
|
+
throw new SchemaV2ConversionError(issues, options.targetKey, options.environment);
|
|
527
|
+
return converted;
|
|
528
|
+
}
|
|
529
|
+
//# sourceMappingURL=schemaV2.js.map
|