2015-12-09 19:23:16 +00:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-01-06 16:25:12 +00:00
|
|
|
|
2024-06-04 21:40:43 +01:00
|
|
|
"go-micro.dev/v5/logger"
|
|
|
|
"go-micro.dev/v5/registry"
|
2015-12-09 19:23:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Registry registry.Registry
|
2016-05-03 22:06:19 +01:00
|
|
|
Strategy Strategy
|
2015-12-31 18:14:40 +00:00
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2022-09-29 16:44:53 +02:00
|
|
|
// Logger is the underline logger
|
|
|
|
Logger logger.Logger
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SelectOptions struct {
|
2015-12-31 18:14:40 +00:00
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
2023-04-26 00:16:34 +00:00
|
|
|
Context context.Context
|
|
|
|
Strategy Strategy
|
|
|
|
|
|
|
|
Filters []Filter
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// SelectOption used when making a select call.
|
2015-12-09 19:23:16 +00:00
|
|
|
type SelectOption func(*SelectOptions)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Registry sets the registry used by the selector.
|
2015-12-09 19:23:16 +00:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// SetStrategy sets the default strategy for the selector.
|
2016-05-03 22:06:19 +01:00
|
|
|
func SetStrategy(fn Strategy) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Strategy = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-23 20:15:01 +01:00
|
|
|
// WithFilter adds a filter function to the list of filters
|
2015-12-09 19:23:16 +00:00
|
|
|
// used during the Select call.
|
2016-04-23 21:37:26 +01:00
|
|
|
func WithFilter(fn ...Filter) SelectOption {
|
2015-12-09 19:23:16 +00:00
|
|
|
return func(o *SelectOptions) {
|
2016-04-23 21:37:26 +01:00
|
|
|
o.Filters = append(o.Filters, fn...)
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 22:06:19 +01:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Strategy sets the selector strategy.
|
2016-05-03 22:06:19 +01:00
|
|
|
func WithStrategy(fn Strategy) SelectOption {
|
|
|
|
return func(o *SelectOptions) {
|
|
|
|
o.Strategy = fn
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 16:44:53 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithLogger sets the underline logger.
|
2022-09-29 16:44:53 +02:00
|
|
|
func WithLogger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|