Documentation ¶
Overview ¶
Package gcfg provides reading, caching and managing for configuration.
Index ¶
- Constants
- type Adapter
- type AdapterFile
- func (c *AdapterFile) AddPath(path string) (err error)
- func (c *AdapterFile) Available(fileName ...string) bool
- func (c *AdapterFile) Clear()
- func (c *AdapterFile) ClearContent()
- func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (c *AdapterFile) Dump()
- func (c *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
- func (c *AdapterFile) GetContent(file ...string) string
- func (c *AdapterFile) GetFileName() string
- func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error)
- func (c *AdapterFile) MustGet(ctx context.Context, pattern string) *gvar.Var
- func (c *AdapterFile) RemoveContent(file ...string)
- func (c *AdapterFile) SetContent(content string, file ...string)
- func (c *AdapterFile) SetFileName(name string)
- func (c *AdapterFile) SetPath(path string) (err error)
- func (c *AdapterFile) SetViolenceCheck(check bool)
- type Config
- func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) GetAdapter() Adapter
- func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
- func (c *Config) MustData(ctx context.Context) map[string]interface{}
- func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var
- func (c *Config) Set(ctx context.Context, pattern string, value interface{})
- func (c *Config) SetAdapter(adapter Adapter)
Constants ¶
const (
DefaultConfigFile = "config.toml" // DefaultConfigFile is the default configuration file name.
)
const (
DefaultName = "config" // DefaultName is the default group name for instance usage.
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { // Get retrieves and returns value by specified `pattern`. Get(ctx context.Context, pattern string) (value interface{}, err error) // Data retrieves and returns all configuration data as map type. // Note that this function may lead lots of memory usage if configuration data is too large, // you can implement this function if necessary. Data(ctx context.Context) (data map[string]interface{}, err error) }
Adapter is the interface for configuration retrieving.
type AdapterFile ¶
type AdapterFile struct {
// contains filtered or unexported fields
}
func NewAdapterFile ¶
func NewAdapterFile(file ...string) (*AdapterFile, error)
NewAdapterFile returns a new configuration management object. The parameter `file` specifies the default configuration file name for reading.
func (*AdapterFile) AddPath ¶
func (c *AdapterFile) AddPath(path string) (err error)
AddPath adds an absolute or relative path to the search paths.
func (*AdapterFile) Available ¶
func (c *AdapterFile) Available(fileName ...string) bool
Available checks and returns whether configuration of given `file` is available.
func (*AdapterFile) Clear ¶
func (c *AdapterFile) Clear()
Clear removes all parsed configuration files content cache, which will force reload configuration content from file.
func (*AdapterFile) ClearContent ¶
func (c *AdapterFile) ClearContent()
ClearContent removes all global configuration contents.
func (*AdapterFile) Data ¶
func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
Data retrieves and returns all configuration data as map type.
func (*AdapterFile) Dump ¶
func (c *AdapterFile) Dump()
Dump prints current Json object with more manually readable.
func (*AdapterFile) Get ¶
func (c *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by `def` if value for `pattern` is not found.
func (*AdapterFile) GetContent ¶
func (c *AdapterFile) GetContent(file ...string) string
GetContent returns customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) GetFileName ¶
func (c *AdapterFile) GetFileName() string
GetFileName returns the default configuration file name.
func (*AdapterFile) GetFilePath ¶
func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error)
GetFilePath returns the absolute configuration file path for the given filename by `file`. If `file` is not passed, it returns the configuration file path of the default name. It returns an empty `path` string and an error if the given `file` does not exist.
func (*AdapterFile) RemoveContent ¶
func (c *AdapterFile) RemoveContent(file ...string)
RemoveContent removes the global configuration with specified `file`. If `name` is not passed, it removes configuration of the default group name.
func (*AdapterFile) SetContent ¶
func (c *AdapterFile) SetContent(content string, file ...string)
SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) SetFileName ¶
func (c *AdapterFile) SetFileName(name string)
SetFileName sets the default configuration file name.
func (*AdapterFile) SetPath ¶
func (c *AdapterFile) SetPath(path string) (err error)
SetPath sets the configuration directory path for file search. The parameter `path` can be absolute or relative path, but absolute path is strongly recommended.
func (*AdapterFile) SetViolenceCheck ¶
func (c *AdapterFile) SetViolenceCheck(check bool)
SetViolenceCheck sets whether to perform hierarchical conflict checking. This feature needs to be enabled when there is a level symbol in the key name. It is off in default.
Note that, turning on this feature is quite expensive, and it is not recommended allowing separators in the key names. It is best to avoid this on the application side.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the configuration management object.
func Instance ¶
Instance returns an instance of Config with default settings. The parameter `name` is the name for the instance. But very note that, if the file "name.toml" exists in the configuration directory, it then sets it as the default configuration file. The toml file type is the default configuration file type.
func NewWithAdapter ¶
NewWithAdapter creates and returns a Config object with given adapter.
func (*Config) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
It returns a default value specified by `def` if value for `pattern` is not found.
func (*Config) GetAdapter ¶
GetAdapter returns the adapter of current Config object.
func (*Config) GetWithCmd ¶
func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
GetWithCmd returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
func (*Config) GetWithEnv ¶
func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error)
GetWithEnv returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
func (*Config) MustGetWithCmd ¶
MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
func (*Config) MustGetWithEnv ¶
MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
func (*Config) Set ¶
Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default. It is commonly used for updates certain configuration value in runtime.
func (*Config) SetAdapter ¶
SetAdapter sets the adapter of current Config object.