github.com/mailru/activerecord@v1.12.2/pkg/activerecord/option.go (about)

     1  package activerecord
     2  
     3  type Option interface {
     4  	apply(*ActiveRecord)
     5  }
     6  
     7  type optionFunc func(*ActiveRecord)
     8  
     9  func (o optionFunc) apply(c *ActiveRecord) {
    10  	o(c)
    11  }
    12  
    13  func WithLogger(logger LoggerInterface) Option {
    14  	return optionFunc(func(a *ActiveRecord) {
    15  		a.logger = logger
    16  	})
    17  }
    18  
    19  func WithConfig(config ConfigInterface) Option {
    20  	return optionFunc(func(a *ActiveRecord) {
    21  		a.config = config
    22  	})
    23  }
    24  
    25  func WithConfigCacher(configCacher ConfigCacherInterface) Option {
    26  	return optionFunc(func(a *ActiveRecord) {
    27  		a.configCacher = configCacher
    28  	})
    29  }
    30  
    31  func WithMetrics(metric MetricInterface) Option {
    32  	return optionFunc(func(a *ActiveRecord) {
    33  		a.metric = metric
    34  	})
    35  }
    36  
    37  func WithConnectionPinger(pc ClusterCheckerInterface) Option {
    38  	return optionFunc(func(a *ActiveRecord) {
    39  		a.pinger = pc
    40  	})
    41  }
    42  
    43  type clusterOption interface {
    44  	apply(*Cluster)
    45  }
    46  
    47  type clusterOptionFunc func(*Cluster)
    48  
    49  func (o clusterOptionFunc) apply(c *Cluster) {
    50  	o(c)
    51  }
    52  
    53  func WithShard(masters []OptionInterface, replicas []OptionInterface) clusterOption {
    54  	return clusterOptionFunc(func(c *Cluster) {
    55  		newShard := Shard{}
    56  
    57  		for _, opt := range masters {
    58  			newShard.Masters = append(newShard.Masters, ShardInstance{
    59  				ParamsID: opt.GetConnectionID(),
    60  				Config:   ShardInstanceConfig{Addr: "static"},
    61  				Options:  opt,
    62  			})
    63  		}
    64  
    65  		c.Append(newShard)
    66  	})
    67  }