codify-plugin-lib 1.0.14 → 1.0.16
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/entities/change-set.d.ts +1 -0
- package/dist/entities/change-set.js +9 -1
- package/dist/entities/resource.d.ts +2 -2
- package/dist/entities/resource.js +1 -1
- package/package.json +1 -1
- package/src/entities/change-set.test.ts +67 -0
- package/src/entities/change-set.ts +12 -1
- package/src/entities/resource.test.ts +6 -6
- package/src/entities/resource.ts +3 -3
|
@@ -12,4 +12,5 @@ export declare class ChangeSet {
|
|
|
12
12
|
static createForNullCurrentConfig(desiredConfig: ResourceConfig): ChangeSet;
|
|
13
13
|
static calculateParameterChangeSet(prev: ResourceConfig, next: ResourceConfig): ParameterChange[];
|
|
14
14
|
static combineResourceOperations(prev: ResourceOperation, next: ResourceOperation): ResourceOperation;
|
|
15
|
+
static isSame(a: unknown, b: unknown): boolean;
|
|
15
16
|
}
|
|
@@ -39,7 +39,7 @@ class ChangeSet {
|
|
|
39
39
|
delete filteredPrev[k];
|
|
40
40
|
continue;
|
|
41
41
|
}
|
|
42
|
-
if (filteredPrev[k]
|
|
42
|
+
if (!ChangeSet.isSame(filteredPrev[k], filteredNext[k])) {
|
|
43
43
|
parameterChangeSet.push({
|
|
44
44
|
name: k,
|
|
45
45
|
previousValue: v,
|
|
@@ -84,5 +84,13 @@ class ChangeSet {
|
|
|
84
84
|
const indexNext = orderOfOperations.indexOf(next);
|
|
85
85
|
return orderOfOperations[Math.max(indexPrev, indexNext)];
|
|
86
86
|
}
|
|
87
|
+
static isSame(a, b) {
|
|
88
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
89
|
+
const sortedPrev = a.map((x) => x).sort();
|
|
90
|
+
const sortedNext = b.map((x) => x).sort();
|
|
91
|
+
return JSON.stringify(sortedPrev) === JSON.stringify(sortedNext);
|
|
92
|
+
}
|
|
93
|
+
return a === b;
|
|
94
|
+
}
|
|
87
95
|
}
|
|
88
96
|
exports.ChangeSet = ChangeSet;
|
|
@@ -8,8 +8,8 @@ export declare abstract class Resource<T extends ResourceConfig> {
|
|
|
8
8
|
onInitialize(): Promise<void>;
|
|
9
9
|
plan(desiredConfig: T): Promise<Plan<T>>;
|
|
10
10
|
apply(plan: Plan<T>): Promise<void>;
|
|
11
|
-
abstract validate(config:
|
|
12
|
-
abstract getCurrentConfig(): Promise<T | null>;
|
|
11
|
+
abstract validate(config: unknown): Promise<boolean>;
|
|
12
|
+
abstract getCurrentConfig(desiredConfig: T): Promise<T | null>;
|
|
13
13
|
abstract calculateOperation(change: ParameterChange): ResourceOperation.MODIFY | ResourceOperation.RECREATE;
|
|
14
14
|
abstract applyCreate(plan: Plan<T>): Promise<void>;
|
|
15
15
|
abstract applyModify(plan: Plan<T>): Promise<void>;
|
|
@@ -12,7 +12,7 @@ class Resource {
|
|
|
12
12
|
async onInitialize() { }
|
|
13
13
|
async plan(desiredConfig) {
|
|
14
14
|
await this.validate(desiredConfig);
|
|
15
|
-
const currentConfig = await this.getCurrentConfig();
|
|
15
|
+
const currentConfig = await this.getCurrentConfig(desiredConfig);
|
|
16
16
|
if (!currentConfig) {
|
|
17
17
|
return plan_1.Plan.create(change_set_1.ChangeSet.createForNullCurrentConfig(desiredConfig), desiredConfig);
|
|
18
18
|
}
|
package/package.json
CHANGED
|
@@ -74,6 +74,38 @@ describe('Change set tests', () => {
|
|
|
74
74
|
expect(cs[0].operation).to.eq(ParameterOperation.NOOP);
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
it ('handles simple arrays', () => {
|
|
78
|
+
const before = {
|
|
79
|
+
type: 'config',
|
|
80
|
+
propA: ['a', 'b', 'c'],
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const after = {
|
|
84
|
+
type: 'config',
|
|
85
|
+
propA: ['b', 'a', 'c'],
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const cs = ChangeSet.calculateParameterChangeSet(before, after);
|
|
89
|
+
expect(cs.length).to.eq(1);
|
|
90
|
+
expect(cs[0].operation).to.eq(ParameterOperation.NOOP);
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it ('handles simple arrays', () => {
|
|
94
|
+
const before = {
|
|
95
|
+
type: 'config',
|
|
96
|
+
propA: ['a', 'b'],
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const after = {
|
|
100
|
+
type: 'config',
|
|
101
|
+
propA: ['b', 'a', 'c'],
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const cs = ChangeSet.calculateParameterChangeSet(before, after);
|
|
105
|
+
expect(cs.length).to.eq(1);
|
|
106
|
+
expect(cs[0].operation).to.eq(ParameterOperation.MODIFY);
|
|
107
|
+
})
|
|
108
|
+
|
|
77
109
|
it ('determines the order of operations 1', () => {
|
|
78
110
|
const op1 = ResourceOperation.MODIFY;
|
|
79
111
|
const op2 = ResourceOperation.CREATE
|
|
@@ -97,4 +129,39 @@ describe('Change set tests', () => {
|
|
|
97
129
|
const opResult = ChangeSet.combineResourceOperations(op1, op2);
|
|
98
130
|
expect(opResult).to.eq(ResourceOperation.MODIFY);
|
|
99
131
|
})
|
|
132
|
+
|
|
133
|
+
it('correctly determines array equality', () => {
|
|
134
|
+
const arrA = ['a', 'b', 'd'];
|
|
135
|
+
const arrB = ['a', 'b', 'd'];
|
|
136
|
+
|
|
137
|
+
expect(ChangeSet.isSame(arrA, arrB)).to.be.true;
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('correctly determines array equality 2', () => {
|
|
141
|
+
const arrA = ['a', 'b'];
|
|
142
|
+
const arrB = ['a', 'b', 'd'];
|
|
143
|
+
|
|
144
|
+
expect(ChangeSet.isSame(arrA, arrB)).to.be.false;
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('correctly determines array equality 3', () => {
|
|
148
|
+
const arrA = ['b', 'a', 'd'];
|
|
149
|
+
const arrB = ['a', 'b', 'd'];
|
|
150
|
+
|
|
151
|
+
expect(ChangeSet.isSame(arrA, arrB)).to.be.true;
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('correctly determines array equality 4', () => {
|
|
155
|
+
const arrA = [{ key1: 'a' }, { key1: 'a' }, { key1: 'a' }];
|
|
156
|
+
const arrB = [{ key1: 'a' }, { key1: 'a' }, { key1: 'b' }];
|
|
157
|
+
|
|
158
|
+
expect(ChangeSet.isSame(arrA, arrB)).to.be.false;
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('correctly determines array equality 5', () => {
|
|
162
|
+
const arrA = [{ key1: 'b' }, { key1: 'a' }, { key1: 'a' }];
|
|
163
|
+
const arrB = [{ key1: 'a' }, { key1: 'a' }, { key1: 'b' }];
|
|
164
|
+
|
|
165
|
+
expect(ChangeSet.isSame(arrA, arrB)).to.be.false;
|
|
166
|
+
})
|
|
100
167
|
})
|
|
@@ -60,7 +60,7 @@ export class ChangeSet {
|
|
|
60
60
|
continue;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
if (filteredPrev[k]
|
|
63
|
+
if (!ChangeSet.isSame(filteredPrev[k], filteredNext[k])) {
|
|
64
64
|
parameterChangeSet.push({
|
|
65
65
|
name: k,
|
|
66
66
|
previousValue: v,
|
|
@@ -114,4 +114,15 @@ export class ChangeSet {
|
|
|
114
114
|
|
|
115
115
|
return orderOfOperations[Math.max(indexPrev, indexNext)];
|
|
116
116
|
}
|
|
117
|
+
|
|
118
|
+
static isSame(a: unknown, b: unknown): boolean {
|
|
119
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
120
|
+
const sortedPrev = a.map((x) => x).sort();
|
|
121
|
+
const sortedNext = b.map((x) => x).sort();
|
|
122
|
+
|
|
123
|
+
return JSON.stringify(sortedPrev) === JSON.stringify(sortedNext);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return a === b;
|
|
127
|
+
}
|
|
117
128
|
}
|
|
@@ -7,19 +7,19 @@ import { expect } from 'chai';
|
|
|
7
7
|
import { Plan } from './plan';
|
|
8
8
|
|
|
9
9
|
class TestResource extends Resource<TestConfig> {
|
|
10
|
-
applyCreate(plan: Plan): Promise<void> {
|
|
10
|
+
applyCreate(plan: Plan<TestConfig>): Promise<void> {
|
|
11
11
|
return Promise.resolve(undefined);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
applyDestroy(plan: Plan): Promise<void> {
|
|
14
|
+
applyDestroy(plan: Plan<TestConfig>): Promise<void> {
|
|
15
15
|
return Promise.resolve(undefined);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
applyModify(plan: Plan): Promise<void> {
|
|
18
|
+
applyModify(plan: Plan<TestConfig>): Promise<void> {
|
|
19
19
|
return Promise.resolve(undefined);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
applyRecreate(plan: Plan): Promise<void> {
|
|
22
|
+
applyRecreate(plan: Plan<TestConfig>): Promise<void> {
|
|
23
23
|
return Promise.resolve(undefined);
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -156,7 +156,7 @@ describe('Resource tests', () => {
|
|
|
156
156
|
const result = await resourceSpy.apply(
|
|
157
157
|
Plan.create(
|
|
158
158
|
new ChangeSet(ResourceOperation.CREATE, []),
|
|
159
|
-
{ type: 'resource' }
|
|
159
|
+
{ type: 'resource', propA: 'a', propB: 0 }
|
|
160
160
|
)
|
|
161
161
|
)
|
|
162
162
|
|
|
@@ -174,7 +174,7 @@ describe('Resource tests', () => {
|
|
|
174
174
|
const result = await resourceSpy.apply(
|
|
175
175
|
Plan.create(
|
|
176
176
|
new ChangeSet(ResourceOperation.DESTROY, []),
|
|
177
|
-
{ type: 'resource' }
|
|
177
|
+
{ type: 'resource', propA: 'a', propB: 0 }
|
|
178
178
|
)
|
|
179
179
|
)
|
|
180
180
|
|
package/src/entities/resource.ts
CHANGED
|
@@ -17,7 +17,7 @@ export abstract class Resource<T extends ResourceConfig> {
|
|
|
17
17
|
async plan(desiredConfig: T): Promise<Plan<T>> {
|
|
18
18
|
await this.validate(desiredConfig);
|
|
19
19
|
|
|
20
|
-
const currentConfig = await this.getCurrentConfig();
|
|
20
|
+
const currentConfig = await this.getCurrentConfig(desiredConfig);
|
|
21
21
|
if (!currentConfig) {
|
|
22
22
|
return Plan.create(ChangeSet.createForNullCurrentConfig(desiredConfig), desiredConfig);
|
|
23
23
|
}
|
|
@@ -55,9 +55,9 @@ export abstract class Resource<T extends ResourceConfig> {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
abstract validate(config:
|
|
58
|
+
abstract validate(config: unknown): Promise<boolean>;
|
|
59
59
|
|
|
60
|
-
abstract getCurrentConfig(): Promise<T | null>;
|
|
60
|
+
abstract getCurrentConfig(desiredConfig: T): Promise<T | null>;
|
|
61
61
|
|
|
62
62
|
abstract calculateOperation(change: ParameterChange): ResourceOperation.MODIFY | ResourceOperation.RECREATE;
|
|
63
63
|
|