fix: skip variadic params in constructors

We use them for "options". Ideally we'd be able to forward options
through, but that would require real dependency injection.
This commit is contained in:
Steven Allen 2021-09-26 11:51:19 +01:00
parent 621eafcecd
commit c07f74fd44
2 changed files with 20 additions and 1 deletions

View File

@ -81,7 +81,11 @@ func callConstructor(c reflect.Value, args []reflect.Value) (interface{}, error)
type constructor func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) interface{}
func makeArgumentConstructors(fnType reflect.Type, argTypes map[reflect.Type]constructor) ([]constructor, error) {
out := make([]constructor, fnType.NumIn())
params := fnType.NumIn()
if fnType.IsVariadic() {
params--
}
out := make([]constructor, params)
for i := range out {
argType := fnType.In(i)
c, ok := argTypes[argType]

15
config/transport_test.go Normal file
View File

@ -0,0 +1,15 @@
package config
import (
"testing"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
)
func TestTransportVariadicOptions(t *testing.T) {
_, err := TransportConstructor(func(_ peer.ID, _ ...int) transport.Transport { return nil })
if err != nil {
t.Fatal(err)
}
}