Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ type Logger interface {
func Populate(values ...interface{}) error {
var g Graph
for _, v := range values {
if err := g.Provide(&Object{Value: v}); err != nil {
var o *Object
var ok bool

if o, ok = v.(*Object); !ok {
o = &Object{Value: v}
}

if err := g.Provide(o); err != nil {
return err
}
}

return g.Populate()
}

Expand Down
30 changes: 27 additions & 3 deletions inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ func TestInjectSimple(t *testing.T) {
}
}

func TestInjectMixed(t *testing.T) {
var v struct {
A1 *TypeAnswerStruct `inject:"one"`
A2 *TypeAnswerStruct `inject:"another"`
}

if err := inject.Populate(
&v,
&inject.Object{Value: &TypeAnswerStruct{answer: 1}, Name: "one"},
&inject.Object{Value: &TypeAnswerStruct{answer: 2}, Name: "another"}); err != nil {
t.Fatal(err)
}

if v.A1 == nil {
t.Fatal("v.A1 is nil")
}
if v.A2 == nil {
t.Fatal("v.A2 is nil")
}
if v.A1 == v.A2 {
t.Fatal("v.A1 equal to v.A2")
}
}

func TestDoesNotOverwrite(t *testing.T) {
a := &TypeAnswerStruct{}
var v struct {
Expand Down Expand Up @@ -852,8 +876,8 @@ func TestObjectString(t *testing.T) {

type TypeForGraphObjects struct {
TypeNestedStruct `inject:"inline"`
A *TypeNestedStruct `inject:"foo"`
E struct {
A *TypeNestedStruct `inject:"foo"`
E struct {
B *TypeNestedStruct `inject:""`
} `inject:"inline"`
}
Expand Down Expand Up @@ -916,7 +940,7 @@ type TypeForLoggingEmbedded struct {

type TypeForLogging struct {
TypeForLoggingEmbedded `inject:"inline"`
TypeForLoggingCreated *TypeForLoggingCreated `inject:""`
TypeForLoggingCreated *TypeForLoggingCreated `inject:""`
}

func TestInjectLogging(t *testing.T) {
Expand Down