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

Add Invoke function to resolve dependency #23

Merged
merged 7 commits into from
Apr 27, 2017
Merged

Add Invoke function to resolve dependency #23

merged 7 commits into from
Apr 27, 2017

Conversation

anuptalwalkar
Copy link
Contributor

#21
Invoke function resolves the dependencies provided as arguments to the constructor, and invokes the function. The Invoke(func) can be used as a callback function with resolved dependencies from dig.

container.go Outdated
// occurred during the execution
func (c *Container) Invoke(t interface{}) error {
c.Lock()
defer c.Unlock()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if locking is needed here since we are not updating the container.

Copy link
Collaborator

@glibsm glibsm Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets move unlock to right before Call.
still need to lock for map read though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to RLock()

var c1 Child1
var c2 Child2

err = c.Invoke(func(p1 *Parent1, p2 *Parent12) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate p1 and p2

@@ -112,6 +145,8 @@ func (c *Container) Resolve(obj interface{}) (err error) {
objElemType := reflect.TypeOf(obj).Elem()
objVal := reflect.ValueOf(obj)

c.Lock()
defer c.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might also be good to just put the RLock around the map access, but not a big deal.

glibsm
glibsm previously approved these changes Apr 27, 2017
Copy link
Collaborator

@glibsm glibsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@glibsm glibsm dismissed their stale review April 27, 2017 01:15

realized something else

@glibsm
Copy link
Collaborator

glibsm commented Apr 27, 2017

I'm not sure that this fully satisfies the requirement we have for Invoke. Not only we want to be able to call, but also sometimes store the returned values into the graph (if we wanted to directly call a constructor).

That can probably be an option passed into Invoke, so this diff can be merged anyway, but lets talk through the scenario that I have in mind to make sure this is setting us on the correct path.

@anuptalwalkar
Copy link
Contributor Author

I added the functionality to register returned objects back to the graph. This satisfies the requirement of executing Invoke function and having the returned objects available for later Resolve. The Invoke will also support anonymous functions, if users don't wish to register any objects back to dig.

container.go Outdated
}
args[idx] = v
} else {
return fmt.Errorf("%v dependency of type %v is not registered", ctype, arg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should move this to c.get(type) so this is not duplicated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also do errors.Wrapf like the one above

return nil
}

func (c *Container) insertObjectToGraph(v reflect.Value, vtype reflect.Type) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no lock in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock is applied in Provide and Invoke around insertObject func. I am refactoring this to move locking out and away from the container to simplify the API.

@anuptalwalkar anuptalwalkar merged commit 7dcb2e5 into master Apr 27, 2017
@anuptalwalkar anuptalwalkar deleted the dig-call branch April 27, 2017 23:19
@glibsm
Copy link
Collaborator

glibsm commented Apr 27, 2017

forgot to mention, could you update changelog?

hbdf pushed a commit to hbdf/dig that referenced this pull request Jul 14, 2022
* Exclude nil providers from ProviderGroup

* Removed comment

* Capture oldProviders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants