mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 19:27:46 +08:00
add default value for gcfg.Get* functions; rename gconv.TimeDuration to gconv.Duration, and do corresponding changes to caller packages
This commit is contained in:
parent
fdfefbb94d
commit
718997327a
@ -80,7 +80,7 @@ func (v *Var) Time(format...string) time.Time {
|
||||
// TimeDuration converts and returns <v> as time.Duration.
|
||||
// If value of <v> is string, then it uses time.ParseDuration for conversion.
|
||||
func (v *Var) TimeDuration() time.Duration {
|
||||
return gconv.TimeDuration(v.Val())
|
||||
return gconv.Duration(v.Val())
|
||||
}
|
||||
|
||||
// GTime converts and returns <v> as *gtime.Time.
|
||||
|
@ -178,8 +178,8 @@ func (j *Json) GetTime(pattern string, format... string) time.Time {
|
||||
return gconv.Time(j.Get(pattern), format...)
|
||||
}
|
||||
|
||||
func (j *Json) GetTimeDuration(pattern string, def...interface{}) time.Duration {
|
||||
return gconv.TimeDuration(j.Get(pattern, def...))
|
||||
func (j *Json) GetDuration(pattern string, def...interface{}) time.Duration {
|
||||
return gconv.Duration(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
func (j *Json) GetGTime(pattern string, format... string) *gtime.Time {
|
||||
|
@ -130,8 +130,8 @@ func (p *Parser) GetTime(pattern string, format...string) time.Time {
|
||||
return p.json.GetTime(pattern, format...)
|
||||
}
|
||||
|
||||
func (p *Parser) GetTimeDuration(pattern string, def...interface{}) time.Duration {
|
||||
return p.json.GetTimeDuration(pattern, def...)
|
||||
func (p *Parser) GetDuration(pattern string, def...interface{}) time.Duration {
|
||||
return p.json.GetDuration(pattern, def...)
|
||||
}
|
||||
|
||||
func (p *Parser) GetGTime(pattern string, format...string) *gtime.Time {
|
||||
|
@ -201,10 +201,10 @@ func Redis(name...string) *gredis.Redis {
|
||||
redisConfig.MaxActive = gconv.Int(v)
|
||||
}
|
||||
if v, ok := parse["idleTimeout"]; ok {
|
||||
redisConfig.IdleTimeout = gconv.TimeDuration(v)*time.Second
|
||||
redisConfig.IdleTimeout = gconv.Duration(v)*time.Second
|
||||
}
|
||||
if v, ok := parse["maxConnLifetime"]; ok {
|
||||
redisConfig.MaxConnLifetime = gconv.TimeDuration(v)*time.Second
|
||||
redisConfig.MaxConnLifetime = gconv.Duration(v)*time.Second
|
||||
}
|
||||
addConfigMonitor(key, config)
|
||||
return gredis.New(redisConfig)
|
||||
@ -225,7 +225,7 @@ func Redis(name...string) *gredis.Redis {
|
||||
glog.Errorfln(`configuration for redis not found for group "%s"`, group)
|
||||
}
|
||||
} else {
|
||||
glog.Errorfln(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.GetFilePath())
|
||||
glog.Errorfln(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -238,7 +238,7 @@ func Redis(name...string) *gredis.Redis {
|
||||
// 添加对单例对象的配置文件inotify监控
|
||||
func addConfigMonitor(key string, config *gcfg.Config) {
|
||||
// 使用gfsnotify进行文件监控,当配置文件有任何变化时,清空对象单例缓存
|
||||
if path := config.GetFilePath(); path != "" {
|
||||
if path := config.FilePath(); path != "" {
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
instances.Remove(key)
|
||||
})
|
||||
@ -246,7 +246,7 @@ func addConfigMonitor(key string, config *gcfg.Config) {
|
||||
}
|
||||
|
||||
// 模板内置方法:config
|
||||
func funcConfig(pattern string, file...string) string {
|
||||
func funcConfig(pattern string, file...interface{}) string {
|
||||
return Config().GetString(pattern, file...)
|
||||
}
|
||||
|
||||
|
@ -224,8 +224,8 @@ func (s *Session) GetGTime(key string, format...string) *gtime.Time {
|
||||
return gconv.GTime(s.Get(key), format...)
|
||||
}
|
||||
|
||||
func (s *Session) GetTimeDuration(key string, def...interface{}) time.Duration {
|
||||
return gconv.TimeDuration(s.Get(key, def...))
|
||||
func (s *Session) GetDuration(key string, def...interface{}) time.Duration {
|
||||
return gconv.Duration(s.Get(key, def...))
|
||||
}
|
||||
|
||||
// 将变量转换为对象,注意 pointer 参数必须为struct指针
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/os/gspath"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -39,7 +41,7 @@ type Config struct {
|
||||
|
||||
// New returns a new configuration management object.
|
||||
// The param <file> specifies the default configuration file name for reading.
|
||||
func New(file ...string) *Config {
|
||||
func New(file...string) *Config {
|
||||
name := DEFAULT_CONFIG_FILE
|
||||
if len(file) > 0 {
|
||||
name = file[0]
|
||||
@ -78,7 +80,7 @@ func (c *Config) filePath(file...string) (path string) {
|
||||
if len(file) > 0 {
|
||||
name = file[0]
|
||||
}
|
||||
path = c.GetFilePath(name)
|
||||
path = c.FilePath(name)
|
||||
if path == "" {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if c.paths.Len() > 0 {
|
||||
@ -101,7 +103,8 @@ func (c *Config) filePath(file...string) (path string) {
|
||||
}
|
||||
|
||||
// SetPath sets the configuration directory path for file search.
|
||||
// The param <path> can be absolute or relative path, but absolute path is suggested.
|
||||
// The param <path> can be absolute or relative path,
|
||||
// but absolute path is strongly recommended.
|
||||
func (c *Config) SetPath(path string) error {
|
||||
// Absolute path.
|
||||
realPath := gfile.RealPath(path)
|
||||
@ -149,7 +152,7 @@ func (c *Config) SetPath(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetViolenceCheck sets whether to perform level conflict check.
|
||||
// SetViolenceCheck sets whether to perform hierarchical conflict check.
|
||||
// This feature needs to be enabled when there is a level symbol in the key name.
|
||||
// The default is off.
|
||||
// Turning on this feature is quite expensive,
|
||||
@ -205,11 +208,17 @@ func (c *Config) AddPath(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
// Alias of FilePath.
|
||||
func (c *Config) GetFilePath(file...string) (path string) {
|
||||
return c.FilePath(file...)
|
||||
}
|
||||
|
||||
// GetFilePath returns the absolute path of the specified configuration file.
|
||||
// If <file> is not passed, it returns the configuration file path of the default name.
|
||||
// If the specified configuration file does not exist,
|
||||
// an empty string is returned.
|
||||
func (c *Config) GetFilePath(file...string) (path string) {
|
||||
func (c *Config) FilePath(file...string) (path string) {
|
||||
name := c.name.Val()
|
||||
if len(file) > 0 {
|
||||
name = file[0]
|
||||
@ -280,171 +289,192 @@ func (c *Config) getJson(file...string) *gjson.Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) Get(pattern string, file...string) interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.Get(pattern)
|
||||
func (c *Config) Get(pattern string, def...interface{}) interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.Get(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetVar(pattern string, file...string) gvar.VarRead {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return gvar.New(j.Get(pattern), true)
|
||||
func (c *Config) GetVar(pattern string, def...interface{}) gvar.VarRead {
|
||||
if j := c.getJson(); j != nil {
|
||||
return gvar.New(j.Get(pattern, def...), true)
|
||||
}
|
||||
return gvar.New(nil, true)
|
||||
}
|
||||
|
||||
func (c *Config) Contains(pattern string, file...string) bool {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
func (c *Config) Contains(pattern string) bool {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.Contains(pattern)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetMap(pattern)
|
||||
func (c *Config) GetMap(pattern string, def...interface{}) map[string]interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMap(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetArray(pattern string, file...string) []interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetArray(pattern)
|
||||
func (c *Config) GetArray(pattern string, def...interface{}) []interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetArray(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetString(pattern string, file...string) string {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetString(pattern)
|
||||
func (c *Config) GetString(pattern string, def...interface{}) string {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetString(pattern, def...)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *Config) GetStrings(pattern string, file...string) []string {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetStrings(pattern)
|
||||
func (c *Config) GetStrings(pattern string, def...interface{}) []string {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStrings(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetInterfaces(pattern string, file...string) []interface{} {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInterfaces(pattern)
|
||||
func (c *Config) GetInterfaces(pattern string, def...interface{}) []interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInterfaces(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetBool(pattern string, file...string) bool {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetBool(pattern)
|
||||
func (c *Config) GetBool(pattern string, def...interface{}) bool {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetBool(pattern, def...)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Config) GetFloat32(pattern string, file...string) float32 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetFloat32(pattern)
|
||||
func (c *Config) GetFloat32(pattern string, def...interface{}) float32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloat32(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetFloat64(pattern string, file...string) float64 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetFloat64(pattern)
|
||||
func (c *Config) GetFloat64(pattern string, def...interface{}) float64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloat64(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetFloats(pattern string, file...string) []float64 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetFloats(pattern)
|
||||
func (c *Config) GetFloats(pattern string, def...interface{}) []float64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloats(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetInt(pattern string, file...string) int {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt(pattern)
|
||||
func (c *Config) GetInt(pattern string, def...interface{}) int {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
func (c *Config) GetInt8(pattern string, file...string) int8 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt8(pattern)
|
||||
func (c *Config) GetInt8(pattern string, def...interface{}) int8 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt8(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetInt16(pattern string, file...string) int16 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt16(pattern)
|
||||
func (c *Config) GetInt16(pattern string, def...interface{}) int16 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt16(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetInt32(pattern string, file...string) int32 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt32(pattern)
|
||||
func (c *Config) GetInt32(pattern string, def...interface{}) int32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt32(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetInt64(pattern string, file...string) int64 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInt64(pattern)
|
||||
func (c *Config) GetInt64(pattern string, def...interface{}) int64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt64(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetInts(pattern string, file...string) []int {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetInts(pattern)
|
||||
func (c *Config) GetInts(pattern string, def...interface{}) []int {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInts(pattern, def...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetUint(pattern string, file...string) uint {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint(pattern)
|
||||
func (c *Config) GetUint(pattern string, def...interface{}) uint {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetUint8(pattern string, file...string) uint8 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint8(pattern)
|
||||
func (c *Config) GetUint8(pattern string, def...interface{}) uint8 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint8(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetUint16(pattern string, file...string) uint16 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint16(pattern)
|
||||
func (c *Config) GetUint16(pattern string, def...interface{}) uint16 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint16(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetUint32(pattern string, file...string) uint32 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint32(pattern)
|
||||
func (c *Config) GetUint32(pattern string, def...interface{}) uint32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint32(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetUint64(pattern string, file...string) uint64 {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetUint64(pattern)
|
||||
func (c *Config) GetUint64(pattern string, def...interface{}) uint64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint64(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetToStruct(pattern string, objPointer interface{}, file...string) error {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.GetToStruct(pattern, objPointer)
|
||||
func (c *Config) GetTime(pattern string, format...string) time.Time {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetTime(pattern, format...)
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (c *Config) GetDuration(pattern string, def...interface{}) time.Duration {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetDuration(pattern, def...)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *Config) GetGTime(pattern string, format...string) *gtime.Time {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetGTime(pattern, format...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) GetToStruct(pattern string, pointer interface{}, def...interface{}) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetToStruct(pattern, pointer)
|
||||
}
|
||||
return errors.New("config file not found")
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ var (
|
||||
|
||||
// Instance returns an instance of Config with default settings.
|
||||
// The param <name> is the name for the instance.
|
||||
func Instance(name ...string) *Config {
|
||||
func Instance(name...string) *Config {
|
||||
key := DEFAULT_GROUP_NAME
|
||||
if len(name) > 0 {
|
||||
key = name[0]
|
||||
|
@ -71,7 +71,7 @@ array = [1,2,3]
|
||||
"disk" : "127.0.0.1:6379,0",
|
||||
"cache" : "127.0.0.1:6379,1",
|
||||
})
|
||||
gtest.AssertEQ(c.GetFilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
gtest.AssertEQ(c.FilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
|
||||
})
|
||||
}
|
||||
@ -197,7 +197,7 @@ func Test_SetFileName(t *testing.T) {
|
||||
"disk" : "127.0.0.1:6379,0",
|
||||
"cache" : "127.0.0.1:6379,1",
|
||||
})
|
||||
gtest.AssertEQ(c.GetFilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
gtest.AssertEQ(c.FilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
|
||||
})
|
||||
}
|
||||
@ -265,7 +265,7 @@ func Test_Instance(t *testing.T) {
|
||||
"disk" : "127.0.0.1:6379,0",
|
||||
"cache" : "127.0.0.1:6379,1",
|
||||
})
|
||||
gtest.AssertEQ(c.GetFilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
gtest.AssertEQ(c.FilePath(), gfile.Pwd() + gfile.Separator + path)
|
||||
|
||||
})
|
||||
}
|
@ -34,7 +34,7 @@ var (
|
||||
// 默认定时器属性参数值
|
||||
defaultSlots = cmdenv.Get("gf.gtimer.slots", gDEFAULT_SLOT_NUMBER).Int()
|
||||
defaultLevel = cmdenv.Get("gf.gtimer.level", gDEFAULT_WHEEL_LEVEL).Int()
|
||||
defaultInterval = cmdenv.Get("gf.gtimer.interval", gDEFAULT_WHEEL_INTERVAL).TimeDuration()*time.Millisecond
|
||||
defaultInterval = cmdenv.Get("gf.gtimer.interval", gDEFAULT_WHEEL_INTERVAL).Duration()*time.Millisecond
|
||||
// 默认的wheel管理对象
|
||||
defaultTimer = New(defaultSlots, defaultInterval, defaultLevel)
|
||||
)
|
||||
|
@ -17,10 +17,10 @@ func Time(i interface{}, format...string) time.Time {
|
||||
return GTime(i, format...).Time
|
||||
}
|
||||
|
||||
// TimeDuration converts <i> to time.Duration.
|
||||
// Duration converts <i> to time.Duration.
|
||||
// If <i> is string, then it uses time.ParseDuration to convert it.
|
||||
// If <i> is numeric, then it converts <i> as nanoseconds.
|
||||
func TimeDuration(i interface{}) time.Duration {
|
||||
func Duration(i interface{}) time.Duration {
|
||||
s := String(i)
|
||||
if !gstr.IsNumeric(s) {
|
||||
d, _ := time.ParseDuration(s)
|
||||
|
@ -20,6 +20,6 @@ func Test_Time(t *testing.T) {
|
||||
t1 := "2011-10-10 01:02:03.456"
|
||||
gtest.AssertEQ(gconv.GTime(t1), gtime.NewFromStr(t1))
|
||||
gtest.AssertEQ(gconv.Time(t1), gtime.NewFromStr(t1).Time)
|
||||
gtest.AssertEQ(gconv.TimeDuration(100), 100*time.Nanosecond)
|
||||
gtest.AssertEQ(gconv.Duration(100), 100*time.Nanosecond)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user