Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
yourheropaul committed Aug 26, 2015
2 parents cc8b2f0 + b3eed5c commit 9c55516
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Graph struct {
Nodes nodeMap
UnmetDependencies int
Errors []string
indexes []reflect.Type
}

// Create a new instance of a graph with allocated memory
Expand Down
16 changes: 8 additions & 8 deletions graph_inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ func (g *Graph) Inject(fn interface{}, args ...interface{}) {
// Get an incoming arg reflection type
in := ftype.In(i)

// Find an entry in the graph
for typ, node := range g.Nodes {
if typ.AssignableTo(in) {
argv[i] = node.Value
return
}
}

// Check the additional args, if available
if len(xargs) > 0 {

Expand All @@ -65,6 +57,14 @@ func (g *Graph) Inject(fn interface{}, args ...interface{}) {
}
}

// Find an entry in the graph
for j := 0; j < len(g.indexes); j++ {
if g.indexes[j].AssignableTo(in) {
argv[i] = g.Nodes[g.indexes[j]].Value
return
}
}

// If it's STILL not found, panic
panic(fmt.Sprintf("[inj.Inject] Can't find value for arg %d [%s]", i, in))
}()
Expand Down
2 changes: 1 addition & 1 deletion graph_inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func Test_GraphComplexInjectionHappyPath2(t *testing.T) {

// Pass an anonymous function
g.Inject(func(s string) {
if s != "string one" {
if s != "string two" {
t.Fatalf("Expected 'string one', got %s", s)
}
}, "string two")
Expand Down
13 changes: 13 additions & 0 deletions graph_provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ func (g *Graph) Provide(inputs ...interface{}) error {
}
}

///////////////////////////////////////////////
// Plug everything together
///////////////////////////////////////////////

g.connect()

///////////////////////////////////////////////
// Store a list of types for speed later on
///////////////////////////////////////////////

g.indexes = make([]reflect.Type, 0)

for typ, _ := range g.Nodes {
g.indexes = append(g.indexes, typ)
}

return nil
}
22 changes: 21 additions & 1 deletion graph_provide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ func Test_ProvideHappyPath1(t *testing.T) {

// Check the whole type
assertConcreteValue(c, t)

// Check index count
if g, e := len(g.indexes), 6; g != e {
t.Errorf("Expected %d indexes, got %d", g, e)
}
}

// Multiple calls to Provide shouldh have the same effect as a single call
// Multiple calls to Provide should have the same effect as a single call
func Test_ProvideHappyPath2(t *testing.T) {

g, c := NewGraph(), ConcreteType{}
Expand Down Expand Up @@ -60,6 +65,11 @@ func Test_ProvideHappyPath2(t *testing.T) {

// Check the whole type
assertConcreteValue(c, t)

// Check index count
if g, e := len(g.indexes), 6; g != e {
t.Errorf("Expected %d indexes, got %d", g, e)
}
}

// New dependency provisions shouldn't overwrite previously set ones
Expand All @@ -74,6 +84,11 @@ func Test_ProvideOverride1(t *testing.T) {
t.Fatalf("Graph.Provide: %s", err)
}

// Check index count
if g, e := len(g.indexes), 2; g != e {
t.Errorf("[1] Expected %d indexes, got %d", g, e)
}

// The graph now includes DEFAULT_STRING as its
// only met dependency (missing dependencies covered
// by graph_assert_test.go). Adding another string
Expand All @@ -90,6 +105,11 @@ func Test_ProvideOverride1(t *testing.T) {
if g, e := c.String, DEFAULT_STRING; g != e {
t.Errorf("Got %s, expected %s", g, e)
}

// Check index count
if g, e := len(g.indexes), 2; g != e {
t.Errorf("[1] Expected %d indexes, got %d", g, e)
}
}

//////////////////////////////////////////
Expand Down

0 comments on commit 9c55516

Please sign in to comment.