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

Using di.WithName() breaks when having one entry without a di.Name() #16

Closed
fionera opened this issue May 21, 2020 · 7 comments · Fixed by #19
Closed

Using di.WithName() breaks when having one entry without a di.Name() #16

fionera opened this issue May 21, 2020 · 7 comments · Fixed by #19
Labels
bug Something isn't working

Comments

@fionera
Copy link

fionera commented May 21, 2020

When I provide the Container with multiple implementations of an Object and have one that doesnt have a Name the container doesnt know how to resolve it properly and returns an error when resolving a named entry

@erickskrauch
Copy link

WithName just applies an additional alias to the definition but keeps its original type. So there is no error.

@fionera
Copy link
Author

fionera commented May 22, 2020

Here is an example of what I mean. The error is not very clear too

package main

import (
	"fmt"

	"github.com/goava/di"
)

func main() {
	c, err := di.New(
		di.Provide(NewOne),
		di.Provide(NewTwo, di.WithName("two")),
		di.Provide(NewThree, di.WithName("three")),
	)
	if err != nil {
		panic(err)
	}

	var one Example
	err = c.Resolve(&one)
	if err != nil {
		panic(err)
	}
	fmt.Println(one)

	var two Example
	err = c.Resolve(&two, di.Name("two"))
	if err != nil {
		panic(err)
	}
	fmt.Println(two)

	var three Example
	err = c.Resolve(&three, di.Name("three"))
	if err != nil {
		panic(err)
	}
	fmt.Println(three)
}

type Example string

func NewOne() Example {
	return "one"
}

func NewTwo(one Example) Example {
	return "two" + one
}

type ThreeParameters struct {
	di.Inject

	Two Example `di:"two"`
}

func NewThree(parameters ThreeParameters) Example {
	return parameters.Two + "three"
}

@fionera
Copy link
Author

fionera commented May 22, 2020

The same also happens if you have one entry in the container with an di.As Option and one or more without it.

@d3fvxl
Copy link
Owner

d3fvxl commented May 29, 2020

@fionera Sorry for the delay. I think this is not a feature :) #19 will solve the problem.

@fionera
Copy link
Author

fionera commented May 29, 2020

No problem ^^ I actually like the idea of supporting having multiple named and unnamed ones. But of course it would have to resolve it properly :)

@d3fvxl
Copy link
Owner

d3fvxl commented May 29, 2020

@fionera You can experiment with this function to get the desired result:

  • plist - all providers for type
  • param - dependency identity
func findNamedProvider(plist *providerList, param parameter) (result provider, _ error) {
	for _, p := range plist.All() {
		if p.Name() == param.name {
			if result != nil {
				return nil, errHaveSeveralInstances{typ: param.typ}
			}
			result = p
		}
	}
	if param.name == "" && result == nil {
		return nil, errHaveSeveralInstances{typ: param.typ}
	}
	if result != nil {
		return result, nil
	}
	return nil, errParameterProviderNotFound{param: param}
}

@fionera
Copy link
Author

fionera commented May 29, 2020

Ah yeah I missinterpreted the test you wrote :) Looks awesome thx

@d3fvxl d3fvxl added the bug Something isn't working label May 29, 2020
@d3fvxl d3fvxl changed the title Using WithName breaks when having one entry without a Name Using di.WithName() breaks when having one entry without a di.Name() May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants