feat(config): add core utilities for config merge and difference extraction

Add comprehensive config merge utilities for both Rust backend and TypeScript frontend
to support the new common config architecture where customConfig overrides commonConfig.

Backend (Rust):
- Add config_merge.rs module with JSON and TOML merge functions
- Support deep merge where source overrides target for nested objects
- Add extract_difference functions to identify custom-only keys
- Include compute_final_*_config functions for runtime merge calculation
- Register module in lib.rs

Frontend (TypeScript):
- Add configMerge.ts with isPlainObject, deepClone, deepEqual, deepMerge utilities
- Implement computeFinalConfig for merging common + custom configs
- Add extractDifference to identify keys unique to live config
- Add tomlConfigMerge.ts with TOML-specific merge/extract functions
- Use smol-toml for parsing and serialization

These utilities form the foundation for the refactored common config system
where providers store only custom config and merge with shared templates at runtime.
This commit is contained in:
YoVinchen
2026-01-26 13:57:48 +08:00
parent 3da25e59cd
commit b1446d0227
4 changed files with 1377 additions and 0 deletions
+293
View File
@@ -0,0 +1,293 @@
/**
* 配置合并工具函数
*
* 用于公共配置重构后的运行时合并逻辑:
* - computeFinalConfig: 计算最终配置(通用配置 + 自定义配置)
* - extractDifference: 从 live 配置中提取与通用配置不同的部分
*/
// ============================================================================
// 工具函数
// ============================================================================
/**
* 检查值是否为普通对象
*/
export const isPlainObject = (
value: unknown,
): value is Record<string, unknown> => {
return Object.prototype.toString.call(value) === "[object Object]";
};
/**
* 深拷贝对象
*/
export const deepClone = <T>(obj: T): T => {
if (obj === null || typeof obj !== "object") return obj;
if (obj instanceof Date) return new Date(obj.getTime()) as T;
if (Array.isArray(obj)) return obj.map((item) => deepClone(item)) as T;
if (isPlainObject(obj)) {
const clonedObj = {} as Record<string, unknown>;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clonedObj[key] = deepClone((obj as Record<string, unknown>)[key]);
}
}
return clonedObj as T;
}
return obj;
};
/**
* 深度相等比较
*/
export const deepEqual = (a: unknown, b: unknown): boolean => {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if (a === null || b === null) return a === b;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
return a.every((item, index) => deepEqual(item, b[index]));
}
if (isPlainObject(a) && isPlainObject(b)) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
return keysA.every((key) => deepEqual(a[key], b[key]));
}
return false;
};
// ============================================================================
// 配置合并函数
// ============================================================================
/**
* 深度合并两个对象(source 覆盖 target
*
* 合并规则:
* - 嵌套对象:递归合并
* - 数组:source 完全替换 target(不做元素级合并)
* - 原始值:source 覆盖 target
* - undefined:不覆盖
*/
export const deepMerge = <T extends Record<string, unknown>>(
target: T,
source: T,
): T => {
const result = deepClone(target);
for (const key of Object.keys(source)) {
const sourceValue = source[key];
const targetValue = result[key];
// undefined 不覆盖
if (sourceValue === undefined) {
continue;
}
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
// 嵌套对象:递归合并
result[key as keyof T] = deepMerge(
targetValue as Record<string, unknown>,
sourceValue as Record<string, unknown>,
) as T[keyof T];
} else {
// 其他情况(数组、原始值):source 覆盖
result[key as keyof T] = deepClone(sourceValue) as T[keyof T];
}
}
return result;
};
/**
* 计算最终配置
*
* 通用配置作为 base,自定义配置覆盖(自定义优先)
*
* @param customConfig - 供应商自定义配置(来自 settings_config 字段)
* @param commonConfig - 通用配置片段(来自数据库)
* @param enabled - 是否启用通用配置
* @returns 合并后的最终配置
*/
export const computeFinalConfig = (
customConfig: Record<string, unknown>,
commonConfig: Record<string, unknown>,
enabled: boolean,
): Record<string, unknown> => {
const safeCustom = customConfig ?? {};
const safeCommon = commonConfig ?? {};
if (!enabled || Object.keys(safeCommon).length === 0) {
return deepClone(safeCustom);
}
// 通用配置作为 base,自定义配置覆盖
// 这样自定义配置的值会优先
return deepMerge(safeCommon, safeCustom);
};
// ============================================================================
// 差异提取函数
// ============================================================================
/**
* 差异提取结果
*/
export interface ExtractDifferenceResult {
/** 自定义配置(与通用配置不同的部分) */
customConfig: Record<string, unknown>;
/** 是否检测到通用配置的键(用于判断是否应启用通用配置) */
hasCommonKeys: boolean;
}
/**
* 从 live 配置中提取与通用配置不同的部分作为自定义配置
*
* 提取规则:
* - 通用配置中不存在的键 → 加入自定义配置
* - 通用配置中存在但值不同 → 加入自定义配置(用户覆盖)
* - 通用配置中存在且值相同 → 跳过(避免冗余存储)
*
* @param liveConfig - 从本地文件读取的配置
* @param commonConfig - 通用配置片段
* @returns { customConfig, hasCommonKeys }
*/
export const extractDifference = (
liveConfig: Record<string, unknown>,
commonConfig: Record<string, unknown>,
): ExtractDifferenceResult => {
const customConfig: Record<string, unknown> = {};
let hasCommonKeys = false;
/**
* 递归提取差异
*/
const extract = (
live: Record<string, unknown>,
common: Record<string, unknown>,
target: Record<string, unknown>,
): void => {
for (const [key, liveValue] of Object.entries(live)) {
const commonValue = common[key];
if (commonValue === undefined) {
// Case 1: 通用配置中不存在该键,完整保留到自定义配置
target[key] = deepClone(liveValue);
} else if (isPlainObject(liveValue) && isPlainObject(commonValue)) {
// Case 2: 嵌套对象,递归处理
const nested: Record<string, unknown> = {};
extract(
liveValue as Record<string, unknown>,
commonValue as Record<string, unknown>,
nested,
);
if (Object.keys(nested).length > 0) {
// 嵌套对象有差异,保留差异部分
target[key] = nested;
} else {
// 嵌套对象完全相同,标记有通用配置的键
hasCommonKeys = true;
}
} else if (!deepEqual(liveValue, commonValue)) {
// Case 3: 值不同,保留到自定义配置(用户覆盖)
target[key] = deepClone(liveValue);
} else {
// Case 4: 值完全相同,不保存到自定义配置(避免冗余)
hasCommonKeys = true;
}
}
};
extract(liveConfig, commonConfig, customConfig);
return { customConfig, hasCommonKeys };
};
// ============================================================================
// JSON 格式便捷函数
// ============================================================================
/**
* 计算最终配置(JSON 字符串版本)
*/
export const computeFinalConfigJson = (
customConfigJson: string,
commonConfigJson: string,
enabled: boolean,
): { finalConfig: string; error?: string } => {
try {
const customConfig = customConfigJson ? JSON.parse(customConfigJson) : {};
const commonConfig = commonConfigJson ? JSON.parse(commonConfigJson) : {};
if (!isPlainObject(customConfig)) {
return {
finalConfig: customConfigJson,
error: "自定义配置必须是 JSON 对象",
};
}
if (!isPlainObject(commonConfig)) {
return {
finalConfig: customConfigJson,
error: "通用配置必须是 JSON 对象",
};
}
const final = computeFinalConfig(customConfig, commonConfig, enabled);
return {
finalConfig: JSON.stringify(final, null, 2),
};
} catch (err) {
return {
finalConfig: customConfigJson,
error: `JSON 解析失败: ${err instanceof Error ? err.message : String(err)}`,
};
}
};
/**
* 从 JSON 字符串中提取差异
*/
export const extractDifferenceJson = (
liveConfigJson: string,
commonConfigJson: string,
): { customConfig: string; hasCommonKeys: boolean; error?: string } => {
try {
const liveConfig = liveConfigJson ? JSON.parse(liveConfigJson) : {};
const commonConfig = commonConfigJson ? JSON.parse(commonConfigJson) : {};
if (!isPlainObject(liveConfig)) {
return {
customConfig: liveConfigJson,
hasCommonKeys: false,
error: "Live 配置必须是 JSON 对象",
};
}
if (!isPlainObject(commonConfig)) {
return {
customConfig: liveConfigJson,
hasCommonKeys: false,
error: "通用配置必须是 JSON 对象",
};
}
const result = extractDifference(liveConfig, commonConfig);
return {
customConfig: JSON.stringify(result.customConfig, null, 2),
hasCommonKeys: result.hasCommonKeys,
};
} catch (err) {
return {
customConfig: liveConfigJson,
hasCommonKeys: false,
error: `JSON 解析失败: ${err instanceof Error ? err.message : String(err)}`,
};
}
};