Skip to content

Commit e0d9d5d

Browse files
committed
refactor: clean up config query update format
1 parent 91b0512 commit e0d9d5d

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/queries/catalog/config-query/config-query-format.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Joi from 'joi';
66
import type { FlowrConfigOptions } from '../../../config';
77
import { jsonReplacer } from '../../../util/json';
88
import type { DeepPartial } from 'ts-essentials';
9-
import type { ParsedQueryLine, SupportedQuery } from '../../query';
9+
import type { ParsedQueryLine, Query, SupportedQuery } from '../../query';
1010
import type { ReplOutput } from '../../../cli/repl/commands/repl-main';
1111
import type { CommandCompletions } from '../../../cli/repl/core';
1212

@@ -83,12 +83,49 @@ function configQueryLineParser(output: ReplOutput, line: readonly string[], _con
8383
};
8484
}
8585

86+
function collectKeysFromUpdate(update: DeepPartial<FlowrConfigOptions>, prefix: string = ''): string[] {
87+
// only collect leaf keys
88+
const keys: string[] = [];
89+
for(const [key, value] of Object.entries(update)) {
90+
const fullKey = prefix ? `${prefix}.${key}` : key;
91+
if(value && typeof value === 'object' && !Array.isArray(value)) {
92+
keys.push(...collectKeysFromUpdate(value as DeepPartial<FlowrConfigOptions>, fullKey));
93+
} else {
94+
keys.push(fullKey);
95+
}
96+
}
97+
return keys;
98+
}
99+
100+
function getValueAtPath(obj: object, path: string[]): unknown {
101+
let current: unknown = obj;
102+
for(const key of path) {
103+
if(current && typeof current === 'object' && (current as Record<string, unknown>)[key] !== undefined) {
104+
current = (current as Record<string, unknown>)[key];
105+
} else {
106+
return undefined;
107+
}
108+
}
109+
return current;
110+
}
111+
86112
export const ConfigQueryDefinition = {
87113
executor: executeConfigQuery,
88-
asciiSummarizer: (formatter: OutputFormatter, _analyzer: unknown, queryResults: BaseQueryResult, result: string[]) => {
114+
asciiSummarizer: (formatter: OutputFormatter, _analyzer: unknown, queryResults: BaseQueryResult, result: string[], queries: readonly Query[]) => {
89115
const out = queryResults as ConfigQueryResult;
90116
result.push(`Query: ${bold('config', formatter)} (${printAsMs(out['.meta'].timing, 0)})`);
91-
result.push(` ╰ Config:\n${JSON.stringify(out.config, jsonReplacer, 4)}`);
117+
const configQueries = queries.filter(q => q.type === 'config');
118+
if(configQueries.some(q => q.update)) {
119+
const updatedKeys = configQueries.flatMap(q => q.update ? collectKeysFromUpdate(q.update) : []);
120+
result.push(' ╰ Updated configuration:');
121+
for(const key of updatedKeys) {
122+
const path = key.split('.');
123+
const newValue = getValueAtPath(out.config, path);
124+
result.push(` - ${key}: ${JSON.stringify(newValue, jsonReplacer)}`);
125+
}
126+
} else {
127+
result.push(` ╰ Config:\n${JSON.stringify(out.config, jsonReplacer, 4)}`);
128+
}
92129
return true;
93130
},
94131
completer: configReplCompleter,

0 commit comments

Comments
 (0)