2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-05-08 22:04:36 +08:00
|
|
|
// Package gcfg provides reading, caching and managing for configuration.
|
2017-12-30 23:49:55 +08:00
|
|
|
package gcfg
|
2017-12-13 16:45:00 +08:00
|
|
|
|
|
|
|
import (
|
2019-04-03 09:59:15 +08:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/g/container/garray"
|
|
|
|
"github.com/gogf/gf/g/container/gmap"
|
|
|
|
"github.com/gogf/gf/g/container/gtype"
|
|
|
|
"github.com/gogf/gf/g/container/gvar"
|
|
|
|
"github.com/gogf/gf/g/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/g/internal/cmdenv"
|
|
|
|
"github.com/gogf/gf/g/os/gfile"
|
|
|
|
"github.com/gogf/gf/g/os/gfsnotify"
|
|
|
|
"github.com/gogf/gf/g/os/glog"
|
|
|
|
"github.com/gogf/gf/g/os/gspath"
|
2019-05-09 17:14:24 +08:00
|
|
|
"github.com/gogf/gf/g/os/gtime"
|
|
|
|
"time"
|
2017-12-13 16:45:00 +08:00
|
|
|
)
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
const (
|
2019-04-02 14:37:46 +08:00
|
|
|
// Default configuration file name.
|
2019-03-10 00:35:03 +08:00
|
|
|
DEFAULT_CONFIG_FILE = "config.toml"
|
2017-12-14 17:32:51 +08:00
|
|
|
)
|
2017-12-13 16:45:00 +08:00
|
|
|
|
2019-04-02 14:37:46 +08:00
|
|
|
// Configuration struct.
|
2017-12-14 17:32:51 +08:00
|
|
|
type Config struct {
|
2019-04-02 14:37:46 +08:00
|
|
|
name *gtype.String // Default configuration file name.
|
|
|
|
paths *garray.StringArray // Searching path array.
|
2019-05-08 21:03:04 +08:00
|
|
|
jsons *gmap.StrAnyMap // The pared JSON objects for configuration files.
|
2019-04-02 14:37:46 +08:00
|
|
|
vc *gtype.Bool // Whether do violence check in value index searching.
|
|
|
|
// It affects the performance when set true(false in default).
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
// New returns a new configuration management object.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <file> specifies the default configuration file name for reading.
|
2019-05-09 17:14:24 +08:00
|
|
|
func New(file...string) *Config {
|
2018-10-13 20:43:21 +08:00
|
|
|
name := DEFAULT_CONFIG_FILE
|
2018-05-23 18:42:21 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
2018-10-30 23:58:10 +08:00
|
|
|
c := &Config {
|
2018-05-23 18:42:21 +08:00
|
|
|
name : gtype.NewString(name),
|
2019-02-01 22:00:58 +08:00
|
|
|
paths : garray.NewStringArray(),
|
2019-05-08 17:21:18 +08:00
|
|
|
jsons : gmap.NewStrAnyMap(),
|
2018-07-01 00:38:35 +08:00
|
|
|
vc : gtype.NewBool(),
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
2019-04-03 23:39:31 +08:00
|
|
|
// Customized dir path from env/cmd.
|
|
|
|
if envPath := cmdenv.Get("gf.gcfg.path").String(); envPath != "" {
|
|
|
|
if gfile.Exists(envPath) {
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.SetPath(envPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
} else {
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Errorf("Configuration directory path does not exist: %s", envPath)
|
|
|
|
}
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Dir path of working dir.
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.SetPath(gfile.Pwd())
|
2019-04-03 23:39:31 +08:00
|
|
|
// Dir path of binary.
|
|
|
|
if selfPath := gfile.SelfDir(); selfPath != "" && gfile.Exists(selfPath) {
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.AddPath(selfPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
// Dir path of main package.
|
|
|
|
if mainPath := gfile.MainPkgPath(); mainPath != "" && gfile.Exists(mainPath) {
|
2019-06-05 21:58:27 +08:00
|
|
|
_ = c.AddPath(mainPath)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
2019-04-03 09:59:15 +08:00
|
|
|
}
|
2018-10-30 23:58:10 +08:00
|
|
|
return c
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 15:21:00 +08:00
|
|
|
// filePath returns the absolute configuration file path for the given filename by <file>.
|
2018-11-30 09:48:57 +08:00
|
|
|
func (c *Config) filePath(file...string) (path string) {
|
2018-05-23 18:42:21 +08:00
|
|
|
name := c.name.Val()
|
2018-04-17 15:09:32 +08:00
|
|
|
if len(file) > 0 {
|
2018-05-03 13:35:08 +08:00
|
|
|
name = file[0]
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
2019-05-09 17:14:24 +08:00
|
|
|
path = c.FilePath(name)
|
2018-11-25 22:18:36 +08:00
|
|
|
if path == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
2019-03-01 23:45:55 +08:00
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" in following paths:", name))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
2019-04-04 09:18:43 +08:00
|
|
|
index := 1
|
|
|
|
for _, v := range array {
|
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v))
|
|
|
|
index++
|
2019-04-22 15:47:59 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v + gfile.Separator + "config"))
|
2019-04-04 09:18:43 +08:00
|
|
|
index++
|
2019-03-01 23:45:55 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path set/add", name))
|
|
|
|
}
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(buffer.String())
|
|
|
|
}
|
2018-11-25 22:18:36 +08:00
|
|
|
}
|
2018-11-17 02:39:23 +08:00
|
|
|
return path
|
2018-01-04 11:08:23 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// SetPath sets the configuration directory path for file search.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <path> can be absolute or relative path,
|
2019-05-09 17:14:24 +08:00
|
|
|
// but absolute path is strongly recommended.
|
2018-05-03 13:35:08 +08:00
|
|
|
func (c *Config) SetPath(path string) error {
|
2019-04-05 00:23:59 +08:00
|
|
|
// Absolute path.
|
2018-11-30 09:48:57 +08:00
|
|
|
realPath := gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
2019-04-05 00:23:59 +08:00
|
|
|
// Relative path.
|
2019-03-27 11:48:53 +08:00
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, v := range array {
|
|
|
|
if path, _ := gspath.Search(v, path); path != "" {
|
|
|
|
realPath = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-04-05 00:23:59 +08:00
|
|
|
// Path not exist.
|
2019-03-27 11:48:53 +08:00
|
|
|
if realPath == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] SetPath failed: cannot find directory \"%s\" in following paths:", path))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for k, v := range array {
|
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s",k + 1, v))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" does not exist`, path))
|
|
|
|
}
|
|
|
|
err := errors.New(buffer.String())
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2019-03-27 11:48:53 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-04-05 00:23:59 +08:00
|
|
|
// Should be a directory.
|
2019-03-27 11:48:53 +08:00
|
|
|
if !gfile.IsDir(realPath) {
|
|
|
|
err := errors.New(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" should be directory type`, path))
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
return err
|
2018-01-04 11:59:35 +08:00
|
|
|
}
|
2019-04-05 00:23:59 +08:00
|
|
|
// Repeated path check.
|
2019-02-27 22:53:39 +08:00
|
|
|
if c.paths.Search(realPath) != -1 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-30 09:48:57 +08:00
|
|
|
c.jsons.Clear()
|
|
|
|
c.paths.Clear()
|
|
|
|
c.paths.Append(realPath)
|
2018-05-03 13:35:08 +08:00
|
|
|
return nil
|
2018-04-17 14:47:45 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
// SetViolenceCheck sets whether to perform hierarchical conflict check.
|
2019-04-05 00:23:59 +08:00
|
|
|
// 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,
|
|
|
|
// and it is not recommended to allow separators in the key names.
|
|
|
|
// It is best to avoid this on the application side.
|
2018-07-01 00:38:35 +08:00
|
|
|
func (c *Config) SetViolenceCheck(check bool) {
|
|
|
|
c.vc.Set(check)
|
2019-04-02 16:08:46 +08:00
|
|
|
c.Clear()
|
2018-07-01 00:38:35 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// AddPath adds a absolute or relative path to the search paths.
|
2018-05-03 13:35:08 +08:00
|
|
|
func (c *Config) AddPath(path string) error {
|
2019-04-05 00:23:59 +08:00
|
|
|
// Absolute path.
|
2018-11-30 09:48:57 +08:00
|
|
|
realPath := gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
2019-04-05 00:23:59 +08:00
|
|
|
// Relative path.
|
2019-03-27 11:48:53 +08:00
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, v := range array {
|
|
|
|
if path, _ := gspath.Search(v, path); path != "" {
|
|
|
|
realPath = path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if realPath == "" {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
if c.paths.Len() > 0 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[gcfg] AddPath failed: cannot find directory \"%s\" in following paths:", path))
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for k, v := range array {
|
2019-04-04 09:18:43 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf("\n%d. %s", k + 1, v))
|
2019-03-27 11:48:53 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" does not exist`, path))
|
|
|
|
}
|
|
|
|
err := errors.New(buffer.String())
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2019-03-27 11:48:53 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !gfile.IsDir(realPath) {
|
|
|
|
err := errors.New(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" should be directory type`, path))
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-04-05 00:23:59 +08:00
|
|
|
// Repeated path check.
|
2019-02-27 22:53:39 +08:00
|
|
|
if c.paths.Search(realPath) != -1 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-30 09:48:57 +08:00
|
|
|
c.paths.Append(realPath)
|
2019-03-01 23:45:55 +08:00
|
|
|
//glog.Debug("[gcfg] AddPath:", realPath)
|
2018-05-03 13:35:08 +08:00
|
|
|
return nil
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
// Deprecated.
|
|
|
|
// Alias of FilePath.
|
|
|
|
func (c *Config) GetFilePath(file...string) (path string) {
|
|
|
|
return c.FilePath(file...)
|
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// 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.
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) FilePath(file...string) (path string) {
|
2019-02-27 22:53:39 +08:00
|
|
|
name := c.name.Val()
|
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
|
|
|
c.paths.RLockFunc(func(array []string) {
|
|
|
|
for _, v := range array {
|
|
|
|
if path, _ = gspath.Search(v, name); path != "" {
|
|
|
|
break
|
|
|
|
}
|
2019-04-22 15:47:59 +08:00
|
|
|
if path, _ = gspath.Search(v + gfile.Separator + "config", name); path != "" {
|
2019-03-15 14:54:01 +08:00
|
|
|
break
|
|
|
|
}
|
2019-02-27 22:53:39 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
2018-04-21 11:20:31 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// SetFileName sets the default configuration file name.
|
2018-05-23 18:42:21 +08:00
|
|
|
func (c *Config) SetFileName(name string) {
|
|
|
|
c.name.Set(name)
|
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// GetFileName returns the default configuration file name.
|
2019-03-21 18:20:20 +08:00
|
|
|
func (c *Config) GetFileName() string {
|
|
|
|
return c.name.Val()
|
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:59 +08:00
|
|
|
// getJson returns a gjson.Json object for the specified <file> content.
|
|
|
|
// It would print error if file reading fails.
|
|
|
|
// If any error occurs, it return nil.
|
2018-05-03 13:35:08 +08:00
|
|
|
func (c *Config) getJson(file...string) *gjson.Json {
|
2019-03-21 18:20:20 +08:00
|
|
|
name := c.name.Val()
|
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
2018-11-25 22:18:36 +08:00
|
|
|
}
|
2019-03-21 18:20:20 +08:00
|
|
|
r := c.jsons.GetOrSetFuncLock(name, func() interface{} {
|
2019-04-02 14:37:46 +08:00
|
|
|
content := ""
|
|
|
|
filePath := ""
|
|
|
|
if content = GetContent(name); content == "" {
|
|
|
|
filePath = c.filePath(name)
|
|
|
|
if filePath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
content = gfile.GetContents(filePath)
|
2019-03-21 18:20:20 +08:00
|
|
|
}
|
2019-04-02 14:37:46 +08:00
|
|
|
if j, err := gjson.LoadContent(content); err == nil {
|
2019-03-21 18:20:20 +08:00
|
|
|
j.SetViolenceCheck(c.vc.Val())
|
2019-04-05 00:23:59 +08:00
|
|
|
// Add monitor for this configuration file,
|
|
|
|
// any changes of this file will refresh its cache in Config object.
|
2019-04-02 14:37:46 +08:00
|
|
|
if filePath != "" {
|
2019-06-11 21:14:48 +08:00
|
|
|
_, _ = gfsnotify.Add(filePath, func(event *gfsnotify.Event) {
|
2019-04-02 14:37:46 +08:00
|
|
|
c.jsons.Remove(name)
|
|
|
|
})
|
|
|
|
}
|
2019-03-21 18:20:20 +08:00
|
|
|
return j
|
|
|
|
} else {
|
2019-06-13 20:29:40 +08:00
|
|
|
if errorPrint() {
|
|
|
|
if filePath != "" {
|
|
|
|
glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error())
|
|
|
|
} else {
|
|
|
|
glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 18:20:20 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if r != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
return r.(*gjson.Json)
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-13 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) Get(pattern string, def...interface{}) interface{} {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.Get(pattern, def...)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
return nil
|
2017-12-13 17:35:43 +08:00
|
|
|
}
|
|
|
|
|
2019-05-10 13:38:06 +08:00
|
|
|
func (c *Config) GetVar(pattern string, def...interface{}) *gvar.Var {
|
2019-05-09 17:14:24 +08:00
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return gvar.New(j.Get(pattern, def...), true)
|
2018-10-27 16:05:36 +08:00
|
|
|
}
|
2019-01-14 22:55:43 +08:00
|
|
|
return gvar.New(nil, true)
|
2018-10-27 16:05:36 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) Contains(pattern string) bool {
|
|
|
|
if j := c.getJson(); j != nil {
|
2019-03-15 14:54:01 +08:00
|
|
|
return j.Contains(pattern)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetMap(pattern string, def...interface{}) map[string]interface{} {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetMap(pattern, def...)
|
2017-12-13 17:35:43 +08:00
|
|
|
}
|
2017-12-14 17:32:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetArray(pattern string, def...interface{}) []interface{} {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetArray(pattern, def...)
|
2017-12-13 17:35:43 +08:00
|
|
|
}
|
|
|
|
return nil
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetString(pattern string, def...interface{}) string {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetString(pattern, def...)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetStrings(pattern string, def...interface{}) []string {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetStrings(pattern, def...)
|
2018-07-01 00:27:33 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInterfaces(pattern string, def...interface{}) []interface{} {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInterfaces(pattern, def...)
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetBool(pattern string, def...interface{}) bool {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetBool(pattern, def...)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetFloat32(pattern string, def...interface{}) float32 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetFloat32(pattern, def...)
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetFloat64(pattern string, def...interface{}) float64 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetFloat64(pattern, def...)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetFloats(pattern string, def...interface{}) []float64 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetFloats(pattern, def...)
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInt(pattern string, def...interface{}) int {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInt(pattern, def...)
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
return 0
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:14:37 +08:00
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInt8(pattern string, def...interface{}) int8 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInt8(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInt16(pattern string, def...interface{}) int16 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInt16(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInt32(pattern string, def...interface{}) int32 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInt32(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInt64(pattern string, def...interface{}) int64 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInt64(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetInts(pattern string, def...interface{}) []int {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetInts(pattern, def...)
|
2018-10-09 10:05:55 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetUint(pattern string, def...interface{}) uint {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetUint(pattern, def...)
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
return 0
|
2018-04-17 17:09:07 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetUint8(pattern string, def...interface{}) uint8 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetUint8(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetUint16(pattern string, def...interface{}) uint16 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetUint16(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetUint32(pattern string, def...interface{}) uint32 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetUint32(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
func (c *Config) GetUint64(pattern string, def...interface{}) uint64 {
|
|
|
|
if j := c.getJson(); j != nil {
|
|
|
|
return j.GetUint64(pattern, def...)
|
2018-05-23 20:14:37 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:24 +08:00
|
|
|
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)
|
2018-10-08 13:38:36 +08:00
|
|
|
}
|
|
|
|
return errors.New("config file not found")
|
|
|
|
}
|
|
|
|
|
2019-04-02 16:08:46 +08:00
|
|
|
// Deprecated. See Clear.
|
2019-04-02 17:10:21 +08:00
|
|
|
func (c *Config) Reload() {
|
2019-04-02 16:08:46 +08:00
|
|
|
c.jsons.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes all parsed configuration files content cache,
|
|
|
|
// which will force reload configuration content from file.
|
|
|
|
func (c *Config) Clear() {
|
2018-04-17 17:09:07 +08:00
|
|
|
c.jsons.Clear()
|
|
|
|
}
|
|
|
|
|