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

[Streams 🌊] Add processors validation and simulation gate #206566

Merged
merged 12 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(kbn-object-utils): add updated to calculateObjectDiff
  • Loading branch information
tonyghiani committed Jan 13, 2025
commit c43875cc837c7f5c0477f603193de9d8ad4c7612
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@
import { calculateObjectDiff } from './calculate_object_diff';

describe('calculateObjectDiff', () => {
it('should return the added and removed parts between 2 objects', () => {
const { added, removed } = calculateObjectDiff({ alpha: 1, beta: 2 }, { alpha: 1, gamma: 3 });
it('should return the added, removed and updated parts between 2 objects', () => {
const { added, removed, updated } = calculateObjectDiff(
{ alpha: 1, beta: 2, sigma: 4 },
{ alpha: 1, gamma: 3, sigma: 5 }
);
expect(added).toEqual({ gamma: 3 });
expect(removed).toEqual({ beta: 2 });
expect(updated).toEqual({ sigma: 5 });
});

it('should work on nested objects', () => {
const { added, removed } = calculateObjectDiff(
{ alpha: 1, beta: { gamma: 2, delta: { sigma: 7 } } },
{ alpha: 1, beta: { gamma: 2, eta: 4 } }
const { added, removed, updated } = calculateObjectDiff(
{ alpha: 1, beta: { gamma: 2, delta: { sigma: 7, omega: 8 } } },
{ alpha: 1, beta: { gamma: 2, delta: { omega: 9 }, eta: 4 } }
);

expect(added).toEqual({ beta: { eta: 4 } });
expect(removed).toEqual({ beta: { delta: { sigma: 7 } } });
expect(updated).toEqual({ beta: { delta: { omega: 9 } } });
});

it('should return empty added/removed when the objects are the same', () => {
const { added, removed } = calculateObjectDiff({ alpha: 1, beta: 2 }, { alpha: 1, beta: 2 });
it('should return empty added/removed/updated when the objects are the same', () => {
const { added, removed, updated } = calculateObjectDiff(
{ alpha: 1, beta: 2 },
{ alpha: 1, beta: 2 }
);
expect(added).toEqual({});
expect(removed).toEqual({});
expect(updated).toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type DeepPartial<TInputObj> = {
interface ObjectDiffResult<TBase, TCompare> {
added: DeepPartial<TCompare>;
removed: DeepPartial<TBase>;
updated: {
[K in keyof TBase & keyof TCompare]?: TBase[K] extends TCompare[K] ? never : TCompare[K];
};
}

/**
Expand All @@ -34,31 +37,37 @@ export function calculateObjectDiff<TBase extends Obj, TCompare extends Obj>(
oldObj: TBase,
newObj?: TCompare
): ObjectDiffResult<TBase, TCompare> {
const added: DeepPartial<TCompare> = {};
const removed: DeepPartial<TBase> = {};
const added: ObjectDiffResult<TBase, TCompare>['added'] = {};
const removed: ObjectDiffResult<TBase, TCompare>['removed'] = {};
const updated: ObjectDiffResult<TBase, TCompare>['updated'] = {};

if (!newObj) return { added, removed };
if (!newObj) return { added, removed, updated };

function diffRecursive(
base: Obj,
compare: Obj,
addedMap: DeepPartial<Obj>,
removedMap: DeepPartial<Obj>
removedMap: DeepPartial<Obj>,
updatedMap: DeepPartial<Obj>
): void {
for (const key in compare) {
if (!(key in base)) {
addedMap[key] = compare[key];
} else if (isPlainObject(base[key]) && isPlainObject(compare[key])) {
addedMap[key] = {};
removedMap[key] = {};
updatedMap[key] = {};
diffRecursive(
base[key] as Obj,
compare[key] as Obj,
addedMap[key] as Obj,
removedMap[key] as Obj
removedMap[key] as Obj,
updatedMap[key] as Obj
);
if (isEmpty(addedMap[key])) delete addedMap[key];
if (isEmpty(removedMap[key])) delete removedMap[key];
} else if (base[key] !== compare[key]) {
updatedMap[key] = compare[key];
}
}

Expand All @@ -69,7 +78,7 @@ export function calculateObjectDiff<TBase extends Obj, TCompare extends Obj>(
}
}

diffRecursive(oldObj, newObj, added, removed);
diffRecursive(oldObj, newObj, added, removed, updated);

return { added, removed };
return { added, removed, updated };
}