配置类

package
v0.0.0-...-fcd50c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 21 Imported by: 1

Documentation

Overview

包gcfg提供了配置的读取、缓存和管理功能。 md5:5ae504d1379cd99a

Index

Constants

View Source
const (
	X默认实例名称   = "config" // DefaultName 是实例使用的默认实例名称。 md5:4736f3b4285b6846
	X默认配置文件名称 = "config" // DefaultConfigFile 是默认的配置文件名。 md5:b558e9c92a774f9a
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// 可用性检查并返回后端配置服务是否可用。
	// 可选参数 `resource` 指定特定的配置资源。
	//
	// 请注意,此函数不会返回错误,因为它只是简单地检查后端配置服务。
	// md5:8c240c72c0849cd7
	Available(ctx context.Context, resource ...string) (ok bool)

	// Get 通过在当前资源中指定的`pattern`获取并返回值。
	// 模式示例:
	// 对于映射项,使用 "x.y.z"。
	// 对于切片项,使用 "x.0.y"。
	// md5:821429a92b84150c
	Get(ctx context.Context, pattern string) (value interface{}, err error)

	// Data 获取并返回当前资源中的所有配置数据作为映射。
	// 请注意,如果配置数据过大,此函数可能导致大量内存使用。
	// 如果需要,你可以自行实现这个函数。
	// md5:7eaedd1a7f099a23
	Data(ctx context.Context) (data map[string]interface{}, err error)
}

Adapter是用于获取配置的接口。 md5:5c3d613bea87d056

type AdapterContent

type AdapterContent struct {
	// contains filtered or unexported fields
}

AdapterContent 使用内容实现 Adapter 接口。 配置内容支持的编码类型与 `gjson` 包中的编码类型相同。 md5:81e39ba9e6de51fa

func NewAdapterContent

func NewAdapterContent(content ...string) (*AdapterContent, error)

NewAdapterContent 返回一个使用自定义内容的新配置管理对象。 参数 `content` 指定用于读取的默认配置内容。 md5:efafcabf61d7087b

func (*AdapterContent) Available

func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)

可用性检查并返回后端配置服务是否可用。 可选参数 `resource` 指定特定的配置资源。

请注意,此函数不会返回错误,因为它只是简单地检查后端配置服务。 md5:79f955eb2fcdd137

func (*AdapterContent) Data

func (a *AdapterContent) Data(ctx context.Context) (data map[string]interface{}, err error)

Data 获取并以映射的形式返回当前资源中的所有配置数据。 注意,如果配置数据量过大,此函数可能会占用大量内存。 如有需要,你可以根据实际情况实现这个函数。 md5:19dfa88d9aa6ece5

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" 用于切片中的条目。 md5:39b9171603468968

func (*AdapterContent) SetContent

func (a *AdapterContent) SetContent(content string) error

SetContent 为指定的`file`设置自定义配置内容。 `file`是可选参数,默认值为DefaultConfigFile。 md5:49ae38cf671e3b96

type AdapterFile

type AdapterFile struct {
	// contains filtered or unexported fields
}

AdapterFile 实现了使用文件的 Adapter 接口。 md5:c0f0e0b1d4b217fd

func NewAdapterFile

func NewAdapterFile(file ...string) (*AdapterFile, error)

NewAdapterFile 返回一个新的配置管理对象。 参数 `file` 指定了默认的配置文件读取名称。 md5:52ab633a98562ceb

func (*AdapterFile) AddPath

func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)

AddPath 向搜索路径中添加一个绝对或相对的`目录`路径。

请注意,此参数是目录路径,而不是文件路径。 md5:25c79c7444dc4e16

func (*AdapterFile) Available

func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool

可用检查并返回给定`file`的配置是否可用。 md5:d915d3cb575cbd5b

func (*AdapterFile) Clear

func (a *AdapterFile) Clear()

Clear 清除所有解析的配置文件内容缓存,这将强制重新从文件加载配置内容。 md5:5868c636ce62cb14

