Skip to content

Commit

Permalink
Update xdi
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Nov 6, 2023
1 parent ac5a9da commit 4a38df1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
27 changes: 22 additions & 5 deletions src/xdi/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xdi

import (
"errors"
"fmt"
"reflect"
"sync"
Expand Down Expand Up @@ -33,7 +34,7 @@ type Container struct {
func (t *Container) Provide(objects ...*Object) error {
for _, o := range objects {
if _, ok := t.tidyObjects.Load(o.Name); ok {
return fmt.Errorf("error: object '%s' existing", o.Name)
return fmt.Errorf("xdi: object '%s' existing", o.Name)
}
t.tidyObjects.Store(o.Name, o)
}
Expand All @@ -43,20 +44,36 @@ func (t *Container) Provide(objects ...*Object) error {
func (t *Container) Object(name string) (*Object, error) {
v, ok := t.tidyObjects.Load(name)
if !ok {
return nil, fmt.Errorf("error: object '%s' not found", name)
return nil, fmt.Errorf("xdi: object '%s' not found", name)
}
obj := v.(*Object)
return obj, nil
}

func (t *Container) Populate(name string, ptr interface{}) error {
func (t *Container) Populate(name string, ptr interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(fmt.Sprint(e))
}
}()

obj, err := t.Object(name)
if err != nil {
return err
}
ptrCopy := func(to, from interface{}) {
reflect.ValueOf(to).Elem().Set(reflect.ValueOf(from))

if reflect.ValueOf(ptr).Kind() != reflect.Ptr {
return errors.New("xdi: argument can only be pointer type")
}

ptrCopy := func(ptr, newValue interface{}) {
v := reflect.ValueOf(ptr)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
v.Set(reflect.ValueOf(newValue))
}

if !obj.NewEverytime {
refresher := &obj.refresher
if p, ok := t.instances.Load(name); ok && !refresher.status() {
Expand Down
21 changes: 19 additions & 2 deletions src/xdi/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,33 @@ func TestPopulate(t *testing.T) {
}
_ = c.Provide(objs...)

// 测试单例
var f1 *foo
_ = c.Populate("foo", &f1)
var f2 *foo
_ = c.Populate("foo", &f2)
a.Equal(fmt.Sprintf("%p", f1), fmt.Sprintf("%p", f2))

// 错误使用测试
var f3 foo
err := c.Populate("foo", f3) // 非指针
a.Contains(err.Error(), "can only be pointer type")
var f4 foo
err = c.Populate("foo", &f4) // New函数返回的指针,但是f3为引用 [panic: reflect.Set: value of type *xdi.foo is not assignable to type xdi.foo]
a.Contains(err.Error(), "is not assignable to")

// 测试嵌套依赖
var f *foo
_ = c.Populate("foo", &f)
text := fmt.Sprintf("%#v \n", f.Client)
a.Contains(text, "Timeout:10000000000")

// 测试多次失败场景
var i interface{}
err := c.Populate("bar", &i)
err = c.Populate("bar", &i)
a.Equal(err, errors.New("error"))
err = c.Populate("bar", &i)
a.Equal(err, errors.New("error")) // 测试多次失败场景
a.Equal(err, errors.New("error"))
}

func TestSingletonConcurrency(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/xdi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Object struct {

func (t *Object) Refresh() error {
if t.NewEverytime {
return fmt.Errorf("error: '%s' is NewEverytime, unable to refresh", t.Name)
return fmt.Errorf("xdi: '%s' is NewEverytime, unable to refresh", t.Name)
}
t.refresher.on()
return nil
Expand Down

0 comments on commit 4a38df1

Please sign in to comment.