[Integration Assistant] Improve sample merge functionality (#190656)

## Summary

Adds a few extra checks when doing deep recursive merges, will test a
few things when it comes to the overhead it adds before merging.

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
This commit is contained in:
Marius Iversen 2024-08-19 12:08:53 +02:00 committed by GitHub
parent d69e598e30
commit d971c6a10e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -55,22 +55,25 @@ function isEmptyValue(value: unknown): boolean {
function merge(target: Record<string, any>, source: Record<string, any>): Record<string, unknown> {
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 (key !== '__proto__' && key !== 'constructor') {
if (Object.prototype.hasOwnProperty.call(target, key)) {
const targetValue = target[key];
if (Array.isArray(sourceValue)) {
target[key] = sourceValue;
} else if (
typeof sourceValue === 'object' &&
sourceValue !== null &&
typeof targetValue === 'object' &&
targetValue !== null &&
!Array.isArray(targetValue)
) {
target[key] = merge(targetValue, sourceValue);
} else if (isEmptyValue(targetValue) && !isEmptyValue(sourceValue)) {
target[key] = sourceValue;
}
} else if (!isEmptyValue(sourceValue)) {
target[key] = sourceValue;
}
} else if (!(key in target) || (isEmptyValue(targetValue) && !isEmptyValue(sourceValue))) {
target[key] = sourceValue;
}
}
return target;