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

[BUG/FUTURE] add custom IsDestType in RegisterDependency #1842

Closed
RelicOfTesla opened this issue Feb 3, 2022 · 4 comments
Closed

[BUG/FUTURE] add custom IsDestType in RegisterDependency #1842

RelicOfTesla opened this issue Feb 3, 2022 · 4 comments

Comments

@RelicOfTesla
Copy link

RelicOfTesla commented Feb 3, 2022

	hasPanic := false
	app.ConfigureContainer(func(container *router.APIContainer) {
		container.EnableStrictMode(true)
		defer func() {
			if e := recover(); e != nil {
				hasPanic = true
			}
		}()
		type good_willInitPanic struct {
		}
		container.Handle(http.MethodGet, "/struct1", func(good_willInitPanic) string {
			return "1"
		})
	})
	require.True(t, hasPanic)

	called := 0
	hasPanic = false
	app.ConfigureContainer(func(container *router.APIContainer) {
		type bad_IWillInitPanic struct {
		}
		container.EnableStrictMode(true)
		container.RegisterDependency(&hero.Dependency{
			Handle: func(ctx *context.Context, input *hero.Input) (reflect.Value, error) {
				called++
				return reflect.Value{}, hero.ErrSeeOther
			},
			DestType: nil,
			/*
				// todo: your can add field IsDestType
				IsDestType : func(input *hero.Input) bool{
					return input.Type == reflect.TypeOf(bad_IWillInitPanic{})
					|| or other logic: example isEmbedsType(input.Type, xxx)
					|| or index check
					|| or replace hero matchDependency()
				},
			*/
		})
		defer func() {
			if e := recover(); e != nil {
				hasPanic = true
			}
		}()
		container.Handle(http.MethodGet, "/struct2", func(bad_IWillInitPanic) string {
			return "2"
		})
	})
	require.True(t, hasPanic) // not support

	go (func() {
		time.Sleep(time.Second * 1)
		//http.Get("http://127.0.0.1:8080/struct1")
		http.Get("http://127.0.0.1:8080/struct2") // bad function: only find error when request it.
	})()
	if err := app.Listen(":8080"); err != nil {
		fmt.Println(err)
	}
@RelicOfTesla RelicOfTesla changed the title [BUG] add custom IsDestType in RegisterDependency [BUG/FUTURE] add custom IsDestType in RegisterDependency Feb 3, 2022
kataras added a commit that referenced this issue Mar 1, 2022
…tcher and hero.Dependency.Match to fullfil the feature request asked at: #1842
@kataras
Copy link
Owner

kataras commented Mar 1, 2022

Hello @RelicOfTesla,

I've pushed a commit which does that and more. Now you have the option to change the default way of how "matchDependency" behaves with two ways:

  1. In API DI Container, so all dependencies share the same "matchDependency" using the container.SetDependencyMatcher(func(d *hero.Dependnecy, in reflect.Type) bool{...})
  2. At Dependency-level itself by &hero.Dependency { Match: func(in reflect.Type) bool {...}, ...}

Take a look and share your feedbad, don't hesitate to ask more!

@RelicOfTesla
Copy link
Author

RelicOfTesla commented Mar 2, 2022

Thanks.
In addition, it is recommended to use “hero.input” as the parameter of match, so that the Parameter sequence index bind may be controlled in the future.
Or it can carry “Route“ and more in “hero.input“ for more complex context control in the future.
Although I don't need such advanced functions for the time being. :)

@RelicOfTesla
Copy link
Author

[bug] old source code panic when RegisterDependency(&hero.Dependency{ /Match : nil/, ...... })

@kataras
Copy link
Owner

kataras commented Mar 12, 2022

Hello @RelicOfTesla, why do you manually construct a &hero.Dependency? I can't control the parameters there. You should just use RegisterDependency(customStructValueOrFunctionHere).

@kataras kataras closed this as completed Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants