From 2e614f017d4a96dfab8828000ad0045e9cc33139 Mon Sep 17 00:00:00 2001 From: limpo1989 Date: Tue, 5 Dec 2023 18:25:26 +0800 Subject: [PATCH] Fix method bean parent resolve incorrect --- gs/arg/arg.go | 17 +++++++++++++++++ gs/gs.go | 5 +---- gs/gs_test.go | 10 +++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/gs/arg/arg.go b/gs/arg/arg.go index 1eca2f74..cd95a638 100644 --- a/gs/arg/arg.go +++ b/gs/arg/arg.go @@ -362,6 +362,23 @@ func (r *Callable) In(i int) (reflect.Type, bool) { return r.fnType.In(i), true } +func (r *Callable) Parent() (selector interface{}, ok bool) { + // check receiver + if selector, ok = r.In(0); !ok || !utils.IsBeanType(selector.(reflect.Type)) { + return nil, false + } + + // bean selector + if arg, exists := r.Arg(0); exists { + switch arg.(type) { + case utils.BeanDefinition: + return arg, true + } + } + + return +} + // Call invokes the function with its binding arguments processed in the IoC // container. If the function returns an error, then the Call returns it. func (r *Callable) Call(ctx Context) ([]reflect.Value, error) { diff --git a/gs/gs.go b/gs/gs.go index 37b3fce7..9bb98c4f 100644 --- a/gs/gs.go +++ b/gs/gs.go @@ -487,10 +487,7 @@ func (c *container) resolveBean(b *BeanDefinition) error { // method bean 先确定 parent bean 是否存在 if b.method { - selector, ok := b.f.Arg(0) - if !ok || selector == "" { - selector, ok = b.f.In(0) - } + selector, ok := b.f.Parent() if ok { parents, err := c.findBean(selector) if err != nil { diff --git a/gs/gs_test.go b/gs/gs_test.go index bd05533a..e069c3df 100644 --- a/gs/gs_test.go +++ b/gs/gs_test.go @@ -3048,6 +3048,7 @@ func TestLogLogger(t *testing.T) { type testFoo struct { prefix string + name string } type testBar struct { @@ -3070,12 +3071,12 @@ func (tac *testAutoConfiguration) NewEmpty() *BeanDefinition { return NewBean(new(struct{})).Name("empty") } -func (tac *testAutoConfiguration) newFoo() *testFoo { - return &testFoo{prefix: tac.Prefix} +func (tac *testAutoConfiguration) newFoo(name string) *testFoo { + return &testFoo{prefix: tac.Prefix, name: name} } func (tac *testAutoConfiguration) NewFoo() *BeanDefinition { - return NewBean(tac.newFoo) + return NewBean(tac.newFoo, "${name}") } func (tac *testAutoConfiguration) NewBar(foo *testFoo) (*testBar, error) { @@ -3097,6 +3098,7 @@ func TestConfiguration(t *testing.T) { p := conf.New() p.Set("prefix", "hello") p.Set("open", "true") + p.Set("name", "go-spring") err := c.Properties().Refresh(p) assert.Nil(t, err) @@ -3109,6 +3111,7 @@ func TestConfiguration(t *testing.T) { subject := bd.Interface().(*testConfiguration) assert.Equal(t, subject.Subject.Bar.foo.prefix, "hello") + assert.Equal(t, subject.Subject.Bar.foo.name, "go-spring") }) t.Run("test Configuration with conditional", func(t *testing.T) { @@ -3118,6 +3121,7 @@ func TestConfiguration(t *testing.T) { p.Set("prefix", "hello") p.Set("open", "true") p.Set("enable", "false") + p.Set("name", "go-spring") err := c.Properties().Refresh(p) assert.Nil(t, err)