advconf

package
v0.0.0-...-575c38c Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Directory 默认配置目录
	Directory = "config"
	// EnvDirectory 环境变量指定额外的配置目录
	EnvDirectory = "ADVCONF_DIR"
)

Variables

View Source
var (
	// Embed 允许从外部注入一个 embed.FS 用以让 advconf 读取内嵌文件
	Embed *embed.FS
)

Functions

func JoinKey

func JoinKey(keys ...string) string

func PrefixKey

func PrefixKey(pfx string, keys ...string) string

func Render

func Render(tpl []byte) (out []byte, err error)

func SplitKey

func SplitKey(key string) []string

Types

type Duration

type Duration time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(bytes []byte) error

func (Duration) Unwrap

func (d Duration) Unwrap() time.Duration

type FSLoader

type FSLoader struct {
	Filesystems []fs.FS
	Formats     []Format
}

func NewFSLoader

func NewFSLoader(filesystems ...fs.FS) *FSLoader

func (*FSLoader) FindFormat

func (l *FSLoader) FindFormat(name string) Format

func (*FSLoader) Load

func (l *FSLoader) Load(ctx context.Context, out interface{}, key string) (found bool, err error)

Load 从指定目录加载配置文件,会尝试 JSON, YAML 等支持的文件格式 注意,待反序列化的 struct 只需要添加 JSON 标注即可,advconf 会将所有配置语言最终都转化为 JSON 格式,再进行反序列化

func (*FSLoader) LoadData

func (l *FSLoader) LoadData(ctx context.Context) (data map[string]interface{}, err error)

type Format

type Format interface {
	Name() string
	Extensions() []string
	Load(ctx context.Context, filename string, buf []byte, out *map[string]interface{}) (err error)
}

Format 将各种配置文件转换为 map[string]interface{},以便于最终以 JSON 格式反序列化 struct

type Loader

type Loader interface {
	// Load 载入配置,并反序列化到一个结构体上,会从所有可能的数据源(内嵌文件,当前工作目录的 config 文件夹,环境变量指定的其他目录 等)
	// 加载数据,从 key 路径获取 map[string]interface{},最终以 JSON 格式反序列化到 out 上。注意,Load 方法同时会执行 validator 和
	// defaults 两个工具
	Load(ctx context.Context, out interface{}, key string) (found bool, err error)
}

Loader 配置载入工具

func DefaultLoader

func DefaultLoader() Loader

DefaultLoader 创建一个默认 Loader

type Validatable

type Validatable interface {
	Validate() error
}

Jump to

Keyboard shortcuts

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