@featurevisor/sdk 1.27.6 → 1.28.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.
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  Feature,
3
3
  Segment,
4
- DatafileContent,
4
+ DatafileContentV1,
5
+ DatafileContentV2,
5
6
  Attribute,
6
7
  AttributeKey,
7
8
  SegmentKey,
@@ -23,16 +24,34 @@ export function parseJsonConditionsIfStringified<T>(record: T, key: string): T {
23
24
  export class DatafileReader {
24
25
  private schemaVersion: string;
25
26
  private revision: string;
27
+
28
+ // v1
26
29
  private attributes: Attribute[];
27
30
  private segments: Segment[];
28
31
  private features: Feature[];
29
32
 
30
- constructor(datafileJson: DatafileContent) {
33
+ // v2
34
+ private attributesV2: Record<AttributeKey, Attribute>;
35
+ private segmentsV2: Record<SegmentKey, Segment>;
36
+ private featuresV2: Record<FeatureKey, Feature>;
37
+
38
+ constructor(datafileJson: DatafileContentV1 | DatafileContentV2) {
31
39
  this.schemaVersion = datafileJson.schemaVersion;
32
40
  this.revision = datafileJson.revision;
33
- this.segments = datafileJson.segments;
34
- this.attributes = datafileJson.attributes;
35
- this.features = datafileJson.features;
41
+
42
+ if (this.schemaVersion === "2") {
43
+ const datafileJsonV2 = datafileJson as DatafileContentV2;
44
+
45
+ this.attributesV2 = datafileJsonV2.attributes;
46
+ this.segmentsV2 = datafileJsonV2.segments;
47
+ this.featuresV2 = datafileJsonV2.features;
48
+ } else {
49
+ const datafileJsonV1 = datafileJson as DatafileContentV1;
50
+
51
+ this.segments = datafileJsonV1.segments;
52
+ this.attributes = datafileJsonV1.attributes;
53
+ this.features = datafileJsonV1.features;
54
+ }
36
55
  }
37
56
 
38
57
  getRevision(): string {
@@ -44,15 +63,33 @@ export class DatafileReader {
44
63
  }
45
64
 
46
65
  getAllAttributes(): Attribute[] {
47
- return this.attributes;
66
+ if (this.schemaVersion !== "2") {
67
+ return this.attributes;
68
+ }
69
+
70
+ // v2
71
+ const result: Attribute[] = [];
72
+
73
+ for (const key in this.attributesV2) {
74
+ result.push(this.attributesV2[key]);
75
+ }
76
+
77
+ return result;
48
78
  }
49
79
 
50
80
  getAttribute(attributeKey: AttributeKey): Attribute | undefined {
81
+ if (this.schemaVersion === "2") {
82
+ return this.attributesV2[attributeKey];
83
+ }
84
+
51
85
  return this.attributes.find((a) => a.key === attributeKey);
52
86
  }
53
87
 
54
88
  getSegment(segmentKey: SegmentKey): Segment | undefined {
55
- const segment = this.segments.find((s) => s.key === segmentKey);
89
+ const segment =
90
+ this.schemaVersion === "2"
91
+ ? this.segmentsV2[segmentKey]
92
+ : this.segments.find((s) => s.key === segmentKey);
56
93
 
57
94
  if (!segment) {
58
95
  return undefined;
@@ -62,7 +99,10 @@ export class DatafileReader {
62
99
  }
63
100
 
64
101
  getFeature(featureKey: FeatureKey): Feature | undefined {
65
- const feature = this.features.find((s) => s.key === featureKey);
102
+ const feature =
103
+ this.schemaVersion === "2"
104
+ ? this.featuresV2[featureKey]
105
+ : this.features.find((f) => f.key === featureKey);
66
106
 
67
107
  if (!feature) {
68
108
  return undefined;