Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not recurse into arrays when constructing Target #59

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/distance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ function* getOperations<S>(
* Calculates the list of changes between the current state and the target
*/
export function diff<S>(s: S, t: Target<S>): Array<Operation<S, any>> {
return [...getOperations(s, t)]
.filter(({ isLeaf }) => isLeaf)
.map(({ isLeaf, ...op }) => op);
const ops = [...getOperations(s, t)];
return ops.filter(({ isLeaf }) => isLeaf).map(({ isLeaf, ...op }) => op);
}

function from<S>(src: S, tgt: Target<S>): Distance<S> {
Expand Down
11 changes: 11 additions & 0 deletions lib/target.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,16 @@ describe('Target', () => {
g: { h: 'hello' },
});
});

it('does not iterate into arrays', () => {
type S = {
a?: string[];
};

const state: S = { a: ['foo', 'bar'] };
expect(Target.from(state, {})).to.deep.equal({ a: UNDEFINED });
expect(Target.from(state, { a: ['foo'] })).to.deep.equal({ a: ['foo'] });
expect(Target.from(state, { a: ['bar'] })).to.deep.equal({ a: ['bar'] });
});
});
});
4 changes: 4 additions & 0 deletions lib/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function from<S>(
while (queue.length > 0) {
const { s, t, p } = queue.shift()!;

// Don't recurse into arrays
if (Array.isArray(s) || Array.isArray(t)) {
continue;
}
for (const key of Object.keys(s)) {
if (key in t && t[key] === undefined && s[key] !== undefined) {
t[key] = UNDEFINED;
Expand Down
2 changes: 1 addition & 1 deletion tests/orchestrator/planning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('orchestrator/planning', () => {
},
},
keys: {},
images: [{ name: 'a0_main:r0' }],
images: { 'a0_main:r0': { name: 'alpine:latest' } },
};

const result = planner.findPlan(device, {
Expand Down
Loading