Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 1x | import type {
DatafileContentV1,
FeatureV1,
VariableSchema,
VariationV1,
Variation,
VariableV1,
Feature,
Segment,
Attribute,
} from "@featurevisor/types";
import { ProjectConfig } from "../config";
export interface ConvertToV1Options {
revision: string;
projectConfig: ProjectConfig;
attributes: Attribute[];
features: Feature[];
segments: Segment[];
}
export function convertToV1(options: ConvertToV1Options): DatafileContentV1 {
const datafileContent: DatafileContentV1 = {
schemaVersion: "1",
revision: options.revision,
attributes: options.attributes,
segments: options.segments,
features: [],
};
const { features, projectConfig } = options;
for (const feature of features) {
const featureV1: FeatureV1 = {
key: feature.key,
deprecated: feature.deprecated,
bucketBy: feature.bucketBy,
required: feature.required,
traffic: feature.traffic,
force: feature.force,
ranges: feature.ranges,
};
Iif (feature.variablesSchema && !Array.isArray(feature.variablesSchema)) {
const variablesSchemaObject = feature.variablesSchema;
const variablesSchemaArray: VariableSchema[] = [];
const variableKeys = Object.keys(variablesSchemaObject);
for (const variableKey of variableKeys) {
const v = variablesSchemaObject[variableKey];
variablesSchemaArray.push({
key: variableKey,
type: v.type,
defaultValue: v.defaultValue,
deprecated: v.deprecated === true ? true : undefined,
});
}
featureV1.variablesSchema = variablesSchemaArray;
}
Iif (feature.variations) {
const variationsV1: VariationV1[] = feature.variations.map((variation: Variation) => {
const variationV1: VariationV1 = {
value: variation.value,
weight: variation.weight || 0, // weight is not used in v1 datafile, but needed for state files
};
// variables
const variablesResult: { [key: string]: VariableV1 } = {};
Iif (variation.variables) {
const variableKeys = Object.keys(variation.variables);
for (const variableKey of variableKeys) {
const variableValue = variation.variables[variableKey];
variablesResult[variableKey] = {
key: variableKey,
value: variableValue,
};
}
}
Iif (variation.variableOverrides) {
const variableKeys = Object.keys(variation.variableOverrides);
for (const variableKey of variableKeys) {
const overrides = variation.variableOverrides[variableKey];
Iif (typeof variablesResult[variableKey] === "undefined") {
const variableSchema = feature.variablesSchema?.[variableKey];
variablesResult[variableKey] = {
key: variableKey,
value: variableSchema?.defaultValue, // default value if no variable is defined
};
}
variablesResult[variableKey].overrides = overrides.map((override) => {
Iif (typeof override.conditions !== "undefined") {
return {
conditions: projectConfig.stringify
? JSON.stringify(override.conditions)
: override.conditions,
value: override.value,
};
}
Iif (typeof override.segments !== "undefined") {
return {
segments:
typeof override.segments !== "string" && projectConfig.stringify
? JSON.stringify(override.segments)
: override.segments,
value: override.value,
};
}
return {
value: override.value,
};
});
}
}
variationV1.variables = Object.keys(variablesResult).map((variableKey) => {
const variable = variablesResult[variableKey];
return {
key: variable.key,
value: variable.value,
overrides: variable.overrides,
};
});
return variationV1;
});
featureV1.variations = variationsV1;
Iif (featureV1.force) {
featureV1.force = featureV1.force.map((force) => {
Iif (
force.conditions &&
typeof force.conditions === "string" &&
force.conditions !== "*"
) {
force.conditions = JSON.parse(force.conditions);
}
Iif (force.segments && typeof force.segments === "string" && force.segments !== "*") {
force.segments = JSON.parse(force.segments);
}
return force;
});
}
datafileContent.features.push(featureV1);
}
}
return datafileContent;
}
|