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
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/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
|
|
|
|
2020-06-25 15:40:23 +01:00
|
|
|
// Domain to lookup services from within the registry
|
|
|
|
Domain string
|
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SelectOptions struct {
|
2016-05-03 22:06:19 +01:00
|
|
|
Filters []Filter
|
|
|
|
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
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option used to initialise the selector
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// SelectOption used when making a select call
|
|
|
|
type SelectOption func(*SelectOptions)
|
|
|
|
|
|
|
|
// Registry sets the registry used by the selector
|
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:40:23 +01:00
|
|
|
// Domain sets the domain used by the selector
|
|
|
|
func Domain(d string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Domain = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:06:19 +01:00
|
|
|
// SetStrategy sets the default strategy for the selector
|
|
|
|
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
|
|
|
|
|
|
|
// Strategy sets the selector strategy
|
|
|
|
func WithStrategy(fn Strategy) SelectOption {
|
|
|
|
return func(o *SelectOptions) {
|
|
|
|
o.Strategy = fn
|
|
|
|
}
|
|
|
|
}
|