mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-27 03:20:25 +08:00
feat: 系统升级支持数据库自动迁移,避免手动执行升级脚本
This commit is contained in:
@@ -54,3 +54,67 @@ export function getValueByPath(obj: any, path: string) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段路径设置字段值,若路径不存在,则建对应的路径对象信息
|
||||
* @param obj 对象
|
||||
* @param path 字段路径
|
||||
* @param value 字段值
|
||||
*/
|
||||
export function setValueByPath(obj: any, path: string[], value: any) {
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
const key = path[i];
|
||||
if (!obj[key]) {
|
||||
obj[key] = {};
|
||||
}
|
||||
obj = obj[key];
|
||||
}
|
||||
obj[path[path.length - 1]] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用递归函数进行深度克隆,并支持通过回调函数进行指定字段值的调整
|
||||
*
|
||||
* @param obj 要克隆的对象
|
||||
* @param callback 回调函数,在每个字段被克隆之前调用,可以调整字段的值
|
||||
* @param hash 用于处理循环引用的 WeakMap
|
||||
* @returns 深度克隆后的对象
|
||||
*/
|
||||
export function deepClone(
|
||||
obj: any,
|
||||
callback: (key: string | number, value: any) => any = (key: string | number, value: any) => value,
|
||||
hash = new WeakMap()
|
||||
): any {
|
||||
if (Object(obj) !== obj) return obj; // 基本数据类型直接返回
|
||||
if (hash.has(obj)) return hash.get(obj); // 处理循环引用
|
||||
|
||||
let result: any;
|
||||
|
||||
if (obj instanceof Set) {
|
||||
result = new Set();
|
||||
hash.set(obj, result);
|
||||
obj.forEach((val) => result.add(deepClone(val, callback, hash)));
|
||||
} else if (obj instanceof Map) {
|
||||
result = new Map();
|
||||
hash.set(obj, result);
|
||||
obj.forEach((val, key) => result.set(key, deepClone(val, callback, hash)));
|
||||
} else if (obj instanceof Date) {
|
||||
result = new Date(obj.getTime());
|
||||
} else if (obj instanceof RegExp) {
|
||||
result = new RegExp(obj);
|
||||
} else if (typeof obj === 'object') {
|
||||
result = Array.isArray(obj) ? [] : {};
|
||||
hash.set(obj, result);
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
let value = obj[key];
|
||||
value = callback(key, value);
|
||||
result[key] = deepClone(value, callback, hash);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = obj;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user