From 35693c8eeffd9af7cc071c411c8b9532788bbab2 Mon Sep 17 00:00:00 2001 From: "junqiang.zhang" Date: Sun, 15 May 2022 22:12:32 +0800 Subject: [PATCH] modify getByInterface func --- exmaple/main.go | 36 +++++++++++++++++++++++++----------- gdi.go | 18 +++++++++--------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/exmaple/main.go b/exmaple/main.go index d000bbc..3a0f4ba 100644 --- a/exmaple/main.go +++ b/exmaple/main.go @@ -9,6 +9,7 @@ import ( type AA struct { B *BB A *string // `inject:"name:hello"` + *CC } type BB struct { @@ -28,9 +29,15 @@ type DD struct { E *EE } +type cc struct { + name string + age *int +} + type EE struct { A *AA //`inject:"name:a" json:"a"` - *FF + *FF + *cc } type FF struct { @@ -72,14 +79,18 @@ func (d *DD) Add(a, b int) int { //注意:当有多个实现时,存在不确 } -func (d *CC) Add(a, b int) int { //注意:当有多个实现时,存在不确定因索 - - return a - b - -} +//func (d *CC) Add(a, b int) int { //注意:当有多个实现时,存在不确定因索 +// +// return a - b +// +//} func init() { + //gdi.Register(&CC{ + // Name: "jq", + //}) + //gdi.Register( // &AA{}, // &BB{}, @@ -133,9 +144,13 @@ func init() { func main() { - gdi.Register(func()(*II,string) { - return &II{},"ii" + //gdi.Register(func()(*II,string) { + // return &II{},"ii" + // + //}) + gdi.Register(&cc{ + name: "xxx", }) gdi.Init() @@ -163,8 +178,7 @@ func main() { fmt.Println(a.B.D.E.A.B.C.Name) fmt.Println(a.B.D.E.T.Hl) fmt.Println(*a.B.D.E.Hello=="") - ////tl.Typelinks() - //fmt.Println(*a.A) - //gdi.Register() + fmt.Println(a.CC.Name) + fmt.Println(a.B.e.name) } diff --git a/gdi.go b/gdi.go index 1bda7d5..5279cfc 100644 --- a/gdi.go +++ b/gdi.go @@ -211,11 +211,11 @@ func (gdi *GDIPool) build(v reflect.Value) { } } if field.Kind() == reflect.Interface { - if im, ok := gdi.getByInterface(field.Type()); ok { + if im, err := gdi.getByInterface(field.Type()); err==nil { field.Set(im) continue } else { - panic("not Implements") + gdi.panic(err.Error()) } } if im, ok := gdi.get(field.Type()); ok { @@ -374,7 +374,7 @@ func (gdi *GDIPool) get(t reflect.Type) (result reflect.Value, ok bool) { return } -func (gdi *GDIPool) getByInterface(i reflect.Type) (value reflect.Value, ok bool) { +func (gdi *GDIPool) getByInterface(i reflect.Type) (value reflect.Value, err error) { cnt := 0 var values []reflect.Value for t, v := range gdi.all() { @@ -385,17 +385,17 @@ func (gdi *GDIPool) getByInterface(i reflect.Type) (value reflect.Value, ok bool } } if cnt == 1 { - return value, true + return value, nil } if cnt > 1 { var msgs []string - for _,v:=range values { - msgs = append(msgs, fmt.Sprintf("%v",v.Type())) + for _, v := range values { + msgs = append(msgs, fmt.Sprintf("%v", v.Type())) } - gdi.panic(fmt.Sprintf("there is one more object impliment %v interface [%v].", i.Name(),strings.Join(msgs,","))) - return reflect.Value{}, false + msg := fmt.Sprintf("there is one more object impliment %v interface [%v].", i.Name(), strings.Join(msgs, ",")) + return reflect.Value{}, fmt.Errorf(msg) } - return reflect.Value{}, false + return reflect.Value{}, fmt.Errorf("type:%v not found", i.Name()) } func (gdi *GDIPool) getByName(name string) (result reflect.Value, ok bool) {