Skip to content

Commit

Permalink
NoFill methods
Browse files Browse the repository at this point in the history
N/A
  • Loading branch information
teou committed Apr 13, 2017
1 parent f2fe371 commit 3982cd5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
48 changes: 44 additions & 4 deletions inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ func (g *Graph) setboth(name string, o *Object) {
}
}

func (g *Graph) RegisterOrFailNoFill(name string, value interface{}) interface{} {
v, err := g.RegisterNoFill(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(err.Error())
}
return v
}

func (g *Graph) RegisterOrFailSingleNoFill(name string, value interface{}) interface{} {
v, err := g.RegisterSingleNoFill(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(err.Error())
}
return v
}

func (g *Graph) RegisterOrFail(name string, value interface{}) interface{} {
v, err := g.Register(name, value)
if err != nil {
Expand All @@ -159,19 +181,31 @@ func (g *Graph) RegisterOrFailSingle(name string, value interface{}) interface{}
return v
}

func (g *Graph) RegisterNoFill(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, false, true)
}

func (g *Graph) RegisterSingleNoFill(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, true, true)
}

func (g *Graph) Register(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, false)
return g.register(name, value, false, false)
}

func (g *Graph) RegisterSingle(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, true)
return g.register(name, value, true, false)
}

func (g *Graph) register(name string, value interface{}, singleton bool) (interface{}, error) {
func (g *Graph) register(name string, value interface{}, singleton bool, noFill bool) (interface{}, error) {
reflectType := reflect.TypeOf(value)

if isStructPtr(reflectType) {
Expand All @@ -197,13 +231,19 @@ func (g *Graph) register(name string, value interface{}, singleton bool) (interf
if isStructPtr(o.reflectType) {
t := reflectType.Elem()
var v reflect.Value
created := false
if isNil(value) {
created = true
v = reflect.New(t)
} else {
v = reflect.ValueOf(value)
}

for i := 0; i < t.NumField(); i++ {
if !created && noFill {
continue
}

f := t.Field(i)
vfe := v.Elem()
vf := vfe.Field(i)
Expand Down Expand Up @@ -255,7 +295,7 @@ func (g *Graph) register(name string, value interface{}, singleton bool) (interf
continue
}
if isStructPtr(f.Type) {
_, err := g.register(tag, reflect.NewAt(f.Type.Elem(), nil).Interface(), singletonTag)
_, err := g.register(tag, reflect.NewAt(f.Type.Elem(), nil).Interface(), singletonTag, noFill)
if err != nil {
return nil, err
}
Expand Down
37 changes: 37 additions & 0 deletions inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,40 @@ func TestIntAssign2(t *testing.T) {
}

}

type Rec struct {
HaHa string `inject:"haha"`
}

type Rec1 struct {
Rec *Rec `inject:"rec"`
Rec2 *Rec2 `inject:"rec2"`
}

type Rec2 struct {
Rec *Rec `inject:"rec"`
Rec1 *Rec1 `inject:"rec1"`
}

//causing a stack overflow error
func _TestRec(t *testing.T) {
fmt.Println("############## test rec")
InitDefault()
l := &Log{}
SetLogger(l)
defer Close()

defer func() {
if x := recover(); x != nil {
fmt.Println("panic : ", x)
}
}()
haha := "aa"
RegisterOrFail("haha", haha)

//this will cause:
//runtime: goroutine stack exceeds 1000000000-byte limit
//fatal error: stack overflow
RegisterOrFail("rec1", (*Rec1)(nil))
RegisterOrFail("rec2", (*Rec2)(nil))
}
8 changes: 8 additions & 0 deletions inji.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func SetLogger(logger Logger) {
_g.Logger = logger
}

func RegisterOrFailNoFill(name string, value interface{}) interface{} {
return _g.RegisterOrFailNoFill(name, value)
}

func RegisterOrFail(name string, value interface{}) interface{} {
return _g.RegisterOrFail(name, value)
}
Expand All @@ -24,6 +28,10 @@ func Register(name string, value interface{}) (interface{}, error) {
return _g.Register(name, value)
}

func RegisterOrFailSingleNoFill(name string, value interface{}) interface{} {
return _g.RegisterOrFailSingleNoFill(name, value)
}

func RegisterOrFailSingle(name string, value interface{}) interface{} {
return _g.RegisterOrFailSingle(name, value)
}
Expand Down

0 comments on commit 3982cd5

Please sign in to comment.