config

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 20 Imported by: 1

Documentation

Overview

Package config 提供统一的配置管理接口和实现。 支持从多种配置源(文件、etcd、consul、nacos)加载和监听配置项。

Package config 提供统一的配置管理接口和实现。 支持从多种配置源(文件、etcd、consul、nacos)加载和监听配置项。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close 关闭配置监听

func Get

func Get(pattern string, def ...any) value.Value

Get 获取配置值

func Has

func Has(pattern string) bool

Has 检测多个匹配规则中是否存在配置

func Set

func Set(pattern string, value any) error

Set 设置配置值

func SetConfigurator

func SetConfigurator(configurator Configurator)

SetConfigurator 设置配置器

func SetConfiguratorWithSources

func SetConfiguratorWithSources(sources ...Source)

SetConfiguratorWithSources 通过设置配置源来设置配置器

func SetContextProvider added in v1.0.9

func SetContextProvider(provider ContextProvider)

SetContextProvider 设置 Context 提供者(由 dawn 包调用)

func Store

func Store(ctx context.Context, source string, file string, content any, override ...bool) error

Store 保存配置项

func Watch

func Watch(cb WatchCallbackFunc, names ...string)

Watch 设置监听回调

Types

type Configuration

type Configuration struct {
	Path     string // 文件路径
	File     string // 文件全称
	Name     string // 文件名称
	Format   string // 文件格式
	Content  []byte // 文件内容
	FullPath string // 文件全路径
	// contains filtered or unexported fields
}

Configuration 配置项

func Load

func Load(ctx context.Context, source string, file ...string) ([]*Configuration, error)

Load 加载配置项

func (*Configuration) Decode

func (c *Configuration) Decode() (any, error)

Decode 解码

func (*Configuration) Scan

func (c *Configuration) Scan(dest any) error

Scan 扫描

type Configurator

type Configurator interface {
	// Has 检测多个匹配规则中是否存在配置
	Has(pattern string) bool
	// Get 获取配置值
	Get(pattern string, def ...any) value.Value
	// Set 设置配置值
	Set(pattern string, value any) error
	// Match 匹配多个规则
	Match(patterns ...string) Matcher
	// Watch 设置监听回调
	Watch(cb WatchCallbackFunc, names ...string)
	// Load 加载配置项
	Load(ctx context.Context, source string, file ...string) ([]*Configuration, error)
	// Store 保存配置项
	Store(ctx context.Context, source string, file string, content any, override ...bool) error
	// Close 关闭配置监听
	Close()
}

func GetConfigurator

func GetConfigurator() Configurator

GetConfigurator 获取配置器 优先从 Context 获取,如果没有关联 Context 则使用全局变量

func NewConfigurator

func NewConfigurator(opts ...Option) Configurator

type ContextProvider added in v1.0.9

type ContextProvider interface {
	// Configurator 获取配置器
	Configurator() Configurator
}

ContextProvider Context 提供者接口,用于避免循环依赖

func GetContextProvider added in v1.0.9

func GetContextProvider() ContextProvider

GetContextProvider 获取 Context 提供者

type Decoder

type Decoder func(format string, content []byte) (any, error)

type Encoder

type Encoder func(format string, content any) ([]byte, error)

type Matcher

type Matcher interface {
	// Has 检测多个匹配规则中是否存在配置
	Has() bool
	// Get 获取配置值
	Get(def ...any) value.Value
	// Scan 扫描读取配置值
	Scan(dest any) error
}

func Match

func Match(patterns ...string) Matcher

Match 匹配多个规则

type Mode

type Mode string

Mode 配置源的模式:只读、只写或读写

const (
	ReadOnly  Mode = "read-only"  // 只读
	WriteOnly Mode = "write-only" // 只写
	ReadWrite Mode = "read-write" // 读写
)

type Option

type Option func(o *options)

func WithContext

func WithContext(ctx context.Context) Option

WithContext 设置上下文

func WithDecoder

func WithDecoder(decoder Decoder) Option

WithDecoder 设置解码器

func WithEncoder

func WithEncoder(encoder Encoder) Option

WithEncoder 设置编码器

func WithSources

func WithSources(sources ...Source) Option

WithSources 设置配置源

type Scanner

type Scanner func(format string, content []byte, dest any) error

type Source

type Source interface {
	// Name 配置源名称
	Name() string
	// Load 加载配置项
	Load(ctx context.Context, file ...string) ([]*Configuration, error)
	// Store 保存配置项
	Store(ctx context.Context, file string, content []byte) error
	// Watch 监听配置项
	Watch(ctx context.Context) (Watcher, error)
	// Close 关闭配置源
	Close() error
}

Source 配置源接口,定义了配置的加载、存储和监听功能。 实现该接口可以支持不同的配置后端(文件系统、etcd、consul、nacos 等)。

type WatchCallbackFunc

type WatchCallbackFunc func(names ...string)

type Watcher

type Watcher interface {
	// Next 返回配置列表
	Next() ([]*Configuration, error)
	// Stop 停止监听
	Stop() error
}

Watcher 配置监听器接口,用于监听配置变化。

Directories

Path Synopsis
nacos module

Jump to

Keyboard shortcuts

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