iris icon indicating copy to clipboard operation
iris copied to clipboard

[BUG] router.APIContainer.RegisterDependency only works with specific registration sequence

Open yz89122 opened this issue 4 years ago • 1 comments

Describe the bug router.APIContainer.RegisterDependency only works with specific registration sequence.

To Reproduce

  1. In main.go

    package main
    
    import (
        "github.com/kataras/iris/v12"
    )
    
    func main() {
        app := iris.Default()
        app.ConfigureContainer(func(api *iris.APIContainer) {
    	    api.RegisterDependency(func(f32 float32) float64 {
    		    return float64(f32)
    	    })
    	    api.RegisterDependency(float32(1))
        })
    }
    
  2. run with go run main.go

  3. panic

    [DBUG] 2021/07/22 21:57 Log level set to "debug"
    [DBUG] 2021/07/22 21:57 Using <./access.log> to log requests
    [DBUG] 2021/07/22 21:57 Using <UUID4> to identify requests
    panic: expected [1] bindings (input parameters) but got [0]
    Function:
      - main.main.func1.1
    Expected:
      - [1] float32
    Missing:
      - [1] float32
    
    goroutine 1 [running]:
    github.com/kataras/iris/v12/hero.getBindingsForFunc(0x18a8380, 0x1a247d0, 0x13, 0xc000293a40, 0xc, 0xc, 0x1b29b00, 0xffffffffffffffff, 0x19b9f60, 0x1892e00, ...)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/hero/binding.go:274 +0x785
    github.com/kataras/iris/v12/hero.fromDependentFunc(0x18a8380, 0x1a247d0, 0x13, 0xc000293b00, 0xc000293b00, 0xc000293a40, 0xc, 0xc, 0xc000293b00)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/hero/dependency.go:214 +0x112
    github.com/kataras/iris/v12/hero.resolveDependency(0x18a8380, 0x1a247d0, 0x13, 0xc0002d1600, 0xc000293b00, 0xc000293a40, 0xc, 0xc, 0x1)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/hero/dependency.go:91 +0xdf
    github.com/kataras/iris/v12/hero.newDependency(0x18a8380, 0x1a247d0, 0xc00029ee00, 0xc000293a40, 0xc, 0xc, 0x1823387)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/hero/dependency.go:77 +0x1dc
    github.com/kataras/iris/v12/hero.(*Container).Register(0xc0004adc70, 0x18a8380, 0x1a247d0, 0xc000291190)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/hero/container.go:284 +0x6a
    github.com/kataras/iris/v12/core/router.(*APIContainer).RegisterDependency(...)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/core/router/api_container.go:71
    main.main.func1(0xc000273338)
            /Users/yz/workspace/test/main.go:10 +0x47
    github.com/kataras/iris/v12/core/router.(*APIBuilder).ConfigureContainer(0xc000279e60, 0xc0004c3f70, 0x1, 0x1, 0xffffffff)
            /Users/yz/go/pkg/mod/github.com/kataras/iris/[email protected]/core/router/api_builder.go:335 +0x64
    main.main()
            /Users/yz/workspace/test/main.go:9 +0x66
    exit status 2
    

Expected behavior No panic like

package main

import (
	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.Default()
	app.ConfigureContainer(func(api *iris.APIContainer) {
		api.RegisterDependency(float32(1))
		api.RegisterDependency(func(f32 float32) float64 {
			return float64(f32)
		})
	})
}

Desktop (please complete the following information):

  • OS: macOS Bug Sur 11.4
  • go: 1.16

iris.Version

  • v12.2.0-alpha2

yz89122 avatar Jul 22 '21 14:07 yz89122

This behavior might make the API not easy to use since the user needs to register them in a specific sequence. Maybe we can make a API like container.Build() to build the dependency tree after all dependencies has been registered and before the app started.

yz89122 avatar Jul 22 '21 17:07 yz89122