From 95a20ea1e439dc3c3d7757d357833d2e8d22e21e Mon Sep 17 00:00:00 2001 From: shanyujie <1196661499@qq.com> Date: Mon, 19 Jan 2026 17:44:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E6=9B=B4=E6=96=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E9=80=82=E9=85=8D=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=92=8C=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 jsonMap 从 StrAnyMap 类型更改为泛型 KVMap[string, *gjson.Json] 类型 - 添加 jsonMapChecker 函数用于 JSON 对象验证 - 使用 NewKVMapWithChecker 替代 NewStrAnyMap 提高类型安全性 - 修改 GetOrSetFuncLock 回调函数返回类型为 *gjson.Json - 简化数据库链接关闭日志中的键值转换逻辑 --- database/gdb/gdb_core.go | 2 +- os/gcfg/gcfg_adapter_file.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 6cca52c7e..4b23af05d 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -119,7 +119,7 @@ func (c *Core) Close(ctx context.Context) (err error) { if err != nil { err = gerror.WrapCode(gcode.CodeDbOperationError, err, `db.Close failed`) } - intlog.Printf(ctx, `close link: %s, err: %v`, k, err) + intlog.Printf(ctx, `close link: %s, err: %v`, gconv.String(k), err) if err != nil { return } diff --git a/os/gcfg/gcfg_adapter_file.go b/os/gcfg/gcfg_adapter_file.go index 459a40967..cb3c4837f 100644 --- a/os/gcfg/gcfg_adapter_file.go +++ b/os/gcfg/gcfg_adapter_file.go @@ -32,11 +32,11 @@ var ( // AdapterFile implements interface Adapter using file. type AdapterFile struct { - defaultFileNameOrPath *gtype.String // Default configuration file name or file path. - searchPaths *garray.StrArray // Searching the path array. - jsonMap *gmap.StrAnyMap // The pared JSON objects for configuration files. - violenceCheck bool // Whether it does violence check in value index searching. It affects the performance when set true(false in default). - watchers *WatcherRegistry // Watchers for watching file changes. + defaultFileNameOrPath *gtype.String // Default configuration file name or file path. + searchPaths *garray.StrArray // Searching the path array. + jsonMap *gmap.KVMap[string, *gjson.Json] // The pared JSON objects for configuration files. + violenceCheck bool // Whether it does violence check in value index searching. It affects the performance when set true(false in default). + watchers *WatcherRegistry // Watchers for watching file changes. } const ( @@ -58,6 +58,9 @@ var ( // Prefix array for trying searching in the local system. localSystemTryFolders = []string{"", "config/", "manifest/config"} + + // jsonMapChecker is the checker for JSON map. + jsonMapChecker = func(v *gjson.Json) bool { return v == nil } ) // NewAdapterFile returns a new configuration management object. @@ -78,7 +81,7 @@ func NewAdapterFile(fileNameOrPath ...string) (*AdapterFile, error) { config := &AdapterFile{ defaultFileNameOrPath: gtype.NewString(usedFileNameOrPath), searchPaths: garray.NewStrArray(true), - jsonMap: gmap.NewStrAnyMap(true), + jsonMap: gmap.NewKVMapWithChecker[string, *gjson.Json](jsonMapChecker, true), watchers: NewWatcherRegistry(), } // Customized dir path from env/cmd. @@ -257,7 +260,7 @@ func (a *AdapterFile) getJson(fileNameOrPath ...string) (configJson *gjson.Json, usedFileNameOrPath = fileNameOrPath[0] } // It uses JSON map to cache specified configuration file content. - result := a.jsonMap.GetOrSetFuncLock(usedFileNameOrPath, func() any { + result := a.jsonMap.GetOrSetFuncLock(usedFileNameOrPath, func() *gjson.Json { var ( content string filePath string @@ -326,7 +329,7 @@ func (a *AdapterFile) getJson(fileNameOrPath ...string) (configJson *gjson.Json, return configJson }) if result != nil { - return result.(*gjson.Json), err + return result, err } return }