Skip to content

Commit

Permalink
ignore interface{} inject
Browse files Browse the repository at this point in the history
  • Loading branch information
junqiang.zhang committed May 15, 2022
1 parent c89620c commit a54a92d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
74 changes: 44 additions & 30 deletions exmaple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"fmt"
"github.com/sjqzhang/gdi"

//"github.com/sjqzhang/gdi/tl"
)

type AA struct {
B *BB
A *string // `inject:"name:hello"`
*CC
a interface{} //`inject:"name:hello"`
}

type BB struct {
Expand Down Expand Up @@ -145,40 +147,52 @@ func init() {
func main() {

//gdi.Register(func()(*II,string) {
// return &II{},"ii"
// return &II{
// Home: "zh",
// },"hello"
//
//})

gdi.Register(&cc{
name: "xxx",
})
//
//gdi.Register(&cc{
// name: "xxx",
//})
//
//gdi.Init()
//
//type X struct {
// a *AA
// b *BB
//}
//var x X
//gdi.DI(&x)
//
//
//fmt.Println(*x.a.A)
//gdi.Debug(true) //显示注入信息,方便排错,需在gdi.Init()方法之前调用
//gdi.AutoCreate(true) //开启自动注入
//gdi.Init() //使用前必须先调用,当出现无解注入对象时会panic,避免运行时出现空指针
//var a *AA
//a = gdi.Get(a).(*AA) //说明,这里可以直接进行类型转换,不会出现空指针,当出现空指针时,gdi.Init()就会panic
//fmt.Println(a.B.C.Name)
//fmt.Println(a.B.D.C.Age)
//fmt.Println(a.B.D.Say("zhangsan"))
//
//fmt.Println(a.B.D.I.Add(2, 3))
//fmt.Println(a.B.D.E.A.B.D.E.A.B.D.E.A.B.C.Age)
//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=="")
//fmt.Println(a.CC.Name)
//fmt.Println(a.B.e.name)
//fmt.Println(a.a.(*II).Home)

gdi.Init()

type X struct {
a *AA
b *BB
}
var x X
gdi.DI(&x)


fmt.Println(*x.a.A)
gdi.Debug(true) //显示注入信息,方便排错,需在gdi.Init()方法之前调用
gdi.AutoCreate(true) //开启自动注入
gdi.Init() //使用前必须先调用,当出现无解注入对象时会panic,避免运行时出现空指针
var a *AA
a = gdi.Get(a).(*AA) //说明,这里可以直接进行类型转换,不会出现空指针,当出现空指针时,gdi.Init()就会panic
fmt.Println(a.B.C.Name)
fmt.Println(a.B.D.C.Age)
fmt.Println(a.B.D.Say("zhangsan"))

fmt.Println(a.B.D.I.Add(2, 3))
fmt.Println(a.B.D.E.A.B.D.E.A.B.D.E.A.B.C.Age)
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=="")
fmt.Println(a.CC.Name)
fmt.Println(a.B.e.name)
var a AA

gdi.DI(&a)


fmt.Println(a.B.D.C.Name)

}
9 changes: 7 additions & 2 deletions gdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ func (gdi *GDIPool) build(v reflect.Value) {
field.Set(im)
continue
} else {
gdi.panic(err.Error())
if field.Type().String()!="interface {}" {
gdi.panic(err.Error())
} else {
gdi.warn(fmt.Sprintf("\u001B[1;31mignore type:%v fieldName:%v of %v\u001B[0m",field.Type(),fieldName,v.Type()))
continue
}
}
}
if im, ok := gdi.get(field.Type()); ok {
Expand Down Expand Up @@ -259,7 +264,7 @@ func Debug(isDebug bool) {
func (gdi *GDIPool) DI(pointer interface{}) error {
defer func() {
if err := recover(); err != nil {
gdi.warn(err.(error).Error())
gdi.warn(fmt.Sprintf("%v",err))
}
}()
var result reflect.Value
Expand Down

0 comments on commit a54a92d

Please sign in to comment.