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

     1  // Code generated by optiongen. DO NOT EDIT.
     2  // optiongen: github.com/timestee/optiongen
     3  
     4  package annotation
     5  
     6  import (
     7  	"sync/atomic"
     8  	"unsafe"
     9  )
    10  
    11  // Options should use NewOptions to initialize it
    12  type Options struct {
    13  	MagicPrefix string       `xconf:"magic_prefix" usage:"只有包含 MagicPrefix 的行,才能萃取到注释"`
    14  	LowerKey    bool         `xconf:"lower_key" usage:"key是否为转化为小写"`
    15  	Descriptors []Descriptor `xconf:"descriptors" usage:"描述数组"`
    16  }
    17  
    18  // NewOptions new Options
    19  func NewOptions(opts ...Option) *Options {
    20  	cc := newDefaultOptions()
    21  	for _, opt := range opts {
    22  		opt(cc)
    23  	}
    24  	if watchDogOptions != nil {
    25  		watchDogOptions(cc)
    26  	}
    27  	return cc
    28  }
    29  
    30  // ApplyOption apply multiple new option and return the old ones
    31  // sample:
    32  // old := cc.ApplyOption(WithTimeout(time.Second))
    33  // defer cc.ApplyOption(old...)
    34  func (cc *Options) ApplyOption(opts ...Option) []Option {
    35  	var previous []Option
    36  	for _, opt := range opts {
    37  		previous = append(previous, opt(cc))
    38  	}
    39  	return previous
    40  }
    41  
    42  // Option option func
    43  type Option func(cc *Options) Option
    44  
    45  // WithMagicPrefix 只有包含 MagicPrefix 的行,才能萃取到注释
    46  func WithMagicPrefix(v string) Option {
    47  	return func(cc *Options) Option {
    48  		previous := cc.MagicPrefix
    49  		cc.MagicPrefix = v
    50  		return WithMagicPrefix(previous)
    51  	}
    52  }
    53  
    54  // WithLowerKey key是否为转化为小写
    55  func WithLowerKey(v bool) Option {
    56  	return func(cc *Options) Option {
    57  		previous := cc.LowerKey
    58  		cc.LowerKey = v
    59  		return WithLowerKey(previous)
    60  	}
    61  }
    62  
    63  // WithDescriptors 描述数组
    64  func WithDescriptors(v ...Descriptor) Option {
    65  	return func(cc *Options) Option {
    66  		previous := cc.Descriptors
    67  		cc.Descriptors = v
    68  		return WithDescriptors(previous...)
    69  	}
    70  }
    71  
    72  // InstallOptionsWatchDog the installed func will called when NewOptions  called
    73  func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
    74  
    75  // watchDogOptions global watch dog
    76  var watchDogOptions func(cc *Options)
    77  
    78  // newDefaultOptions new default Options
    79  func newDefaultOptions() *Options {
    80  	cc := &Options{}
    81  
    82  	for _, opt := range [...]Option{
    83  		WithMagicPrefix("annotation@"),
    84  		WithLowerKey(true),
    85  		WithDescriptors(nil...),
    86  	} {
    87  		opt(cc)
    88  	}
    89  
    90  	return cc
    91  }
    92  
    93  // AtomicSetFunc used for XConf
    94  func (cc *Options) AtomicSetFunc() func(interface{}) { return AtomicOptionsSet }
    95  
    96  // atomicOptions global *Options holder
    97  var atomicOptions unsafe.Pointer
    98  
    99  // onAtomicOptionsSet global call back when  AtomicOptionsSet called by XConf.
   100  // use OptionsInterface.ApplyOption to modify the updated cc
   101  // if passed in cc not valid, then return false, cc will not set to atomicOptions
   102  var onAtomicOptionsSet func(cc OptionsInterface) bool
   103  
   104  // InstallCallbackOnAtomicOptionsSet install callback
   105  func InstallCallbackOnAtomicOptionsSet(callback func(cc OptionsInterface) bool) {
   106  	onAtomicOptionsSet = callback
   107  }
   108  
   109  // AtomicOptionsSet atomic setter for *Options
   110  func AtomicOptionsSet(update interface{}) {
   111  	cc := update.(*Options)
   112  	if onAtomicOptionsSet != nil && !onAtomicOptionsSet(cc) {
   113  		return
   114  	}
   115  	atomic.StorePointer(&atomicOptions, (unsafe.Pointer)(cc))
   116  }
   117  
   118  // AtomicOptions return atomic *OptionsVisitor
   119  func AtomicOptions() OptionsVisitor {
   120  	current := (*Options)(atomic.LoadPointer(&atomicOptions))
   121  	if current == nil {
   122  		defaultOne := newDefaultOptions()
   123  		if watchDogOptions != nil {
   124  			watchDogOptions(defaultOne)
   125  		}
   126  		atomic.CompareAndSwapPointer(&atomicOptions, nil, (unsafe.Pointer)(defaultOne))
   127  		return (*Options)(atomic.LoadPointer(&atomicOptions))
   128  	}
   129  	return current
   130  }
   131  
   132  // all getter func
   133  func (cc *Options) GetMagicPrefix() string       { return cc.MagicPrefix }
   134  func (cc *Options) GetLowerKey() bool            { return cc.LowerKey }
   135  func (cc *Options) GetDescriptors() []Descriptor { return cc.Descriptors }
   136  
   137  // OptionsVisitor visitor interface for Options
   138  type OptionsVisitor interface {
   139  	GetMagicPrefix() string
   140  	GetLowerKey() bool
   141  	GetDescriptors() []Descriptor
   142  }
   143  
   144  // OptionsInterface visitor + ApplyOption interface for Options
   145  type OptionsInterface interface {
   146  	OptionsVisitor
   147  	ApplyOption(...Option) []Option
   148  }