Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP provide params to invoke #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (c *Container) Provide(constructor interface{}, opts ...ProvideOption) erro
//
// The function may return an error to indicate failure. The error will be
// returned to the caller as-is.
func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error {
func (c *Container) Invoke(function interface{}, providedParams ...interface{}) error {
ftype := reflect.TypeOf(function)
if ftype == nil {
return errors.New("can't invoke an untyped nil")
Expand All @@ -485,7 +485,7 @@ func (c *Container) Invoke(function interface{}, opts ...InvokeOption) error {
return fmt.Errorf("can't invoke non-function %v (type %v)", function, ftype)
}

pl, err := newParamList(ftype)
pl, err := newParamList(ftype, providedParams...)
if err != nil {
return err
}
Expand Down
45 changes: 39 additions & 6 deletions param.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type param interface {
}

var (
_ param = paramProvided{}
_ param = paramSingle{}
_ param = paramObject{}
_ param = paramList{}
Expand Down Expand Up @@ -120,7 +121,7 @@ func walkParam(p param, v paramVisitor) {
}

switch par := p.(type) {
case paramSingle, paramGroupedSlice:
case paramSingle, paramGroupedSlice, paramProvided:
// No sub-results
case paramObject:
for _, f := range par.Fields {
Expand Down Expand Up @@ -161,7 +162,7 @@ func (pl paramList) DotParam() []*dot.Param {
//
// Variadic arguments of a constructor are ignored and not included as
// dependencies.
func newParamList(ctype reflect.Type) (paramList, error) {
func newParamList(ctype reflect.Type, providedParams ...interface{}) (paramList, error) {
numArgs := ctype.NumIn()
if ctype.IsVariadic() {
// NOTE: If the function is variadic, we skip the last argument
Expand All @@ -175,11 +176,17 @@ func newParamList(ctype reflect.Type) (paramList, error) {
}

for i := 0; i < numArgs; i++ {
p, err := newParam(ctype.In(i))
if err != nil {
return pl, errWrapf(err, "bad argument %d", i+1)
if i < len(providedParams) {
pl.Params = append(pl.Params, paramProvided{Param: providedParams[i]})

} else {
p, err := newParam(ctype.In(i))
if err != nil {
return pl, errWrapf(err, "bad argument %d", i+1)
}

pl.Params = append(pl.Params, p)
}
pl.Params = append(pl.Params, p)
}

return pl, nil
Expand All @@ -203,6 +210,7 @@ func (pl paramList) BuildList(c containerStore) ([]reflect.Value, error) {
return nil, err
}
}

return args, nil
}

Expand Down Expand Up @@ -452,3 +460,28 @@ func (pt paramGroupedSlice) Build(c containerStore) (reflect.Value, error) {
}
return result, nil
}

// paramProvided is an passed in param.
type paramProvided struct {
Name string
Optional bool
Type reflect.Type
Param interface{}
}

func (pp paramProvided) DotParam() []*dot.Param {
return []*dot.Param{
{
Node: &dot.Node{
Type: pp.Type,
Name: pp.Name,
},
Optional: pp.Optional,
},
}
}

func (pp paramProvided) Build(c containerStore) (reflect.Value, error) {
return reflect.ValueOf(pp.Param), nil
}

4 changes: 4 additions & 0 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@ func (pt paramGroupedSlice) String() string {
// io.Reader[group="foo"] refers to a group of io.Readers called 'foo'
return fmt.Sprintf("%v[group=%q]", pt.Type.Elem(), pt.Group)
}

func (pp paramProvided) String() string {
return fmt.Sprintf("%v[%v]", pp.Type, pp.Param)
}