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

     1  // Code generated by optiongen. DO NOT EDIT.
     2  // optiongen: github.com/timestee/optiongen
     3  
     4  package goformat
     5  
     6  import (
     7  	"sync/atomic"
     8  	"unsafe"
     9  )
    10  
    11  // Options should use NewOptions to initialize it
    12  type Options struct {
    13  	Fragment          bool `xconf:"fragment" usage:"允许解析源文件片段代码"`
    14  	AllErrors         bool `xconf:"all_errors" usage:"打印所有的语法错误到标准输出。如果不使用此标记,则只会打印不同行的前10个错误"`
    15  	RemoveBareReturns bool `xconf:"remove_bare_returns" usage:"移除无效的return"`
    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  // WithFragment 允许解析源文件片段代码
    46  func WithFragment(v bool) Option {
    47  	return func(cc *Options) Option {
    48  		previous := cc.Fragment
    49  		cc.Fragment = v
    50  		return WithFragment(previous)
    51  	}
    52  }
    53  
    54  // WithAllErrors 打印所有的语法错误到标准输出。如果不使用此标记,则只会打印不同行的前10个错误
    55  func WithAllErrors(v bool) Option {
    56  	return func(cc *Options) Option {
    57  		previous := cc.AllErrors
    58  		cc.AllErrors = v
    59  		return WithAllErrors(previous)
    60  	}
    61  }
    62  
    63  // WithRemoveBareReturns 移除无效的return
    64  func WithRemoveBareReturns(v bool) Option {
    65  	return func(cc *Options) Option {
    66  		previous := cc.RemoveBareReturns
    67  		cc.RemoveBareReturns = v
    68  		return WithRemoveBareReturns(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  		WithFragment(false),
    84  		WithAllErrors(false),
    85  		WithRemoveBareReturns(false),
    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) GetFragment() bool          { return cc.Fragment }
   134  func (cc *Options) GetAllErrors() bool         { return cc.AllErrors }
   135  func (cc *Options) GetRemoveBareReturns() bool { return cc.RemoveBareReturns }
   136  
   137  // OptionsVisitor visitor interface for Options
   138  type OptionsVisitor interface {
   139  	GetFragment() bool
   140  	GetAllErrors() bool
   141  	GetRemoveBareReturns() bool
   142  }
   143  
   144  // OptionsInterface visitor + ApplyOption interface for Options
   145  type OptionsInterface interface {
   146  	OptionsVisitor
   147  	ApplyOption(...Option) []Option
   148  }