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 }