Skip to content

Commit

Permalink
modify getByInterface func
Browse files Browse the repository at this point in the history
  • Loading branch information
junqiang.zhang committed May 15, 2022
1 parent c3718b8 commit 35693c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
36 changes: 25 additions & 11 deletions exmaple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type AA struct {
B *BB
A *string // `inject:"name:hello"`
*CC
}

type BB struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

}
18 changes: 9 additions & 9 deletions gdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand 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) {
Expand Down

0 comments on commit 35693c8

Please sign in to comment.