func (*AdapterFile) ClearContent

func (a *AdapterFile) ClearContent()

ClearContent 清除所有全局配置内容。 md5:89d91d7f2a05e7da

func (*AdapterFile) Data

func (a *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error)

Data 获取并以映射类型返回所有配置数据。 md5:2a92e8bbe7388f01

func (*AdapterFile) Dump

func (a *AdapterFile) Dump()

Dump 打印当前的Json对象,使其更便于人工阅读。 md5:c8c6bbdb40fa6383

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`指定的默认值。 md5:8a88d01912ac6218

func (*AdapterFile) GetContent

func (a *AdapterFile) GetContent(file ...string) string

GetContent 为指定的 `file`(默认为DefaultConfigFile)返回自定义配置内容。 `file` 参数是不必要的。 md5:633823fe81267232

func (*AdapterFile) GetFileName

func (a *AdapterFile) GetFileName() string

GetFileName 返回默认的配置文件名。 md5:d13e3bd27526f03d

func (*AdapterFile) GetFilePath

func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error)

GetFilePath 通过 `file` 参数返回给定文件名的绝对配置文件路径。 如果没有传递 `file`,则返回默认名称的配置文件路径。 如果给定的 `file` 不存在,它将返回一个空的 `path` 字符串和一个错误。 md5:b116b9d063e12bc9

func (*AdapterFile) GetPaths

func (a *AdapterFile) GetPaths() []string

GetPaths 返回当前配置管理器的搜索目录路径数组。 md5:c77738d1ef96cc99

func (*AdapterFile) MustGet

func (a *AdapterFile) MustGet(ctx context.Context, pattern string) *gvar.Var

MustGet 行为类似于函数 Get,但如果发生错误时会引发 panic。 md5:b1d3af83a52fd248

func (*AdapterFile) RemoveContent

func (a *AdapterFile) RemoveContent(file ...string)

RemoveContent 移除具有指定`file`的全局配置。如果未传递`name`,则删除默认组名的配置。 md5:93cf717e8dc7670b

func (*AdapterFile) SetContent

func (a *AdapterFile) SetContent(content string, file ...string)

SetContent 为指定的`file`设置自定义配置内容。 `file`是可选参数,默认值为DefaultConfigFile。 md5:49ae38cf671e3b96

func (*AdapterFile) SetFileName

func (a *AdapterFile) SetFileName(name string)

SetFileName 设置默认的配置文件名。 md5:b540171ead70ddf8

func (*AdapterFile) SetPath

func (a *AdapterFile) SetPath(directoryPath string) (err error)

SetPath 设置文件搜索的配置`目录`路径。 参数 `path` 可以是绝对或相对的`目录`路径, 但强烈建议使用绝对`目录`路径。

注意,此参数是一个指向目录的路径,而不是指向文件的路径。 md5:56f162e4bbfc634d

func (*AdapterFile) SetViolenceCheck

func (a *AdapterFile) SetViolenceCheck(check bool)

SetViolenceCheck 设置是否执行层次冲突检查。 当键名中包含级别符号时,需要启用此功能。默认情况下禁用。

注意,开启此功能的开销较大,并不建议在键名中使用分隔符。最好在应用层面上避免这种情况。 md5:5009f694ccd4efc0

func (*AdapterFile) X设置值

func (a *AdapterFile) X设置值(pattern string, value interface{}) error

X设置值 使用指定的 `pattern` 设置值。 它支持通过字符分隔符(默认为`.`)进行层次数据访问。 这通常用于在运行时更新特定配置值。 请注意,不建议在运行时使用 `X设置值` 配置,因为如果底层配置文件更改,配置会自动刷新。 md5:65992c2815af747e

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config 是配置管理对象。 md5:1ef57338c678e400

func X创建

func X创建() (*Config, error)

X创建 创建并返回一个 Config 对象,其默认适配器为 AdapterFile。 md5:52cd678118524272

func X创建并按适配器

func X创建并按适配器(适配器 Adapter) *Config

X创建并按适配器使用给定的适配器创建并返回一个Config对象。 md5:9ddaae0ddb0e0297

func X取单例对象

func X取单例对象(名称 ...string) *Config

X取单例对象 返回一个具有默认设置的 Config 实例。 参数 `name` 是该实例的名称。但请注意,如果配置目录中存在文件 "name.toml", 则将其设置为默认配置文件。TOML 文件类型是默认的配置文件类型。 md5:4164ff567a8c8c31

func (*Config) X取Map

func (c *Config) X取Map(上下文 context.Context) (值 map[string]interface{}, 错误 error)

X取Map 获取并以映射类型返回所有配置数据。 md5:2a92e8bbe7388f01

func (*Config) X取MapPANI

func (c *Config) X取MapPANI(上下文 context.Context) map[string]interface{}

X取MapPANI 行为类似于函数 Data,但如果发生错误则会引发恐慌。 md5:eb72c1ce036d70b6

func (*Config) X取值

func (c *Config) X取值(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*gvar.Var, error)

X取值 通过指定的`pattern`获取并返回值。 如果`pattern`为空字符串或".",它将返回当前Json对象的所有值。 如果根据`pattern`没有找到值,它将返回nil。

如果没有为`pattern`找到值,它将返回由`def`指定的默认值。 md5:b10a106fb9d6af41

func (*Config) X取值PANI

func (c *Config) X取值PANI(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *gvar.Var

X取值PANI 行为类似于函数 Get,但如果发生错误时会引发 panic。 md5:b1d3af83a52fd248

func (*Config) X取值并从启动命令

func (c *Config) X取值并从启动命令(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*gvar.Var, error)

X取值并从启动命令 根据模式 `pattern` 返回配置值。 如果找不到配置值,它将检索并返回由 `key` 指定的命令行选项。 如果它们都不存在,则返回默认值 `def`。

获取规则:命令行参数采用小写格式,例如:gf.package.variable。 md5:2a77887f42041d88

func (*Config) X取值并从启动命令PANI_有bug

func (c *Config) X取值并从启动命令PANI_有bug(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *gvar.Var

X取值并从启动命令PANI_有bug 的行为类似于 GetWithCmd 函数,但如果发生错误,它会直接 panic。 md5:683d24a1f4aceb7b

func (*Config) X取值并从环境变量

func (c *Config) X取值并从环境变量(上下文 context.Context, 表达式 string, 默认值 ...interface{}) (*gvar.Var, error)

X取值并从环境变量 根据模式`pattern`返回配置值。 如果配置值不存在,那么它会获取并返回由`key`指定的环境变量值。 如果两者都不存在,则返回默认值`def`。

获取规则:环境变量参数以大写格式表示,例如:GF_PACKAGE_VARIABLE。 md5:d533293fbfbf6350

func (*Config) X取值并从环境变量PANI

func (c *Config) X取值并从环境变量PANI(上下文 context.Context, 表达式 string, 默认值 ...interface{}) *gvar.Var

X取值并从环境变量PANI 作为 GetWithEnv 函数的行为,但如果发生错误,它会引发恐慌。 md5:9f816c41440b51cf

func (*Config) X取适配器

func (c *Config) X取适配器() Adapter

X取适配器 返回当前Config对象的适配器。 md5:46c003ab367518d8

func (*Config) X是否可用

func (c *Config) X是否可用(上下文 context.Context, resource ...string) (可用 bool)

可用性检查并返回配置服务是否可用。 可选参数 `pattern` 指定某些配置资源。

如果默认AdapterFile中存在配置文件,则返回true,否则返回false。 请注意,此函数不会返回错误,因为它只是简单地检查后端配置服务。 md5:771d98d194158bc1

func (*Config) X设置适配器

func (c *Config) X设置适配器(适配器 Adapter)

X设置适配器 设置当前 Config 对象的适配器。 md5:8d00d377baafeb01

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL