Skip to content

Commit

Permalink
新增:支持环境变量,数组规则yml
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jul 22, 2024
1 parent 8c69bba commit 7e90f08
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
32 changes: 20 additions & 12 deletions configure/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configure

import (
"github.com/farseer-go/fs/parse"
"strings"
)

Expand Down Expand Up @@ -70,7 +71,6 @@ func (c *config) Get(key string) any {

// GetSubNodes 获取所有子节点
func (c *config) GetSubNodes(key string) map[string]any {
// 这里需要倒序获取列表,利用后面覆盖前面的方式来获取
m := make(map[string]any)
// 先加载默认值
prefixKey := key + "."
Expand All @@ -80,6 +80,7 @@ func (c *config) GetSubNodes(key string) map[string]any {
}
}

// 这里需要倒序获取列表,利用后面覆盖前面的方式来获取
// 再添加yaml、环境变量
for i := len(c.configProvider) - 1; i >= 0; i-- {
if subMap, exists := c.configProvider[i].GetSubNodes(key); exists {
Expand All @@ -99,21 +100,28 @@ func (c *config) GetSubNodes(key string) map[string]any {

// GetSlice 获取数组
func (c *config) GetSlice(key string) []string {
// 遍历配置提供者
for _, provider := range c.configProvider {
v, exists := provider.Get(key)
if exists {
m, isOk := v.([]any)
if isOk {
var arr []string
for _, s := range m {
arr = append(arr, s.(string))
var result []string
// 先加载默认值
if defVal, exists := c.def[key]; exists {
switch defArr := defVal.(type) {
case []string:
result = defArr
}
}

// 这里需要倒序获取列表,利用后面覆盖前面的方式来获取
// 再添加yaml、环境变量
for i := len(c.configProvider) - 1; i >= 0; i-- {
if arrVal, exists := c.configProvider[i].GetArray(key); exists {
for arrIndex, val := range arrVal {
for len(result) <= arrIndex {
result = append(result, "")
}
return arr
result[arrIndex] = parse.ToString(val)
}
}
}
return []string{}
return result
}

// GetSliceNodes 获取数组节点
Expand Down
24 changes: 24 additions & 0 deletions configure/envConfig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configure

import (
"github.com/farseer-go/fs/parse"
"os"
"strings"
)
Expand Down Expand Up @@ -29,6 +30,29 @@ func (r *envConfig) Get(key string) (any, bool) {
return nil, false
}

func (r *envConfig) GetArray(key string) ([]any, bool) {
prefixKey := strings.ToLower(r.replace(key) + "_")
var result []any
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if len(pair) != 2 {
continue
}

// 有指定key的前缀,说明是数组
envKey := strings.ToLower(r.replace(pair[0]))
if strings.HasPrefix(envKey, prefixKey) {
// 得到索引
arrIndex := parse.ToInt(envKey[len(prefixKey):])
for len(result) <= arrIndex {
result = append(result, "")
}
result[arrIndex] = pair[1]
}
}
return result, len(result) > 0
}

func (r *envConfig) GetSubNodes(key string) (map[string]any, bool) {
m := make(map[string]any)
prefixKey := strings.ToLower(r.replace(key) + "_")
Expand Down
2 changes: 2 additions & 0 deletions configure/iConfigLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type IConfigProvider interface {
Get(key string) (any, bool)
// GetSubNodes 获取所有子节点
GetSubNodes(key string) (map[string]any, bool)
// GetArray 读取切片
GetArray(key string) ([]any, bool)
// Name 提供者名称
Name() string
}

0 comments on commit 7e90f08

Please sign in to comment.