Documentation ¶
Overview ¶
Package gcfg 提供了配置的读取、缓存和管理功能。
Index ¶
- Constants
- type Adapter
- type AdapterContent
- func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)
- func (a *AdapterContent) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (a *AdapterContent) Get(ctx context.Context, pattern string) (value interface{}, err error)
- func (a *AdapterContent) SetContent(content string) error
- type AdapterFile
- func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)
- func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool
- func (a *AdapterFile) Clear()
- func (a *AdapterFile) ClearContent()
- func (a *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
- func (a *AdapterFile) Dump()
- func (a *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
- func (a *AdapterFile) GetContent(file ...string) string
- func (a *AdapterFile) GetFileName() string
- func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error)
- func (a *AdapterFile) GetPaths() []string
- func (a *AdapterFile) MustGet(ctx context.Context, pattern string) *泛型类.Var
- func (a *AdapterFile) RemoveContent(file ...string)
- func (a *AdapterFile) SetContent(content string, file ...string)
- func (a *AdapterFile) SetFileName(name string)
- func (a *AdapterFile) SetPath(directoryPath string) (err error)
- func (a *AdapterFile) SetViolenceCheck(check bool)
- func (a *AdapterFile) X设置值(pattern string, value interface{}) error
- type Config
- func (c *Config) X取Map(上下文 context.Context) (值 map[string]interface{}, 错误 error)
- func (c *Config) X取MapPANI(上下文 context.Context) map[string]interface{}
- func (c *Config) X取值(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*泛型类.Var, error)
- func (c *Config) X取值PANI(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *泛型类.Var
- func (c *Config) X取值并从启动命令(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*泛型类.Var, error)
- func (c *Config) X取值并从启动命令PANI_有bug(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *泛型类.Var
- func (c *Config) X取值并从环境变量(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*泛型类.Var, error)
- func (c *Config) X取值并从环境变量PANI(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *泛型类.Var
- func (c *Config) X取适配器() Adapter
- func (c *Config) X是否可用(上下文 context.Context, resource ...string) (可用 bool)
- func (c *Config) X设置适配器(适配器 Adapter)
Constants ¶
const ( X默认实例名称 = "config" // DefaultName 是用于实例使用的默认实例名称。 X默认配置文件名称 = "config" // DefaultConfigFile 是默认的配置文件名称。 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { // Available 检查并返回配置服务是否可用。 // 可选参数 `resource` 用于指定特定的配置资源。 // // 注意,此函数不会返回错误,因为它只是简单地检查后端配置服务。 Available(ctx context.Context, resource ...string) (ok bool) // Get 通过指定的 `pattern` 从当前资源中获取并返回值。 // 模式示例: // "x.y.z" 用于获取映射项。 // "x.0.y" 用于获取切片项。 Get(ctx context.Context, pattern string) (value interface{}, err error) // Data 函数从当前资源中获取并返回所有的配置数据,以 map 的形式。 // 注意,如果配置数据过大,此函数可能会导致大量内存使用, // 如有必要,您可以自行实现这个函数。 Data(ctx context.Context) (data map[string]interface{}, err error) }
Adapter 是用于获取配置的接口。
type AdapterContent ¶
type AdapterContent struct {
// contains filtered or unexported fields
}
AdapterContent 实现了 Adapter 接口,采用内容方式实现。 配置内容支持 `gjson` 包所支持的各种编码类型。
func NewAdapterContent ¶
func NewAdapterContent(content ...string) (*AdapterContent, error)
NewAdapterContent返回一个使用自定义内容的新的配置管理对象。 参数`content`指定了用于读取的默认配置内容。
func (*AdapterContent) Available ¶
func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)
Available 检查并返回配置服务是否可用。 可选参数 `resource` 用于指定特定的配置资源。
注意,此函数不会返回错误,因为它只是简单地检查后端配置服务。
func (*AdapterContent) Data ¶
func (a *AdapterContent) Data(ctx context.Context) (data map[string]interface{}, err error)
Data 函数从当前资源中获取并返回所有的配置数据,以 map 的形式。 注意:如果配置数据过大,该函数可能会导致大量内存使用。 如果有必要,你可以自行实现这个函数。
func (*AdapterContent) Get ¶
func (a *AdapterContent) Get(ctx context.Context, pattern string) (value interface{}, err error)
Get 通过指定的 `pattern` 从当前资源中获取并返回值。 pattern 格式如下: "x.y.z" 用于获取 map 中的项。 "x.0.y" 用于获取 slice 中的项。
func (*AdapterContent) SetContent ¶
func (a *AdapterContent) SetContent(content string) error
SetContent 为指定的`file`设置自定义配置内容。 `file`参数不是必须的,默认值是DefaultConfigFile。
type AdapterFile ¶
type AdapterFile struct {
// contains filtered or unexported fields
}
AdapterFile实现了使用文件的Adapter接口。
func NewAdapterFile ¶
func NewAdapterFile(file ...string) (*AdapterFile, error)
NewAdapterFile 返回一个新的配置管理对象。 参数`file`指定了用于读取的默认配置文件名。
func (*AdapterFile) AddPath ¶
func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)
AddPath 将绝对或相对的 `directory` 路径添加到搜索路径中。
注意,此参数是目录而非文件的路径。
func (*AdapterFile) Available ¶
func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool
Available 检查并返回给定 `file` 配置是否可用。
func (*AdapterFile) Data ¶
func (a *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)
Data 函数获取并以 map 类型返回所有配置数据。
func (*AdapterFile) Get ¶
func (a *AdapterFile) Get(ctx context.Context, pattern string) (value interface{}, err error)
Get 方法通过指定的`pattern`获取并返回值。 如果`pattern`为空字符串或".",则返回当前Json对象的所有值。 若通过`pattern`未找到任何值,则返回nil。
我们还可以通过在`pattern`中使用索引号访问切片元素,例如: "list.10", "array.0.name", "array.0.1.id"。
如果根据`pattern`未能找到对应的值,则返回由`def`指定的默认值。
func (*AdapterFile) GetContent ¶
func (a *AdapterFile) GetContent(file ...string) string
GetContent 返回为指定 `file` 的自定义配置内容。 其中 `file` 参数非必要,默认为 DefaultConfigFile。
func (*AdapterFile) GetFileName ¶
func (a *AdapterFile) GetFileName() string
GetFileName 返回默认配置文件名称。
func (*AdapterFile) GetFilePath ¶
func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error)
GetFilePath 函数根据传入的文件名`file`返回该文件的绝对配置文件路径。 若未传递`file`参数,则返回默认文件名的配置文件路径。 如果给定的`file`不存在,则返回一个空字符串`path`及错误信息。
func (*AdapterFile) GetPaths ¶
func (a *AdapterFile) GetPaths() []string
GetPaths 返回当前配置管理器的搜索目录路径数组。
func (*AdapterFile) MustGet ¶
func (a *AdapterFile) MustGet(ctx context.Context, pattern string) *泛型类.Var
MustGet 行为类似于函数 Get,但在发生错误时会触发 panic。
func (*AdapterFile) RemoveContent ¶
func (a *AdapterFile) RemoveContent(file ...string)
RemoveContent 删除具有指定 `file` 的全局配置。 如果未传递 `name`,则删除默认组名的配置。
func (*AdapterFile) SetContent ¶
func (a *AdapterFile) SetContent(content string, file ...string)
SetContent 为指定的`file`设置自定义配置内容。 `file`参数不是必须的,默认值是DefaultConfigFile。
func (*AdapterFile) SetFileName ¶
func (a *AdapterFile) SetFileName(name string)
SetFileName 设置默认的配置文件名。
func (*AdapterFile) SetPath ¶
func (a *AdapterFile) SetPath(directoryPath string) (err error)
SetPath 设置配置文件搜索的 `directory` 路径。 参数 `path` 可以是绝对路径或相对 `directory` 路径, 但强烈建议使用绝对 `directory` 路径。
注意,此参数是一个目录而非文件的路径。
func (*AdapterFile) SetViolenceCheck ¶
func (a *AdapterFile) SetViolenceCheck(check bool)
SetViolenceCheck 设置是否进行层级冲突检查。 当键名中存在层级符号时,需要开启此功能。默认情况下,该功能是关闭的。
注意,开启此特性代价较高,且不建议在键名中允许分隔符。最好在应用层面避免这种情况。
func (*AdapterFile) X设置值 ¶
func (a *AdapterFile) X设置值(pattern string, value interface{}) error
Set 通过指定的`pattern`设置值。 它支持使用字符分隔符(默认为'. ')进行层级数据访问。 通常用于在运行时更新特定配置值。 注意,不建议在运行时使用`Set`方法来配置,因为如果底层配置文件发生更改, 配置将会自动刷新。因此,直接运行时设置可能不会持久生效。
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config 是配置管理对象。
func X取单例对象 ¶
Instance 返回一个使用默认设置的 Config 实例。 参数 `name` 是该实例的名称。但请注意,如果配置目录中存在名为 "name.toml" 的文件, 则将其设置为默认配置文件。toml 文件类型是默认的配置文件类型。
func (*Config) X取值 ¶
Get 方法通过指定的`pattern`获取并返回值。 如果给定的`pattern`为空字符串或".",则返回当前Json对象的所有值。 若未找到通过`pattern`匹配到的值,则返回nil。
当通过`pattern`未能找到对应的值时,将返回由`def`指定的默认值。
func (*Config) X取值并从启动命令 ¶
GetWithCmd 根据模式 `pattern` 返回配置值。 如果配置值不存在,则获取并返回由 `key` 指定的命令行选项。 若两者都不存在,则返回默认值 `def`。
获取规则:命令行参数采用小写格式,例如:gf.package.variable。
func (*Config) X取值并从启动命令PANI_有bug ¶
MustGetWithCmd 的行为与函数 GetWithCmd 相同,但当出现错误时它会触发 panic。
func (*Config) X取值并从环境变量 ¶
GetWithEnv 返回通过模式`pattern`指定的配置值。 如果配置值不存在,则获取并返回由`key`指定的环境变量值。 若两者都不存在,则返回默认值 `def`。
获取规则:环境变量参数采用大写格式,例如:GF_PACKAGE_VARIABLE。
func (*Config) X取值并从环境变量PANI ¶
MustGetWithEnv 行为类似于函数 GetWithEnv,但是当发生错误时它会触发panic。