瀏覽代碼

:edit 修改 结构体解析组件

fly 1 年之前
父節點
當前提交
86a2f67fc5
共有 2 個文件被更改,包括 29 次插入4 次删除
  1. 14 2
      config/config.go
  2. 15 2
      config/object.go

+ 14 - 2
config/config.go

@@ -63,8 +63,20 @@ func Set(module, key string, value any) {
 	viper.Set(module+"."+key, value)
 }
 
-func Unmarshal(module, key string, value any) error {
-	return viper.UnmarshalKey(module+"."+key, value)
+func Unmarshal(value any, arr ...string) error {
+	key := ""
+
+	for _, s := range arr {
+		if len(key) != 0 {
+			key += "."
+		}
+		key += s
+	}
+	if len(key) != 0 {
+		return viper.UnmarshalKey(key, value)
+	} else {
+		return viper.Unmarshal(value)
+	}
 }
 
 func ReadConfigAndFileExist() (bool, error) {

+ 15 - 2
config/object.go

@@ -68,8 +68,21 @@ func (c *config) Set(module, key string, value any) {
 	c.Viper.Set(module+"."+key, value)
 }
 
-func (c *config) Unmarshal(module, key string, value any) error {
-	return c.Viper.UnmarshalKey(module+"."+key, value)
+func (c *config) Unmarshal(value any, arr ...string) error {
+	key := ""
+
+	for _, s := range arr {
+		if len(key) != 0 {
+			key += "."
+		}
+		key += s
+	}
+	if len(key) != 0 {
+		return c.Viper.UnmarshalKey(key, value)
+	} else {
+		return c.Viper.Unmarshal(value)
+	}
+
 }
 
 func (c *config) ReadConfigAndFileExist() (bool, error) {