Skip to content

Commit 6715ffd

Browse files
lvan100lianghuan
authored andcommitted
code refactor
1 parent 363d4bc commit 6715ffd

File tree

6 files changed

+59
-52
lines changed

6 files changed

+59
-52
lines changed

gs/internal/gs/gs.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type InitFunc = interface{}
3333
// e.g., `func(bean)` or `func(bean) error`.
3434
type DestroyFunc = interface{}
3535

36+
// BeanSelectorInterface is an interface for selecting beans.
37+
type BeanSelectorInterface interface {
38+
TypeAndName() (reflect.Type, string)
39+
}
40+
3641
// BeanSelector is an identifier for a bean.
3742
type BeanSelector struct {
3843
Type reflect.Type // Type of the bean
@@ -44,6 +49,11 @@ func BeanSelectorForType[T any]() BeanSelector {
4449
return BeanSelector{Type: reflect.TypeFor[T]()}
4550
}
4651

52+
// TypeAndName returns the type and name of the bean.
53+
func (s BeanSelector) TypeAndName() (reflect.Type, string) {
54+
return s.Type, s.Name
55+
}
56+
4757
func (s BeanSelector) String() string {
4858
var sb strings.Builder
4959
sb.WriteString("{")
@@ -82,7 +92,7 @@ type CondContext interface {
8292
// Prop retrieves the value of a property from the IoC container.
8393
Prop(key string, def ...string) string
8494
// Find searches for bean definitions that match the provided BeanSelector.
85-
Find(s BeanSelector) ([]CondBean, error)
95+
Find(s BeanSelectorInterface) ([]CondBean, error)
8696
}
8797

8898
// CondFunc is a function type that determines whether a condition is satisfied.
@@ -145,7 +155,7 @@ type BeanRegistration interface {
145155
// SetCondition adds a condition for the bean.
146156
SetCondition(conditions ...Condition)
147157
// SetDependsOn sets the beans that this bean depends on.
148-
SetDependsOn(selectors ...BeanSelector)
158+
SetDependsOn(selectors ...BeanSelectorInterface)
149159
// SetExport defines the interfaces to be exported by the bean.
150160
SetExport(exports ...reflect.Type)
151161
// SetConfiguration applies the bean configuration.
@@ -159,12 +169,15 @@ type beanBuilder[T any] struct {
159169
b BeanRegistration
160170
}
161171

162-
// BeanSelector returns the BeanSelector for the bean.
163-
func (d *beanBuilder[T]) BeanSelector() BeanSelector {
164-
return BeanSelector{
165-
Name: d.BeanRegistration().Name(),
166-
Type: d.BeanRegistration().Type(),
167-
}
172+
// TypeAndName returns the type and name of the bean.
173+
func (d *beanBuilder[T]) TypeAndName() (reflect.Type, string) {
174+
r := d.BeanRegistration()
175+
return r.Type(), r.Name()
176+
}
177+
178+
// GetArgValue returns the value of the bean.
179+
func (d *beanBuilder[T]) GetArgValue(ctx ArgContext, t reflect.Type) (reflect.Value, error) {
180+
return d.BeanRegistration().Value(), nil
168181
}
169182

170183
// BeanRegistration returns the underlying BeanRegistration instance.
@@ -197,7 +210,7 @@ func (d *beanBuilder[T]) Condition(conditions ...Condition) *T {
197210
}
198211

199212
// DependsOn sets the beans that this bean depends on.
200-
func (d *beanBuilder[T]) DependsOn(selectors ...BeanSelector) *T {
213+
func (d *beanBuilder[T]) DependsOn(selectors ...BeanSelectorInterface) *T {
201214
d.b.SetDependsOn(selectors...)
202215
return *(**T)(unsafe.Pointer(&d))
203216
}
@@ -232,10 +245,6 @@ func NewRegisteredBean(d BeanRegistration) *RegisteredBean {
232245
}
233246
}
234247

235-
func (r *RegisteredBean) GetArgValue(ctx ArgContext, t reflect.Type) (reflect.Value, error) {
236-
return r.BeanRegistration().Value(), nil
237-
}
238-
239248
// BeanDefinition represents a bean that has not yet been registered in the IoC container.
240249
type BeanDefinition struct {
241250
beanBuilder[BeanDefinition]
@@ -248,10 +257,6 @@ func NewBeanDefinition(d BeanRegistration) *BeanDefinition {
248257
}
249258
}
250259

251-
func (r *BeanDefinition) GetArgValue(ctx ArgContext, t reflect.Type) (reflect.Value, error) {
252-
return r.BeanRegistration().Value(), nil
253-
}
254-
255260
/************************************ ioc ************************************/
256261

257262
// Container represents the modifiable aspects of an IoC (Inversion of Control) container.

gs/internal/gs_bean/bean.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ type BeanDestroy interface {
7474

7575
// BeanMetadata holds the metadata information of a bean.
7676
type BeanMetadata struct {
77-
f gs.Callable // Callable for the bean (ctor or method).
78-
init interface{} // Initialization function for the bean.
79-
destroy interface{} // Destruction function for the bean.
80-
dependsOn []gs.BeanSelector // List of dependencies for the bean.
81-
exports []reflect.Type // List of exported types for the bean.
82-
conditions []gs.Condition // List of conditions for the bean.
83-
status BeanStatus // Current status of the bean.
77+
f gs.Callable // Callable for the bean (ctor or method).
78+
init interface{} // Initialization function for the bean.
79+
destroy interface{} // Destruction function for the bean.
80+
dependsOn []gs.BeanSelectorInterface // List of dependencies for the bean.
81+
exports []reflect.Type // List of exported types for the bean.
82+
conditions []gs.Condition // List of conditions for the bean.
83+
status BeanStatus // Current status of the bean.
8484

8585
file string // The file where the bean is defined.
8686
line int // The line number in the file where the bean is defined.
@@ -103,7 +103,7 @@ func (d *BeanMetadata) Destroy() interface{} {
103103
}
104104

105105
// DependsOn returns the list of dependencies for the bean.
106-
func (d *BeanMetadata) DependsOn() []gs.BeanSelector {
106+
func (d *BeanMetadata) DependsOn() []gs.BeanSelectorInterface {
107107
return d.dependsOn
108108
}
109109

@@ -203,6 +203,11 @@ type BeanDefinition struct {
203203
*BeanRuntime
204204
}
205205

206+
// TypeAndName returns the type and name of the bean.
207+
func (d *BeanDefinition) TypeAndName() (reflect.Type, string) {
208+
return d.Type(), d.Name()
209+
}
210+
206211
// Callable returns the callable for the bean.
207212
func (d *BeanDefinition) Callable() gs.Callable {
208213
return d.f
@@ -267,7 +272,7 @@ func (d *BeanDefinition) SetCondition(conditions ...gs.Condition) {
267272
}
268273

269274
// SetDependsOn sets the list of dependencies for the bean.
270-
func (d *BeanDefinition) SetDependsOn(selectors ...gs.BeanSelector) {
275+
func (d *BeanDefinition) SetDependsOn(selectors ...gs.BeanSelectorInterface) {
271276
d.dependsOn = append(d.dependsOn, selectors...)
272277
}
273278

gs/internal/gs_cond/cond.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ func (c *onMissingProperty) String() string {
166166
// onBean checks for the existence of beans that match a selector.
167167
// It returns true if at least one bean matches the selector, and false otherwise.
168168
type onBean struct {
169-
s gs.BeanSelector // The selector used to match beans in the context.
169+
s gs.BeanSelectorInterface // The selector used to match beans in the context.
170170
}
171171

172172
// OnBean creates a condition that evaluates to true if at least one bean matches the provided selector.
173-
func OnBean(s gs.BeanSelector) gs.Condition {
173+
func OnBean(s gs.BeanSelectorInterface) gs.Condition {
174174
return &onBean{s: s}
175175
}
176176

@@ -191,11 +191,11 @@ func (c *onBean) String() string {
191191
// onMissingBean checks for the absence of beans matching a selector.
192192
// It returns true if no beans match the selector, and false otherwise.
193193
type onMissingBean struct {
194-
s gs.BeanSelector // The selector used to find beans.
194+
s gs.BeanSelectorInterface // The selector used to find beans.
195195
}
196196

197197
// OnMissingBean creates a condition that evaluates to true if no beans match the provided selector.
198-
func OnMissingBean(s gs.BeanSelector) gs.Condition {
198+
func OnMissingBean(s gs.BeanSelectorInterface) gs.Condition {
199199
return &onMissingBean{s: s}
200200
}
201201

@@ -216,11 +216,11 @@ func (c *onMissingBean) String() string {
216216
// onSingleBean checks if exactly one matching bean exists in the context.
217217
// It returns true if exactly one bean matches the selector, and false otherwise.
218218
type onSingleBean struct {
219-
s gs.BeanSelector // The selector used to find beans.
219+
s gs.BeanSelectorInterface // The selector used to find beans.
220220
}
221221

222222
// OnSingleBean creates a condition that evaluates to true if exactly one bean matches the provided selector.
223-
func OnSingleBean(s gs.BeanSelector) gs.Condition {
223+
func OnSingleBean(s gs.BeanSelectorInterface) gs.Condition {
224224
return &onSingleBean{s: s}
225225
}
226226

gs/internal/gs_core/bean.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@ func NewBean(objOrCtor interface{}, ctorArgs ...gs.Arg) *gs.BeanDefinition {
101101
// Check if the function is a method and set a condition if needed
102102
method := strings.LastIndexByte(fnInfo.Name(), ')') > 0
103103
if method {
104-
s := gs.BeanSelector{Type: in0}
104+
var s gs.BeanSelectorInterface = gs.BeanSelector{Type: in0}
105105
if len(ctorArgs) > 0 {
106106
switch a := ctorArgs[0].(type) {
107107
case *gs.RegisteredBean:
108-
s = a.BeanSelector()
108+
s = a
109109
case *gs.BeanDefinition:
110-
s = a.BeanSelector()
110+
s = a
111111
case gs_arg.IndexArg:
112112
if a.Idx == 0 {
113113
switch x := a.Arg.(type) {
114114
case *gs.RegisteredBean:
115-
s = x.BeanSelector()
115+
s = x
116116
case *gs.BeanDefinition:
117-
s = x.BeanSelector()
117+
s = x
118118
default:
119119
panic("the arg of IndexArg[0] should be *RegisteredBean or *BeanDefinition")
120120
}

gs/internal/gs_core/core.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ func (c *resolvingStage) scanConfiguration(bd *gs_bean.BeanDefinition) ([]*gs_be
514514
name := bd.Name() + "_" + m.Name
515515
b := gs_bean.NewBean(v.Type(), v, f, name)
516516
b.SetFileLine(file, line)
517-
gs.NewBeanDefinition(b).Condition(gs_cond.OnBean(
518-
gs.BeanSelector{Type: bd.Type(), Name: bd.Name()},
519-
))
517+
gs.NewBeanDefinition(b).Condition(gs_cond.OnBean(bd))
520518
newBeans = append(newBeans, b)
521519
}
522520
break
@@ -553,13 +551,14 @@ func (c *resolvingStage) Prop(key string, def ...string) string {
553551

554552
// Find 查找符合条件的 bean 对象,注意该函数只能保证返回的 bean 是有效的,
555553
// 即未被标记为删除的,而不能保证已经完成属性绑定和依赖注入。
556-
func (c *resolvingStage) Find(s gs.BeanSelector) ([]gs.CondBean, error) {
554+
func (c *resolvingStage) Find(s gs.BeanSelectorInterface) ([]gs.CondBean, error) {
555+
t, name := s.TypeAndName()
557556
var result []gs.CondBean
558557
for _, b := range c.beans {
559558
if b.Status() == gs_bean.StatusResolving || b.Status() == gs_bean.StatusDeleted {
560559
continue
561560
}
562-
if t := s.Type; t != nil {
561+
if t != nil {
563562
if b.Type() != t {
564563
foundType := false
565564
for _, typ := range b.Exports() {
@@ -573,7 +572,7 @@ func (c *resolvingStage) Find(s gs.BeanSelector) ([]gs.CondBean, error) {
573572
}
574573
}
575574
}
576-
if s.Name != "" && s.Name != b.Name() {
575+
if name != "" && name != b.Name() {
577576
continue
578577
}
579578
if err := c.resolveBean(b); err != nil {

gs/internal/gs_core/wire.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (a *ArgContext) Prop(key string, def ...string) string {
188188
return a.c.Prop(key, def...)
189189
}
190190

191-
func (a *ArgContext) Find(s gs.BeanSelector) ([]gs.CondBean, error) {
191+
func (a *ArgContext) Find(s gs.BeanSelectorInterface) ([]gs.CondBean, error) {
192192
beans, err := a.c.findBeans(s)
193193
if err != nil {
194194
return nil, err
@@ -271,21 +271,19 @@ func parseWireTag(p gs.Properties, str string, needResolve bool) (tag wireTag, e
271271
}
272272

273273
// findBeans finds beans based on a given selector.
274-
func (c *Container) findBeans(s gs.BeanSelector) ([]BeanRuntime, error) {
274+
func (c *Container) findBeans(s gs.BeanSelectorInterface) ([]BeanRuntime, error) {
275+
t, name := s.TypeAndName()
275276
var beans []BeanRuntime
276-
if t := s.Type; t != nil {
277+
if t != nil {
277278
beans = c.beansByType[t]
278279
}
279-
if s.Name != "" {
280-
if s.Name == "" {
281-
return nil, fmt.Errorf("bean name is empty")
282-
}
280+
if name != "" {
283281
if beans == nil {
284-
beans = c.beansByName[s.Name]
282+
beans = c.beansByName[name]
285283
}
286284
var ret []BeanRuntime
287285
for _, b := range beans {
288-
if s.Name == b.Name() {
286+
if name == b.Name() {
289287
ret = append(ret, b)
290288
}
291289
}

0 commit comments

Comments
 (0)