Skip to content

Commit

Permalink
refactor the transport constructor code to remove TransportWithOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Nov 8, 2022
1 parent a647731 commit 0d66420
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package libp2p
// those are in defaults.go).

import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"reflect"
"time"

"github.com/libp2p/go-libp2p/config"
Expand Down Expand Up @@ -124,23 +127,42 @@ func Muxer(name string, muxer network.Multiplexer) Option {
// * Public Key
// * Address filter (filter.Filter)
// * Peerstore
func Transport[F any](tpt F) Option {
func Transport(constructor interface{}, opts ...interface{}) Option {
return func(cfg *Config) error {
// generate a random identifier, so that fx can associate the constructor with its options
b := make([]byte, 8)
rand.Read(b)
id := binary.BigEndian.Uint64(b)

tag := fmt.Sprintf(`group:"transportopt_%d"`, id)

typ := reflect.ValueOf(constructor).Type()
numParams := typ.NumIn()
isVariadic := typ.IsVariadic()
var params []string
if isVariadic && len(opts) > 0 {
// If there are transport options, apply the tag.
// Since options are variadic, they have to be the last argument of the constructor.
params = make([]string, numParams)
params[len(params)-1] = tag
}

cfg.Transports = append(cfg.Transports, fx.Provide(
fx.Annotate(
tpt,
constructor,
fx.ParamTags(params...),
fx.As(new(transport.Transport)),
fx.ResultTags(`group:"transport"`),
),
))
return nil
}
}

func TransportWithOptions[F any, Opt any](tpt F, opts ...Opt) Option {
return func(cfg *Config) error {
cfg.Transports = append(cfg.Transports, fx.Provide(fx.Annotate(tpt, fx.ResultTags(`group:"transport"`))))
cfg.Transports = append(cfg.Transports, fx.Supply(opts))
for _, opt := range opts {
cfg.Transports = append(cfg.Transports, fx.Supply(
fx.Annotate(
opt,
fx.ResultTags(tag),
),
))
}
return nil
}
}
Expand Down

0 comments on commit 0d66420

Please sign in to comment.