Skip to content

Commit ba375be

Browse files
committed
Method bean depend on parent bean
1 parent d6726a9 commit ba375be

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

gs/gs.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,16 @@ func (c *container) Configuration(i interface{}) *BeanDefinition {
204204
bValue := reflect.ValueOf(i)
205205
bType := bValue.Type()
206206

207+
parentBean := c.Accept(NewBean(bValue))
208+
207209
for j := 0; j < bType.NumMethod(); j++ {
208210
method := bType.Method(j)
209211
if !strings.HasPrefix(method.Name, "New") {
210212
continue
211213
}
212214

215+
var bd *BeanDefinition
216+
213217
switch method.Type.NumOut() {
214218
case 1: // func(x *T) NewFoo() *Foo
215219
outType := method.Type.Out(0)
@@ -219,16 +223,15 @@ func (c *container) Configuration(i interface{}) *BeanDefinition {
219223

220224
if outType == bdType {
221225
if method.Type.NumIn() != 1 {
222-
panic(fmt.Errorf("non-parameter constructor required: %s", method.Type.String()))
226+
panic(fmt.Errorf("non-parameter constructor required: %s: %s", method.Name, method.Type.String()))
223227
}
224228

225229
bdValues := method.Func.Call([]reflect.Value{bValue})
226230
bdInst := bdValues[0].Interface()
227-
c.Accept(bdInst.(*BeanDefinition))
231+
bd = c.Accept(bdInst.(*BeanDefinition))
228232
} else {
229-
c.Provide(method.Func.Interface())
233+
bd = c.Provide(method.Func.Interface())
230234
}
231-
232235
case 2:
233236
out0Type := method.Type.Out(0)
234237
if !utils.IsBeanType(out0Type) {
@@ -237,12 +240,12 @@ func (c *container) Configuration(i interface{}) *BeanDefinition {
237240

238241
out1Type := method.Type.Out(1)
239242
if !utils.IsErrorType(out1Type) {
240-
panic(fmt.Errorf("%s: second return type must be error", method.Type.String()))
243+
panic(fmt.Errorf("%s: %s: second return type must be error", method.Name, method.Type.String()))
241244
}
242245

243246
if out0Type == bdType {
244247
if method.Type.NumIn() != 1 {
245-
panic(fmt.Errorf("non-parameter constructor required: %s", method.Type.String()))
248+
panic(fmt.Errorf("non-parameter constructor required: %s: %s", method.Name, method.Type.String()))
246249
}
247250

248251
bdValues := method.Func.Call([]reflect.Value{bValue})
@@ -251,16 +254,19 @@ func (c *container) Configuration(i interface{}) *BeanDefinition {
251254
if err, ok := bdErr.(error); ok && nil != err {
252255
panic(fmt.Errorf("%s: %w", method.Type.String(), err))
253256
}
254-
c.Accept(bdInst.(*BeanDefinition))
257+
bd = c.Accept(bdInst.(*BeanDefinition))
255258
} else {
256-
c.Provide(method.Func.Interface())
259+
bd = c.Provide(method.Func.Interface())
257260
}
258261
}
259262

260-
// ignore other methods
263+
// 修改注册行号信息为父级bean注册位置
264+
bd.file, bd.line = parentBean.file, parentBean.line
265+
// 依赖父级bean
266+
bd.On(cond.OnBean(parentBean.ID()))
261267
}
262268

263-
return c.Accept(NewBean(bValue))
269+
return parentBean
264270
}
265271

266272
// Dependencies return the dependency order list in either ascending or descending order.

gs/gs_bean.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,13 @@ func (d *BeanDefinition) Name(name string) *BeanDefinition {
181181
}
182182

183183
// On Set the condition for a bean.
184-
func (d *BeanDefinition) On(cond cond.Condition) *BeanDefinition {
185-
d.cond = cond
184+
func (d *BeanDefinition) On(c cond.Condition) *BeanDefinition {
185+
if nil == d.cond {
186+
d.cond = c
187+
return d
188+
}
189+
190+
d.cond = cond.Group(cond.And, d.cond, c)
186191
return d
187192
}
188193

gs/gs_test.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,24 +2987,49 @@ func (tac *testAutoConfiguration) NewSubject() (*BeanDefinition, error) {
29872987
}
29882988

29892989
type testConfiguration struct {
2990-
Subject *testSubject `autowire:""`
2990+
Subject *testSubject `autowire:"?"`
29912991
}
29922992

29932993
func TestConfiguration(t *testing.T) {
2994-
c := New()
2995-
p := conf.New()
2996-
p.Set("prefix", "hello")
2997-
p.Set("open", "true")
29982994

2999-
err := c.Properties().Refresh(p)
3000-
assert.Nil(t, err)
2995+
t.Run("test Configuration without conditional", func(t *testing.T) {
2996+
c := New()
2997+
p := conf.New()
2998+
p.Set("prefix", "hello")
2999+
p.Set("open", "true")
30013000

3002-
c.Configuration(new(testAutoConfiguration))
3003-
bd := c.Object(new(testConfiguration))
3001+
err := c.Properties().Refresh(p)
3002+
assert.Nil(t, err)
30043003

3005-
err = c.Refresh()
3006-
assert.Nil(t, err)
3004+
c.Configuration(new(testAutoConfiguration))
3005+
bd := c.Object(new(testConfiguration))
3006+
3007+
err = c.Refresh()
3008+
assert.Nil(t, err)
3009+
3010+
subject := bd.Interface().(*testConfiguration)
3011+
assert.Equal(t, subject.Subject.Bar.foo.prefix, "hello")
3012+
})
3013+
3014+
t.Run("test Configuration with conditional", func(t *testing.T) {
3015+
3016+
c := New()
3017+
p := conf.New()
3018+
p.Set("prefix", "hello")
3019+
p.Set("open", "true")
3020+
p.Set("enable", "false")
3021+
3022+
err := c.Properties().Refresh(p)
3023+
assert.Nil(t, err)
3024+
3025+
c.Configuration(new(testAutoConfiguration))
3026+
bd := c.Object(new(testConfiguration)).On(cond.OnProperty("enable", cond.HavingValue("true")))
3027+
3028+
err = c.Refresh()
3029+
assert.Nil(t, err)
3030+
3031+
subject := bd.Interface().(*testConfiguration)
3032+
assert.Nil(t, subject.Subject)
3033+
})
30073034

3008-
subject := bd.Interface().(*testConfiguration)
3009-
assert.Equal(t, subject.Subject.Bar.foo.prefix, "hello")
30103035
}

0 commit comments

Comments
 (0)