Skip to content

Commit

Permalink
Support constructors with variadic arguments
Browse files Browse the repository at this point in the history
This adds support for functions with a variadic number of arguments to
be Provided and Invoked. We will declare a dependency on a slice of the
variadic type when a variadic function is encountered.

With #120, this will be changed to an optional depedency so that
variadic constructors Just Work.
  • Loading branch information
abhinav committed Jul 10, 2017
1 parent d6bf8ca commit 505ee4d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- So that we can introduce new functionality after a 1.0 release, add a
variadic options parameter to all public APIs.
- Added support for functions with variadic arguments. These functions declare
a dependency on a slice of values of the variadic type.

## v1.0.0-rc1 (21 Jun 2017)

Expand Down
15 changes: 13 additions & 2 deletions dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,24 @@ func (c *Container) remove(nodes []*node) {
}

func (c *Container) constructorArgs(ctype reflect.Type) ([]reflect.Value, error) {
numArgs := ctype.NumIn()

args := make([]reflect.Value, 0, ctype.NumIn())
for i := 0; i < ctype.NumIn(); i++ {
for i := 0; i < numArgs; i++ {
arg, err := c.get(edge{key: key{t: ctype.In(i)}})
if err != nil {
return nil, fmt.Errorf("couldn't get arguments for constructor %v: %v", ctype, err)
}
args = append(args, arg)

// If the function is variadic, the last argument is a slice that must
// be expanded into the argument list.
if i == numArgs-1 && ctype.IsVariadic() {
for j := 0; j < arg.Len(); j++ {
args = append(args, arg.Index(j))
}
} else {
args = append(args, arg)
}
}
return args, nil
}
Expand Down
43 changes: 43 additions & 0 deletions dig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,49 @@ func TestEndToEndSuccess(t *testing.T) {
assert.Nil(t, p.Baz, "Baz must be unset")
}), "invoke failed")
})

t.Run("variadic arguments invoke", func(t *testing.T) {
c := New()

type A struct{ i int }

require.NoError(t, c.Provide(func() []*A {
return []*A{{1}, {2}, {3}}
}), "failed to provide A slice")

require.NoError(t, c.Invoke(func(as ...*A) {
require.Len(t, as, 3, "length must match")
assert.Equal(t, &A{1}, as[0], "as[0] must match")
assert.Equal(t, &A{2}, as[1], "as[1] must match")
assert.Equal(t, &A{3}, as[2], "as[2] must match")
}), "failed to invoke")
})

t.Run("variadic arguments dependency", func(t *testing.T) {
c := New()

type A struct{ i int }
type B struct{ i []int }

require.NoError(t, c.Provide(func() []*A {
return []*A{{1}, {2}, {3}}
}), "failed to provide A slice")

require.NoError(t, c.Provide(func(as ...*A) *B {
var b B
for _, a := range as {
b.i = append(b.i, a.i)
}
return &b
}), "failed to provide B")

require.NoError(t, c.Invoke(func(b *B) {
require.Len(t, b.i, 3, "length must match")
assert.Equal(t, 1, b.i[0], "b.i[0] must match")
assert.Equal(t, 2, b.i[1], "b.i[1] must match")
assert.Equal(t, 3, b.i[2], "b.i[2] must match")
}), "failed to invoke")
})
}

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

0 comments on commit 505ee4d

Please sign in to comment.