Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed May 21, 2020
1 parent 220e930 commit 1091c66
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Entity struct {
initializeFunc interface{} // initializeFunc is a func to initialize entity
value interface{}
typ reflect.Type
index int // the index in the container
index int // the index in the container
override bool // identify whether the entity can be override

prototype bool
c *containerImpl
Expand Down Expand Up @@ -256,6 +257,7 @@ func (c *containerImpl) bindValueOverride(key interface{}, value interface{}, ov
key: key,
typ: reflect.TypeOf(value),
value: value,
override: override,
index: len(c.objectSlices),
c: c,
prototype: false,
Expand All @@ -266,6 +268,10 @@ func (c *containerImpl) bindValueOverride(key interface{}, value interface{}, ov
return ErrRepeatedBind("key repeated")
}

if !original.override {
return ErrRepeatedBind("key repeated, override is not allowed for this key")
}

entity.index = original.index
c.objects[key] = &entity
c.objectSlices[original.index] = &entity
Expand Down Expand Up @@ -323,17 +329,18 @@ func (c *containerImpl) newEntityWrapper(initialize interface{}, prototype bool)
}

typ := initializeType.Out(0)
return c.newEntity(typ, typ, initialize, prototype), nil
return c.newEntity(typ, typ, initialize, prototype, true), nil
}

func (c *containerImpl) newEntity(key interface{}, typ reflect.Type, initialize interface{}, prototype bool) *Entity {
func (c *containerImpl) newEntity(key interface{}, typ reflect.Type, initialize interface{}, prototype bool, override bool) *Entity {
entity := Entity{
initializeFunc: initialize,
key: key,
typ: typ,
value: nil,
c: c,
prototype: prototype,
override: override,
}

return &entity
Expand Down Expand Up @@ -556,15 +563,19 @@ func (c *containerImpl) bindWithOverride(key interface{}, typ reflect.Type, init
return ErrRepeatedBind("key repeated")
}

entity := c.newEntity(key, typ, initialize, prototype)
if !original.override {
return ErrRepeatedBind("key repeated, override is not allowed for this key")
}

entity := c.newEntity(key, typ, initialize, prototype, override)
entity.index = original.index
c.objects[key] = entity
c.objectSlices[original.index] = entity

return nil
}

entity := c.newEntity(key, typ, initialize, prototype)
entity := c.newEntity(key, typ, initialize, prototype, override)
entity.index = len(c.objectSlices)

c.objects[key] = entity
Expand Down

0 comments on commit 1091c66

Please sign in to comment.