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

How can I InvokeNamed T to any #60

Closed
iyaozhen opened this issue Feb 23, 2024 · 7 comments
Closed

How can I InvokeNamed T to any #60

iyaozhen opened this issue Feb 23, 2024 · 7 comments
Milestone

Comments

@iyaozhen
Copy link

iyaozhen commented Feb 23, 2024

Demo(use v2):

func TestInvokeAny(t *testing.T) {
	do.ProvideNamed(nil, "test", func(i do.Injector) (int, error) {
		return 1, nil
	})

	int1, err := do.InvokeNamed[int](nil, "test")
	if err != nil {
		t.Errorf("invoke error: %v", err)
		return
	}
	if int1 != 1 {
		t.Errorf("invoke response error: %v", int1)
		return
	}
	int2, err := do.InvokeNamed[any](nil, "test")
	if err != nil {
		t.Errorf("invoke error: %v", err)
		return
	}
	t.Log(int2)
}

do.InvokeNamed[any](nil, "test") not work

image

Why do I have to do this?

I want to generic call my local service, I just known service name

localService, err := do.InvokeNamed[any](nil, "ServiceName")
if err != nil {
return nil, err
}
methodValue := reflect.ValueOf(localService).MethodByName("FuncName")

invokeAnyByName Is what I want, can you Export It ?

thank you

@samber
Copy link
Owner

samber commented Feb 23, 2024

This is embarrassing. 🫣

Let me think about it.

@d-enk
Copy link

d-enk commented Apr 26, 2024

Аlso came across this

Seems to be solved by adding (Must)InvokeAsNamed to support not only any.

It just invokeAnyByName().(T), but with cast check in package

@samber
Copy link
Owner

samber commented May 5, 2024

I suppose do.InvokeNamed() should be able to skip the type check when the type is any ?

@samber samber added this to the v2.1 milestone May 5, 2024
@iyaozhen
Copy link
Author

iyaozhen commented May 6, 2024

I suppose do.InvokeNamed() should be able to skip the type check when the type is any ?

I think this is a good solution.

@samber
Copy link
Owner

samber commented May 16, 2024

@d-enk in your PR you said we might add the behavior to AsNamed. Can you tell us more about your though?

I don't see in what case we would declare an alias to any.

@d-enk
Copy link

d-enk commented May 16, 2024

I assumed. I just look at where invokeByName (type check) is used.

do/service_alias.go

Lines 78 to 79 in 1998a7a

instance, err := invokeByName[Initial](s.scope, s.targetName)
if err != nil {

No more reasons. Of course, it doesn't make sense until it's needed.

Thanks for the approval before 2.0

@samber samber modified the milestones: v2.1, v2 May 16, 2024
@samber
Copy link
Owner

samber commented May 16, 2024

@samber samber closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants