github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/config/format.go (about)

     1  package config
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/mitchellh/mapstructure"
     8  	"github.com/spf13/viper"
     9  	"github.com/volts-dev/utils"
    10  )
    11  
    12  type (
    13  	format struct {
    14  		sync.RWMutex
    15  		v *viper.Viper
    16  	}
    17  )
    18  
    19  func newFormat() *format {
    20  	file := viper.New()
    21  	//file.SetConfigFile(CONFIG_FILE_NAME)
    22  	return &format{
    23  		v: file,
    24  	}
    25  }
    26  
    27  func (self *format) GetBool(key string, defaultValue bool) bool {
    28  	self.RLock()
    29  	defer self.RUnlock()
    30  	self.SetDefault(key, defaultValue)
    31  	return self.v.GetBool(key)
    32  }
    33  
    34  // GetStringValue from default namespace
    35  func (self *format) GetString(key, defaultValue string) string {
    36  	self.RLock()
    37  	defer self.RUnlock()
    38  	val := self.v.GetString(key)
    39  	if val == "" {
    40  		return defaultValue
    41  	}
    42  	return val
    43  }
    44  
    45  // GetIntValue from default namespace
    46  func (self *format) GetInt(key string, defaultValue int) int {
    47  	self.RLock()
    48  	defer self.RUnlock()
    49  	val := self.v.GetInt(key)
    50  	if val == 0 {
    51  		return defaultValue
    52  	}
    53  	return val
    54  }
    55  
    56  func (self *format) GetInt32(key string, defaultValue int32) int32 {
    57  	self.RLock()
    58  	defer self.RUnlock()
    59  	val := self.v.GetInt32(key)
    60  	if val == 0 {
    61  		return defaultValue
    62  	}
    63  	return val
    64  }
    65  
    66  func (self *format) GetInt64(key string, defaultValue int64) int64 {
    67  	self.RLock()
    68  	defer self.RUnlock()
    69  	val := self.v.GetInt64(key)
    70  	if val == 0 {
    71  		return defaultValue
    72  	}
    73  	return val
    74  }
    75  
    76  func (self *format) GetIntSlice(key string, defaultValue []int) []int {
    77  	self.RLock()
    78  	defer self.RUnlock()
    79  	val := self.v.GetIntSlice(key)
    80  	if len(val) == 0 {
    81  		return defaultValue
    82  	}
    83  	return val
    84  }
    85  
    86  func (self *format) GetTime(key string, defaultValue time.Time) time.Time {
    87  	self.RLock()
    88  	defer self.RUnlock()
    89  	self.SetDefault(key, defaultValue)
    90  	return self.v.GetTime(key)
    91  }
    92  
    93  func (self *format) GetDuration(key string, defaultValue time.Duration) time.Duration {
    94  	self.RLock()
    95  	defer self.RUnlock()
    96  	val := self.v.GetDuration(key)
    97  	if val == 0 {
    98  		return defaultValue
    99  	}
   100  	return val
   101  }
   102  
   103  func (self *format) GetFloat64(key string, defaultValue float64) float64 {
   104  	self.RLock()
   105  	defer self.RUnlock()
   106  	val := self.v.GetFloat64(key)
   107  	if val == 0 {
   108  		return defaultValue
   109  	}
   110  	return val
   111  }
   112  
   113  func (self *format) Unmarshal(rawVal interface{}) error {
   114  	self.Lock()
   115  	defer self.Unlock()
   116  	return self.v.Unmarshal(rawVal, withTagName("field"), withMatchName())
   117  }
   118  
   119  // key 可以整个config结构
   120  func (self *format) UnmarshalKey(key string, rawVal interface{}) error {
   121  	self.Lock()
   122  	defer self.Unlock()
   123  	return self.v.UnmarshalKey(key, rawVal, withTagName("field"), withMatchName())
   124  }
   125  
   126  func (self *format) SetValue(key string, value interface{}) {
   127  	self.Lock()
   128  	self.v.Set(key, value)
   129  	self.Unlock()
   130  }
   131  
   132  func (self *format) SetDefault(key string, value interface{}) {
   133  	self.Lock()
   134  	self.v.SetDefault(key, value)
   135  	self.Unlock()
   136  }
   137  
   138  func withTagName(tag string) viper.DecoderConfigOption {
   139  	return func(c *mapstructure.DecoderConfig) {
   140  		c.TagName = tag
   141  	}
   142  }
   143  
   144  func withMatchName() viper.DecoderConfigOption {
   145  	return func(c *mapstructure.DecoderConfig) {
   146  		c.MatchName = func(mapKey, fieldName string) bool {
   147  			return mapKey == utils.SnakeCasedName(fieldName)
   148  		}
   149  	}
   150  }