开源地址:
goconfig: git clone https://gitee.com/ichub/go.git
参考springcloud框架
础结构
package ichubconfig import ( "gitee.com/ichub/goconfig/common/base/basedto" "gitee.com/ichub/goconfig/common/base/baseutils/reflectutils" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" "github.com/sirupsen/logrus" "reflect" "strings" ) type BaseConfig struct { basedto.BaseEntitySingle } func NewBaseConfig() *BaseConfig { var bc = &BaseConfig{} bc.InitProxy(bc) return bc } /* some is ptr */ func (bc *BaseConfig) SetFieldConfigItem(some any, key string, value any) { var values = strings.Split(value.(string), ":") var rv = FindBeanIchubConfig().ReadVar(gstr.Trim(values[0])) var ifnil bool = rv == "" if ifnil && len(values) < 2 { return } if ifnil { rv = reflect.ValueOf(gstr.Trim(values[1])) } var rt = reflect.ValueOf(some).Elem() var fieldvalue = rt.FieldByName(key) switch fieldvalue.Kind() { case reflect.Int: fieldvalue.SetInt(gconv.Int64(rv)) case reflect.String: fieldvalue.SetString(gconv.String(rv)) case reflect.Bool: fieldvalue.SetBool(gconv.Bool(rv)) case reflect.Float64: fieldvalue.SetFloat(gconv.Float64(rv)) case reflect.Float32: fieldvalue.SetFloat(gconv.Float64(rv)) case reflect.Uint: fieldvalue.SetUint(gconv.Uint64(rv)) } } func (bc *BaseConfig) InjectConfigItem(some any) { var linkmap = reflectutils.ParseGoTag(some) for _, key := range linkmap.Keys() { value, found := linkmap.Get(key) if found { bc.SetFieldConfigItem(some, key.(string), value) } } if bc.Proxy() == nil { panic("some is nil!") } logrus.Info(some) }应用
应用结构
package goapolloconfig import ( "gitee.com/ichub/goconfig/common/ichubconfig" ) type RedisBaseConfig struct { ichubconfig.BaseConfig Id int `gocfg:"redis.db:0"` WebServerName string `gocfg:"web.server.name"` MaxLifetime int `gocfg:"gorm.maxLifetime:1200"` } func NewRedisBaseConfig() *RedisBaseConfig { var rbc = &RedisBaseConfig{} rbc.InitProxy(rbc) return rbc }
定义接口
package baseiface type IbaseConfig interface { InjectConfigItem(some any) }
godi di all生成注入代码
package goapolloconfig
import (
"gitee.com/ichub/goconfig/common/base/baseiface"
"gitee.com/ichub/goconfig/common/basedi"
"github.com/sirupsen/logrus"
"gitee.com/ichub/goconfig/common/base/baseutils/goutils"
)
/*
* ********************************************************
* godi工具生成代码,不建议直接修改!
* ********************************************************
*/
/* ********************************************************
@Title 文件名称: redis_base_config_init.go
@Desp 描述: 自动注册注入
@Author 作者: leijmdas@163.com 时间(2024-08-31 18:38:25)
@Update 作者: leijmdas@163.com 时间(2024-08-31 18:38:25)
* *********************************************************/
const singleNameRedisBaseConfig = "*goapolloconfig.RedisBaseConfig-e60c0d18ff434618ac3cf28656cfb7dc"
// init register load
func init() {
registerBeanRedisBaseConfig()
}
// register RedisBaseConfig
func registerBeanRedisBaseConfig() {
basedi.RegisterLoadBean(singleNameRedisBaseConfig, LoadRedisBaseConfig)
}
func FindBeanRedisBaseConfig() *RedisBaseConfig {
bean, ok := basedi.FindBean(singleNameRedisBaseConfig).(*RedisBaseConfig)
if !ok {
logrus.Errorf("FindBeanRedisBaseConfig: failed to cast bean to *RedisBaseConfig")
return nil
}
return bean
}
func LoadRedisBaseConfig() baseiface.ISingleton {
var s = NewRedisBaseConfig()
InjectRedisBaseConfig(s)
return s
}
func InjectRedisBaseConfig(s *RedisBaseConfig) {
goutils.Debug("inject")
}
测试用例与测试结果
func Test012_FindBeanRedisBaseConfig(t *testing.T) { FindBeanRedisBaseConfig() }
INFO[2024-08-831 18:51:57]D:/go-ichub/go/goconfig/common/ichubconfig/base_config.go:70 gitee.com/ichub/goconfig/common/ichubconfig.(*BaseConfig).InjectConfigItem() {
"Id": 0,
"WebServerName": "web.server.com",
"MaxLifetime": 7200
}
注入代码实现:
func (this *beanContainer) FindBean(name string) (*BeanInfo, bool) { this.lock.RLock() defer this.lock.RUnlock() var beanInfo, ok = this.container[name] if !ok { logrus.Error(name, " bean found?", ok) return nil, ok } if beanInfo.bean == nil { beanInfo.SetBean(beanInfo.CreateBean()) } this.autoInjectConfigItem(beanInfo) return beanInfo, ok } func (this *beanContainer) autoInjectConfigItem(beanInfo *BeanInfo) { if ibcfg, ok := beanInfo.bean.(baseiface.IbaseConfig); ok { ibcfg.InjectConfigItem(ibcfg) } }
配置文件: