Browse Source

Merge pull request #1204 from libp2p/fix/allow-variadic

fix: skip variadic params in constructors
pull/1203/head
Steven Allen 3 years ago
committed by GitHub
parent
commit
f15c2a8d95
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      config/reflection_magic.go
  2. 15
      config/transport_test.go

6
config/reflection_magic.go

@ -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

@ -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)
}
}
Loading…
Cancel
Save