go-emlid/reach/client/options.go

76 lines
1.7 KiB
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package client
import (
2024-08-02 12:57:07 +02:00
"log/slog"
"time"
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
2024-07-30 14:28:39 +02:00
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
v1 "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v1"
v2 "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v2"
)
type Options struct {
Protocols *protocol.Registry
PreferredProtocol protocol.Identifier
FallbackProtocol protocol.Identifier
2024-08-02 12:57:07 +02:00
AvailableTimeout time.Duration
Logger logger.Logger
2024-08-05 18:10:19 +02:00
Dial protocol.DialFunc
2024-07-30 14:28:39 +02:00
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
opts := &Options{
PreferredProtocol: v2.Identifier,
FallbackProtocol: v1.Identifier,
Protocols: protocol.DefaultRegistry(),
2024-08-02 12:57:07 +02:00
AvailableTimeout: 5 * time.Second,
Logger: slog.Default(),
2024-08-05 18:10:19 +02:00
Dial: protocol.DefaultDialFunc,
2024-07-30 14:28:39 +02:00
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithPreferredProtocol(identifier protocol.Identifier) OptionFunc {
return func(opts *Options) {
opts.PreferredProtocol = identifier
}
}
func WithFallbackProtocol(identifier protocol.Identifier) OptionFunc {
return func(opts *Options) {
opts.FallbackProtocol = identifier
}
}
func WithProtocols(protocols *protocol.Registry) OptionFunc {
return func(opts *Options) {
opts.Protocols = protocols
}
}
2024-08-02 12:57:07 +02:00
func WithLogger(logger logger.Logger) OptionFunc {
return func(opts *Options) {
opts.Logger = logger
}
}
func WithAvailableTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.AvailableTimeout = timeout
}
}
2024-08-05 18:10:19 +02:00
func WithDial(dial protocol.DialFunc) OptionFunc {
return func(opts *Options) {
opts.Dial = dial
}
}