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

[8.x] [Automatic import] refactor merge (#193270) #193291

Merged
merged 1 commit into from
Sep 18, 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
116 changes: 116 additions & 0 deletions x-pack/plugins/integration_assistant/server/util/samples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { merge } from './samples';

describe('merge', () => {
it('Should return source if target is empty', async () => {
const target = {};
const source = { target: 'user.name', confidence: 0.9, type: 'string', date_formats: [] };

const result = merge(target, source);

expect(result).toEqual(source);
});

it('Should return target if source is empty', async () => {
const target = { hostname: '0.0.0.0', 'teleport.internal/resource-id': '1234' };
const source = {};

const result = merge(target, source);

expect(result).toEqual(target);
});

it('Should return one result', async () => {
const target = {
aaa: {
ei: 0,
event: 'cert.create',
uid: '1234',
cluster_name: 'cluster.com',
identity: { user: 'teleport-admin' },
server_labels: { hostname: 'some-hostname' },
},
};
const source = {
aaa: {
ei: 0,
event: 'session.start',
uid: '4567',
cluster_name: 'cluster.com',
user: 'teleport-admin',
server_labels: { hostname: 'some-other-hostname', id: '1234' },
},
};

const result = merge(target, source);

expect(result).toEqual({
aaa: {
ei: 0,
event: 'cert.create',
uid: '1234',
cluster_name: 'cluster.com',
identity: { user: 'teleport-admin' },
server_labels: { hostname: 'some-hostname', id: '1234' },
user: 'teleport-admin',
},
});
});

it('Should not merge built-in properties of neither target nor source', async () => {
const target = {
__proto__: 'some properties',
constructor: 'some other properties',
hostname: '0.0.0.0',
'teleport.internal/resource-id': '1234',
};
const source = {
__proto__: 'some properties of source',
constructor: 'some other properties of source',
};

const result = merge(target, source);

expect(result).toEqual({ hostname: '0.0.0.0', 'teleport.internal/resource-id': '1234' });
});

it('Should keep source object if it collides with target key that is not an object', async () => {
const target = {
hostname: '',
'teleport.internal/resource-id': '1234',
date_formats: 'format',
};
const source = {
target: 'user.name',
confidence: 0.9,
type: 'string',
date_formats: { key: 'value' },
};

const result = merge(target, source);

expect(result).toEqual({
'teleport.internal/resource-id': '1234',
target: 'user.name',
confidence: 0.9,
type: 'string',
hostname: '',
date_formats: { key: 'value' },
});
});

it('Should copy array into the result', async () => {
const target = { date_formats: ['a', 'b'] };
const source = { target: 'user.name', confidence: 0.9, type: 'string', date_formats: ['c'] };

const result = merge(target, source);

expect(result).toEqual(source);
});
});
72 changes: 47 additions & 25 deletions x-pack/plugins/integration_assistant/server/util/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,39 +163,61 @@ export function generateFields(mergedDocs: string): string {
return yaml.safeDump(fieldsStructure, { sortKeys: false });
}

function isEmptyValue(value: unknown): boolean {
return (
value === null ||
value === undefined ||
(typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 0) ||
(Array.isArray(value) && value.length === 0)
);
}

export function merge(
target: Record<string, any>,
source: Record<string, any>
): Record<string, unknown> {
const filteredTarget = filterOwnProperties(target);
for (const [key, sourceValue] of Object.entries(source)) {
const targetValue = target[key];
if (Array.isArray(sourceValue)) {
// Directly assign arrays
target[key] = sourceValue;
} else if (
typeof sourceValue === 'object' &&
sourceValue !== null &&
!Array.isArray(targetValue)
) {
if (typeof targetValue !== 'object' || isEmptyValue(targetValue)) {
target[key] = merge({}, sourceValue);
} else {
target[key] = merge(targetValue, sourceValue);
if (!isBuiltInProperties(key, source)) {
const targetValue = filteredTarget[key];
if (Array.isArray(sourceValue)) {
// Directly assign arrays
filteredTarget[key] = sourceValue;
} else if (isObject(sourceValue) && !Array.isArray(targetValue)) {
if (!isObject(targetValue) || isEmptyValue(targetValue)) {
filteredTarget[key] = merge({}, sourceValue);
} else {
filteredTarget[key] = merge(targetValue, sourceValue);
}
} else if (
!(key in filteredTarget) ||
(isEmptyValue(targetValue) && !isEmptyValue(sourceValue))
) {
filteredTarget[key] = sourceValue;
}
} else if (!(key in target) || (isEmptyValue(targetValue) && !isEmptyValue(sourceValue))) {
target[key] = sourceValue;
}
}
return target;
return filteredTarget;
}

function isEmptyValue(value: unknown): boolean {
if (value == null) return true;
if (isObject(value)) {
if (Array.isArray(value)) return value.length === 0;
return value && Object.keys(value).length === 0;
}
return false;
}

function isObject(value: any): boolean {
return typeof value === 'object' && value !== null;
}

function isBuiltInProperties(key: string, obj: Record<string, any>): boolean {
return key === 'constructor' || !Object.prototype.hasOwnProperty.call(obj, key);
}

function filterOwnProperties(obj: Record<string, any>): Record<string, any> {
const ownProps: Record<string, any> = {};

for (const key of Object.getOwnPropertyNames(obj)) {
if (!isBuiltInProperties(key, obj)) {
ownProps[key] = (obj as any)[key];
}
}

return ownProps;
}

export function mergeSamples(objects: any[]): string {
Expand Down