config

package
v0.0.0-...-6f44a9b Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfigNotSupport = errors.New("config: not support")
	ErrProviderNotExist = errors.New("config: provider not exist")
	ErrCodecNotExist    = errors.New("config: codec not exist")
)

Functions

func GetInt

func GetInt(key string) (int, error)

GetInt 根据key获取Int类型的值

func GetIntWithDefault

func GetIntWithDefault(key string, def int) int

GetIntWithDefault 根据key获取Int类型的值,如果获取错误就使用 def 指定的默认值

func GetJSON

func GetJSON(key string, val interface{}) error

GetJSON 根据key获取Json类型复合数据

func GetString

func GetString(key string) (string, error)

GetString 根据key获取string类型的值

func GetStringWithDefault

func GetStringWithDefault(key, def string) string

GetStringWithDefault 根据 key 获取 string 类型的值,如果获取错误就使用 def 指定的默认值

func GetTOML

func GetTOML(key string, val interface{}) error

GetTOML 根据key获取TOML类型复合数据

func GetWithUnmarshal

func GetWithUnmarshal(key string, val interface{}, unmarshalName string) error

GetWithUnmarshal 根据key获取特定编码的数据结构

func GetYAML

func GetYAML(key string, val interface{}) error

GetYAML 根据key获取YAML类型复合数据

func Register

func Register(c KVConfig)

Register 注册kvconfig

func RegisterCodec

func RegisterCodec(c Codec)

RegisterCodec 注册编解码器

func RegisterProvider

func RegisterProvider(p DataProvider)

RegisterProvider 注册配置服务提供者组件

func RegisterUnmarshaler

func RegisterUnmarshaler(name string, us Unmarshaler)

RegisterUnmarshaler 注册Unmarshaler

func SetGlobalKV

func SetGlobalKV(kv KV)

SetGlobalKV 设置配置中心KV实例

Types

type Codec

type Codec interface {
	Name() string
	Unmarshal([]byte, interface{}) error
}

Codec 编解码器

func GetCodec

func GetCodec(name string) (Codec, error)

GetCodec 根据名字获取Codec

type Config

type Config interface {
	Load() error
	Reload()
	Get(string, interface{}) interface{}
	Unmarshal(interface{}) error
	IsSet(string) bool
	GetInt(string, int) int
	GetInt32(string, int32) int32
	GetInt64(string, int64) int64
	GetUint(string, uint) uint
	GetUint32(string, uint32) uint32
	GetUint64(string, uint64) uint64
	GetFloat32(string, float32) float32
	GetFloat64(string, float64) float64
	GetString(string, string) string
	GetBool(string, bool) bool
	Bytes() []byte
}

Config 配置通用接口

type DataProvider

type DataProvider interface {
	//TODO:add ability to watch
	Name() string
	Read(string) ([]byte, error)
	Watch(ProviderCallback)
}

DataProvider 通用内容源接口 通过实现Name、Read、Watch等方法,就能从任意的 内容源(file、TConf、ETCD、configmap)中读取配 置并通过编解码器解析为可处理的标准格式(JSON、TOML、YAML)等

func GetProvider

func GetProvider(name string) (DataProvider, error)

GetProvider 根据名字获取provider

type EventType

type EventType uint8

EventType 监听配置变更的事件类型

const (
	// EventTypeNull 空事件
	EventTypeNull EventType = 0
	// EventTypePut 设置或更新配置事件
	EventTypePut EventType = 1
	// EventTypeDel 删除配置项事件
	EventTypeDel EventType = 2
)

type FileProvider

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

FileProvider 从文件系统拉取文件内容

func (*FileProvider) Name

func (*FileProvider) Name() string

Name Provider名字

func (*FileProvider) Read

func (fp *FileProvider) Read(path string) ([]byte, error)

Read 读取指定文件

func (*FileProvider) Watch

func (fp *FileProvider) Watch(cb ProviderCallback)

Watch 注册文件变化处理函数

type JSONCodec

type JSONCodec struct{}

JSONCodec JSON codec

func (*JSONCodec) Name

func (*JSONCodec) Name() string

Name JSON codec

func (*JSONCodec) Unmarshal

func (c *JSONCodec) Unmarshal(in []byte, out interface{}) error

Unmarshal JSON decode

type JSONUnmarshaler

type JSONUnmarshaler struct{}

JSONUnmarshaler json解码

func (*JSONUnmarshaler) Unmarshal

func (ju *JSONUnmarshaler) Unmarshal(data []byte, val interface{}) error

Unmarshal json 解码

type KV

type KV interface {
	// Put 设置或更新配置项key对应的值
	Put(ctx context.Context, key, val string, opts ...Option) error
	// Get 获取配置项key对应的值
	Get(ctx context.Context, key string, opts ...Option) (Response, error)
	// Del 删除配置项key
	Del(ctx context.Context, key string, opts ...Option) error
}

KV 配置中心键值对接口

func GlobalKV

func GlobalKV() KV

GlobalKV 获取配置中心KV实例

type KVConfig

type KVConfig interface {
	KV
	Watcher
	Name() string
}

KVConfig kv配置

func Get

func Get(name string) KVConfig

Get 根据名字使用kvconfig

type Loader

type Loader interface {
	//TODO:complete the design of interface based on tconf. Passing a struct may not be a good choice
	Load(string) (Config, error)
	Reload(string) error
}

Loader 解析配置的通用接口

type Option

type Option func(*options)

Option 配置中心SDK选项

type ProviderCallback

type ProviderCallback func(string, []byte)

ProviderCallback provider内容变更事件回调函数

type Response

type Response interface {
	// Value 获取配置项对应的值
	Value() string
	// MetaData 额外元数据信息
	// 配置Option选项,可用于承载不同配置中心的额外功能实现,例如namespace,group,租约等概念
	MetaData() map[string]string
	// Event 获取Watch事件类型
	Event() EventType
}

Response 配置中心响应

type TomlCodec

type TomlCodec struct{}

TomlCodec toml codec

func (*TomlCodec) Name

func (*TomlCodec) Name() string

Name toml codec

func (*TomlCodec) Unmarshal

func (c *TomlCodec) Unmarshal(in []byte, out interface{}) error

Unmarshal toml decode

type TomlUnmarshaler

type TomlUnmarshaler struct{}

TomlUnmarshaler toml解码

func (*TomlUnmarshaler) Unmarshal

func (tu *TomlUnmarshaler) Unmarshal(data []byte, val interface{}) error

Unmarshal toml解码

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data []byte, value interface{}) error
}

Unmarshaler 配置内容解析抽象

func GetUnmarshaler

func GetUnmarshaler(name string) Unmarshaler

GetUnmarshaler 获取Unmarshaler

type Watcher

type Watcher interface {
	// Watch 监听配置项key的变更事件
	Watch(ctx context.Context, key string, opts ...Option) (<-chan Response, error)
}

Watcher 配置中心Watch事件接口

type YamlCodec

type YamlCodec struct{}

YamlCodec 解码Yaml

func (*YamlCodec) Name

func (*YamlCodec) Name() string

Name yaml codec

func (*YamlCodec) Unmarshal

func (c *YamlCodec) Unmarshal(in []byte, out interface{}) error

Unmarshal yaml decode

type YamlUnmarshaler

type YamlUnmarshaler struct{}

YamlUnmarshaler yaml解码

func (*YamlUnmarshaler) Unmarshal

func (yu *YamlUnmarshaler) Unmarshal(data []byte, val interface{}) error

Unmarshal yaml 解压

Jump to

Keyboard shortcuts

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