Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gs/arg/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions gs/gs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions gs/gs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,7 @@ func TestLogLogger(t *testing.T) {

type testFoo struct {
prefix string
name string
}

type testBar struct {
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand Down