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

Change the way that we merge presets with existing configs #1834

Merged
merged 1 commit into from
Nov 6, 2023
Merged
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
30 changes: 28 additions & 2 deletions web/pkg/opni/components/MonitoringBackend/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import LabeledSelect from '@shell/components/form/LabeledSelect';
import Tab from '@shell/components/Tabbed/Tab';
import Tabbed from '@shell/components/Tabbed';
import { cloneDeep, merge } from 'lodash';
import { cloneDeep } from 'lodash';
import { CortexOps, DriverUtil } from '@pkg/opni/api/opni';
import { getClusterStats } from '@pkg/opni/utils/requests';
import { Duration } from '@bufbuild/protobuf';
Expand All @@ -22,6 +22,32 @@ export async function isEnabled() {
}
}

export function mergeConfig(target, ...sources) {
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}

if (!sources.length) {
return target;
}
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
mergeConfig(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeConfig(target, ...sources);
}

export default {
components: {
Backend,
Expand Down Expand Up @@ -265,7 +291,7 @@ export default {
setPresetAsConfig(index) {
const presetConfig = cloneDeep(this.presets[index].spec);
const currentConfig = cloneDeep(this.config);
const mergedConfig = merge(currentConfig, presetConfig);
const mergedConfig = mergeConfig({}, currentConfig, presetConfig);

this.$set(this, 'config', mergedConfig);
this.prepareConfigFieldsForUI();
Expand Down