github.com/wfusion/gofusion@v1.1.14/common/utils/options.go (about)

     1  package utils
     2  
     3  type OptionExtender interface {
     4  	applyOption(t any)
     5  }
     6  
     7  type OptionFunc[T any] func(*T)
     8  
     9  func (o OptionFunc[T]) applyOption(a any) {
    10  	if t, ok := a.(*T); ok {
    11  		o(t)
    12  	}
    13  }
    14  
    15  func ApplyOptions[T any](opts ...OptionExtender) (t *T) {
    16  	t = new(T)
    17  	for _, optional := range opts {
    18  		if optional != nil {
    19  			optional.applyOption(t)
    20  		}
    21  	}
    22  	return
    23  }