Go实战全家桶之二: GO依赖注入框架godi之十:go BaseEntity通用结构体功能ParseTags、ContainType与Autowire自动注入结构成员-- 工业级巨匠、开发者神器

BaseEntity ParseTags Contain

type BaseEntity struct {
    Proxy    func() *BaseProxy             `json:"-" gorm:"-"`
    Bindings map[string]baseiface.LoadBean `json:"-" gorm:"-"`

    RootDir string `json:"-" gorm:"-"`
    Ver     string `json:"-"  gorm:"-"`
}
func LoadBeanProxy[T baseiface.IbaseProxy]() T {
    var baseproxy T
    var typeOf = reflect.TypeOf(baseproxy)

    var value = reflect.New(typeOf.Elem())
    baseproxy = value.Interface().(T)
    baseproxy.InitProxy(baseproxy)
    baseproxy.Init()

    return baseproxy

}

func (self *BaseEntity) IfAutowire() bool {
    if self.Proxy != nil {
       return self.Proxy().IfAutowire()
    }
    return false

}
func (self *BaseEntity) Autowire() {
    if self.Proxy != nil {
       self.Proxy().Autowire()
    }

}
func (self *BaseEntity) ContainsStru(v any) bool {
    if self.Proxy != nil {
       return self.Proxy().ContainsStru(v)
    }
    return false
}
func (self *BaseEntity) ParseTags(tagkey string) map[string]string {
    if self.Proxy != nil {
       return self.Proxy().ParseTags(tagkey)
    }
func Autowired(some any) error {

    var fields = FindStructFields(some)
    for _, field := range fields {
       var t = field.Type
       if t.Kind() == reflect.Ptr {
          var baseproxy, ok = NewProxyTypeOf(t)
          if ok {
             err := xreflect.SetField(some, field.Name, baseproxy)
             if err != nil {
                logrus.Error(err)
                continue
             }
          }
       }
    }

    return nil
}
type Stru struct {
    basedto.Gometa `gometa:"autowired:true"`
    basedto.BaseEntity
    S1  *Stru
    S2  *Stru
    Dto baseconfig.DbClientDto `godi:"autowired:true"`
    I   int
    *baseconfig.DbClientDto
}
func (this *TestBeanSuite) Test009_Parsetags() {
    var stru = basedi.FindBeanGeneral[*Stru]()
    var tags = stru.ParseTags("godi")
    golog.Info(jsonutils.ToJsonPretty(tags))

}

2024-10-09 13:17:40.245 [INFO] {
     "Dto": "autowired:true"
}

func (this *TestBeanSuite) Test004_FindStructFields() {
    var stru = basedi.FindBeanGeneral[*Stru]()
    stru.Autowire()
    stru.S1.I = 2333322
    stru.DbClientDto.Username = "leijmdas"
    golog.Info(stru)
}

func FindBeanGeneral[T baseiface.IbaseProxy]() T {
    factory := NewBeanFactroy[T]()
    bean, err := factory.FindBeanGeneral()
    if err != nil {
       logErrorWithDetails("Failed to find bean", err)
       return bean // 返回默认值
    }
    //factory.Autowired(bean)
    return bean
}
func (self *BeanFactroy[T]) FindBeanGeneral() (T, error) {
    var beanName, err = self.RegisterLoadBean()
    if err != nil {
       var ptr T
       return ptr, err
    }
    return FindBean(beanName).(T), nil
}
func FindBeanOk(name string) (baseiface.ISingleton, bool) {
    var beanInfo, ok = container.FindBean(name)
    if beanInfo == nil {
       return nil, ok
    }
    if beanInfo.bean.Single() {
       return beanInfo.Bean(), ok
    }
    return beanInfo.CreateBean(), ok
}
func (this *BeanInfo) CreateBean() baseiface.ISingleton {

    var single = this.loadBean()
    single.InitProxy(single)
    if single.IfAutowire() {
       single.Autowire()
    }
    return single
}

water/gobase

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值