github.com/sandwich-go/boost@v1.3.29/ratelimiter/gen_options_optiongen.go (about)

     1  // Code generated by optiongen. DO NOT EDIT.
     2  // optiongen: github.com/timestee/optiongen
     3  
     4  package ratelimiter
     5  
     6  import "time"
     7  
     8  // Options should use NewOptions to initialize it
     9  type Options struct {
    10  	per   time.Duration
    11  	Slack int
    12  }
    13  
    14  // NewOptions new Options
    15  func NewOptions(opts ...Option) *Options {
    16  	cc := newDefaultOptions()
    17  	for _, opt := range opts {
    18  		opt(cc)
    19  	}
    20  	if watchDogOptions != nil {
    21  		watchDogOptions(cc)
    22  	}
    23  	return cc
    24  }
    25  
    26  // ApplyOption apply multiple new option
    27  func (cc *Options) ApplyOption(opts ...Option) {
    28  	for _, opt := range opts {
    29  		opt(cc)
    30  	}
    31  }
    32  
    33  // Option option func
    34  type Option func(cc *Options)
    35  
    36  // WithPer option func for filed per
    37  func WithPer(v time.Duration) Option {
    38  	return func(cc *Options) {
    39  		cc.per = v
    40  	}
    41  }
    42  
    43  // WithSlack option func for filed Slack
    44  func WithSlack(v int) Option {
    45  	return func(cc *Options) {
    46  		cc.Slack = v
    47  	}
    48  }
    49  
    50  // InstallOptionsWatchDog the installed func will called when NewOptions  called
    51  func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
    52  
    53  // watchDogOptions global watch dog
    54  var watchDogOptions func(cc *Options)
    55  
    56  // newDefaultOptions new default Options
    57  func newDefaultOptions() *Options {
    58  	cc := &Options{}
    59  
    60  	for _, opt := range [...]Option{
    61  		WithPer(time.Second),
    62  		WithSlack(10),
    63  	} {
    64  		opt(cc)
    65  	}
    66  
    67  	return cc
    68  }
    69  
    70  // all getter func
    71  func (cc *Options) GetPer() time.Duration { return cc.per }
    72  func (cc *Options) GetSlack() int         { return cc.Slack }
    73  
    74  // OptionsVisitor visitor interface for Options
    75  type OptionsVisitor interface {
    76  	GetPer() time.Duration
    77  	GetSlack() int
    78  }
    79  
    80  // OptionsInterface visitor + ApplyOption interface for Options
    81  type OptionsInterface interface {
    82  	OptionsVisitor
    83  	ApplyOption(...Option)
    84  }