config

package
v0.0.0-...-961a225 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 22 Imported by: 0

README

Config

kubernetes

go get -u github.com/go-kratos/kratos/contrib/config/kubernetes/v2

apollo

go get -u github.com/go-kratos/kratos/contrib/config/apollo/v2

etcd

go get -u github.com/go-kratos/kratos/contrib/config/etcd/v2

nacos

go get -u github.com/go-kratos/kratos/contrib/config/nacos/v2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("key not found")

ErrNotFound 表示在配置中未找到指定键。

Functions

This section is empty.

Types

type Config

type Config interface {
	Load() error                        // 加载配置
	Scan(v interface{}) error           // 将配置解析到目标结构体
	Value(key string) Value             // 获取指定键的配置值
	Watch(key string, o Observer) error // 监听指定键的变化
	Close() error                       // 关闭配置监听器
}

Config 是配置接口。

func New

func New(opts ...Option) Config

New 创建一个配置实例并应用选项。

type Decoder

type Decoder func(*KeyValue, map[string]interface{}) error

Decoder 是配置的解码器类型,接受一个 *KeyValue 和目标 map,进行解码。

type KeyValue

type KeyValue struct {
	// Key 是配置的键。
	Key string
	// Value 是配置的值,以字节切片的形式存储。
	Value []byte
	// Format 是配置值的格式,例如 JSON、YAML 等。
	Format string
}

KeyValue 是配置的键值对。

type Merge

type Merge func(dst, src interface{}) error

Merge 是配置的合并函数类型,用于将源配置合并到目标配置中。

type Observer

type Observer func(string, Value)

Observer 是配置观察者的类型定义。

type Option

type Option func(*options)

Option 是配置的选项函数,用于设置 options。

func WithDecoder

func WithDecoder(d Decoder) Option

WithDecoder 设置自定义的配置解码器。 默认解码器行为: 如果 KeyValue.Format 不为空,则使用指定的格式解码 Value 为 map[string]interface{}。 如果 KeyValue.Format 为空,则直接存储 {KeyValue.Key : KeyValue.Value}。

func WithMergeFunc

func WithMergeFunc(m Merge) Option

WithMergeFunc 设置自定义的合并函数。

func WithResolveActualTypes

func WithResolveActualTypes(enableConvertToType bool) Option

WithResolveActualTypes 配置占位符解析器,启用将配置值转换为实际数据类型的功能。

func WithResolver

func WithResolver(r Resolver) Option

WithResolver 设置自定义的占位符解析器。

func WithSource

func WithSource(s ...Source) Option

WithSource 设置配置来源。

type Reader

type Reader interface {
	Merge(...*KeyValue) error   // 合并配置数据
	Value(string) (Value, bool) // 获取指定路径的配置值
	Source() ([]byte, error)    // 获取所有配置的 JSON 表示
	Resolve() error             // 执行配置解析器
}

Reader 是配置读取器接口。

type Resolver

type Resolver func(map[string]interface{}) error

Resolver 是占位符解析器类型,用于解析配置中的占位符。

type Source

type Source interface {
	// Load 方法从配置源加载配置键值对。
	Load() ([]*KeyValue, error)
	// Watch 方法创建一个监控器,用于监控配置源的变化。
	Watch() (Watcher, error)
}

Source 是配置源的接口。

type Value

type Value interface {
	Bool() (bool, error)
	Int() (int64, error)
	Float() (float64, error)
	String() (string, error)
	Duration() (time.Duration, error)
	Slice() ([]Value, error)
	Map() (map[string]Value, error)
	Scan(interface{}) error
	Load() interface{}
	Store(interface{})
}

Value 是配置值的接口

type Watcher

type Watcher interface {
	// Next 方法等待并返回配置源的下一次变化。
	Next() ([]*KeyValue, error)
	// Stop 方法停止监控器。
	Stop() error
}

Watcher 是监控器的接口,用于监控配置源的变化。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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