mirror of
https://github.com/Monibuca/engine.git
synced 2025-12-24 13:18:07 +08:00
fix: global value override
This commit is contained in:
@@ -17,8 +17,7 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Ptr reflect.Value //指向配置结构体值
|
||||
Value any //当前值,优先级:动态修改值>环境变量>配置文件>defaultYaml>全局配置>默认值
|
||||
Ptr reflect.Value //指向配置结构体值,优先级:动态修改值>环境变量>配置文件>defaultYaml>全局配置>默认值
|
||||
Modify any //动态修改的值
|
||||
Env any //环境变量中的值
|
||||
File any //配置文件中的值
|
||||
@@ -58,7 +57,7 @@ type QuicPlugin interface {
|
||||
}
|
||||
|
||||
func (config *Config) Range(f func(key string, value Config)) {
|
||||
if m, ok := config.Value.(map[string]Config); ok {
|
||||
if m, ok := config.GetValue().(map[string]Config); ok {
|
||||
for k, v := range m {
|
||||
f(k, v)
|
||||
}
|
||||
@@ -66,7 +65,7 @@ func (config *Config) Range(f func(key string, value Config)) {
|
||||
}
|
||||
|
||||
func (config *Config) IsMap() bool {
|
||||
_, ok := config.Value.(map[string]Config)
|
||||
_, ok := config.GetValue().(map[string]Config)
|
||||
return ok
|
||||
}
|
||||
|
||||
@@ -96,11 +95,15 @@ func (config Config) Has(key string) (ok bool) {
|
||||
|
||||
func (config *Config) MarshalJSON() ([]byte, error) {
|
||||
if config.propsMap == nil {
|
||||
return json.Marshal(config.Value)
|
||||
return json.Marshal(config.GetValue())
|
||||
}
|
||||
return json.Marshal(config.propsMap)
|
||||
}
|
||||
|
||||
func (config *Config) GetValue() any{
|
||||
return config.Ptr.Interface()
|
||||
}
|
||||
|
||||
// Parse 第一步读取配置结构体的默认值
|
||||
func (config *Config) Parse(s any, prefix ...string) {
|
||||
var t reflect.Type
|
||||
@@ -115,13 +118,11 @@ func (config *Config) Parse(s any, prefix ...string) {
|
||||
}
|
||||
config.Ptr = v
|
||||
config.Default = v.Interface()
|
||||
config.Value = v.Interface()
|
||||
if len(prefix) > 0 { // 读取环境变量
|
||||
envKey := strings.Join(prefix, "_")
|
||||
if envValue := os.Getenv(envKey); envValue != "" {
|
||||
envv := config.assign(strings.ToLower(prefix[0]), envValue)
|
||||
config.Env = envv.Interface()
|
||||
config.Value = config.Env
|
||||
config.Ptr.Set(envv)
|
||||
}
|
||||
}
|
||||
@@ -170,7 +171,7 @@ func (config *Config) ParseGlobal(g *Config) {
|
||||
v.ParseGlobal(g.Get(k))
|
||||
}
|
||||
} else {
|
||||
config.Value = g.Value
|
||||
config.Ptr.Set(g.Ptr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +190,6 @@ func (config *Config) ParseDefaultYaml(defaultYaml map[string]any) {
|
||||
dv := prop.assign(k, v)
|
||||
prop.Default = dv.Interface()
|
||||
if prop.Env == nil {
|
||||
prop.Value = dv.Interface()
|
||||
prop.Ptr.Set(dv)
|
||||
}
|
||||
}
|
||||
@@ -213,7 +213,6 @@ func (config *Config) ParseUserFile(conf map[string]any) {
|
||||
fv := prop.assign(k, v)
|
||||
prop.File = fv.Interface()
|
||||
if prop.Env == nil {
|
||||
prop.Value = fv.Interface()
|
||||
prop.Ptr.Set(fv)
|
||||
}
|
||||
}
|
||||
@@ -245,13 +244,11 @@ func (config *Config) ParseModifyFile(conf map[string]any) {
|
||||
delete(conf, k)
|
||||
if prop.Modify != nil {
|
||||
prop.Modify = nil
|
||||
prop.Value = vwm
|
||||
prop.Ptr.Set(reflect.ValueOf(vwm))
|
||||
}
|
||||
continue
|
||||
}
|
||||
prop.Modify = v
|
||||
prop.Value = v
|
||||
prop.Ptr.Set(mv)
|
||||
}
|
||||
}
|
||||
@@ -269,7 +266,7 @@ func (config *Config) valueWithoutModify() any {
|
||||
return config.File
|
||||
}
|
||||
if config.Global != nil {
|
||||
return config.Global.Value
|
||||
return config.Global.GetValue()
|
||||
}
|
||||
return config.Default
|
||||
}
|
||||
@@ -295,8 +292,8 @@ func (config *Config) GetMap() map[string]any {
|
||||
if vv := v.GetMap(); vv != nil {
|
||||
m[k] = vv
|
||||
}
|
||||
} else if v.Value != nil {
|
||||
m[k] = v.Value
|
||||
} else if v.GetValue() != nil {
|
||||
m[k] = v.GetValue()
|
||||
}
|
||||
}
|
||||
if len(m) > 0 {
|
||||
|
||||
@@ -30,3 +30,24 @@ func TestModify(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestGlobal 测试全局配置
|
||||
func TestGlobal(t *testing.T) {
|
||||
t.Run(t.Name(), func(t *testing.T) {
|
||||
var defaultValue struct{
|
||||
Publish
|
||||
}
|
||||
var globalValue struct {
|
||||
Publish
|
||||
}
|
||||
globalValue.Publish.KickExist = true
|
||||
var conf Config
|
||||
var globalConf Config
|
||||
globalConf.Parse(&globalValue)
|
||||
conf.Parse(&defaultValue)
|
||||
conf.ParseGlobal(&globalConf)
|
||||
if defaultValue.Publish.KickExist != true {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
0
config/fatal/latest.log
Normal file
0
config/fatal/latest.log
Normal file
@@ -67,7 +67,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
} else {
|
||||
p := Property{
|
||||
Title: config.name,
|
||||
Default: config.Value,
|
||||
Default: config.GetValue(),
|
||||
DecoratorProps: map[string]any{
|
||||
"tooltip": config.tag.Get("desc"),
|
||||
},
|
||||
@@ -90,7 +90,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
p.Type = "string"
|
||||
p.Component = "Input"
|
||||
p.DecoratorProps["addonAfter"] = "正则表达式"
|
||||
str := config.Value.(Regexp).String()
|
||||
str := config.GetValue().(Regexp).String()
|
||||
p.ComponentProps = map[string]any{
|
||||
"placeholder": str,
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
case durationType:
|
||||
p.Type = "string"
|
||||
p.Component = "Input"
|
||||
str := config.Value.(time.Duration).String()
|
||||
str := config.GetValue().(time.Duration).String()
|
||||
p.ComponentProps = map[string]any{
|
||||
"placeholder": str,
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (config *Config) schema(index int) (r any) {
|
||||
p.Type = "number"
|
||||
p.Component = "InputNumber"
|
||||
p.ComponentProps = map[string]any{
|
||||
"placeholder": fmt.Sprintf("%v", config.Value),
|
||||
"placeholder": fmt.Sprintf("%v", config.GetValue()),
|
||||
}
|
||||
case reflect.Bool:
|
||||
p.Type = "boolean"
|
||||
@@ -119,13 +119,13 @@ func (config *Config) schema(index int) (r any) {
|
||||
p.Type = "string"
|
||||
p.Component = "Input"
|
||||
p.ComponentProps = map[string]any{
|
||||
"placeholder": config.Value,
|
||||
"placeholder": config.GetValue(),
|
||||
}
|
||||
case reflect.Slice:
|
||||
p.Type = "array"
|
||||
p.Component = "Input"
|
||||
p.ComponentProps = map[string]any{
|
||||
"placeholder": config.Value,
|
||||
"placeholder": config.GetValue(),
|
||||
}
|
||||
p.DecoratorProps["addonAfter"] = "数组,每个元素用逗号分隔"
|
||||
case reflect.Map:
|
||||
|
||||
2
io.go
2
io.go
@@ -218,7 +218,7 @@ func (io *IO) receive(streamPath string, specific IIO) error {
|
||||
if !isSubscribe {
|
||||
puber := iPub.GetPublisher()
|
||||
conf := puber.Config
|
||||
io.Info("publish", zap.String("ptr", fmt.Sprintf("%p", io.Context)))
|
||||
io.Info("publish", zap.String("ptr", fmt.Sprintf("%p", iPub)))
|
||||
s.pubLocker.Lock()
|
||||
defer s.pubLocker.Unlock()
|
||||
if config.Global.EnableAuth {
|
||||
|
||||
Reference in New Issue
Block a user