github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/selector/config.go (about)

     1  package selector
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/volts-dev/volts/config"
     8  	"github.com/volts-dev/volts/logger"
     9  	"github.com/volts-dev/volts/registry"
    10  )
    11  
    12  type (
    13  	// SelectOption used when making a select call
    14  	SelectOption func(*SelectConfig)
    15  	SelectConfig struct {
    16  		Filters  []Filter
    17  		Strategy Strategy
    18  
    19  		// Other options for implementations of the interface
    20  		// can be stored in a context
    21  		Context context.Context
    22  	}
    23  
    24  	// OptionFn configures options of server.
    25  	Option func(*Config)
    26  	Config struct {
    27  		config.Config `field:"-"`
    28  		Name          string             `field:"-"` // config name/path in config file
    29  		PrefixName    string             `field:"-"` // config prefix name
    30  		Logger        logger.ILogger     `field:"-"` // 保留:提供给扩展使用
    31  		Registry      registry.IRegistry `field:"-"`
    32  		Strategy      Strategy           `field:"-"`
    33  		// Other options for implementations of the interface
    34  		// can be stored in a context
    35  		Context context.Context `field:"-"`
    36  	}
    37  )
    38  
    39  func newConfig(opts ...Option) *Config {
    40  	cfg := &Config{
    41  		Name:     "selector",
    42  		Logger:   log,
    43  		Strategy: Random,
    44  		Registry: registry.Default(),
    45  	}
    46  	cfg.Init(opts...)
    47  	config.Register(cfg)
    48  	return cfg
    49  }
    50  
    51  func (self *Config) String() string {
    52  	if len(self.PrefixName) > 0 {
    53  		return strings.Join([]string{self.PrefixName, self.Name}, ".")
    54  	}
    55  	return self.Name
    56  }
    57  
    58  func (self *Config) Init(opts ...Option) {
    59  	for _, opt := range opts {
    60  		if opt != nil {
    61  			opt(self)
    62  		}
    63  	}
    64  }
    65  
    66  func (self *Config) Load() error {
    67  	return self.LoadToModel(self)
    68  }
    69  
    70  func (self *Config) Save(immed ...bool) error {
    71  	return self.SaveFromModel(self, immed...)
    72  }
    73  
    74  func Debug() Option {
    75  	return func(cfg *Config) {
    76  		cfg.Debug = true
    77  	}
    78  }
    79  
    80  func Logger() logger.ILogger {
    81  	return log
    82  }
    83  
    84  // Registry sets the registry used by the selector
    85  func Registry(r registry.IRegistry) Option {
    86  	return func(cfg *Config) {
    87  		cfg.Registry = r
    88  	}
    89  }
    90  
    91  // WithFilter adds a filter function to the list of filters
    92  // used during the Select call.
    93  func WithFilter(fn ...Filter) SelectOption {
    94  	return func(cfg *SelectConfig) {
    95  		cfg.Filters = append(cfg.Filters, fn...)
    96  	}
    97  }
    98  
    99  func WithStrategy(name string) Option {
   100  	return func(cfg *Config) {
   101  		if fn := Use(name); fn != nil {
   102  			cfg.Strategy = Use(name)
   103  		}
   104  	}
   105  }
   106  
   107  // 修改Config.json的路径
   108  func WithConfigPrefixName(prefixName string) Option {
   109  	return func(cfg *Config) {
   110  		cfg.Unregister(cfg)
   111  		cfg.PrefixName = prefixName
   112  		cfg.Register(cfg)
   113  	}
   114  }