conf

package
v1.1.0-rc4 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package conf reads configuration from any format file, including Java properties, yaml, toml, etc.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist = errors.New("not exist")
)

Functions

func BindValue

func BindValue(p *Properties, v reflect.Value, param BindParam) error

func IsPrimitiveValueType

func IsPrimitiveValueType(t reflect.Type) bool

IsPrimitiveValueType 返回是否是原生值类型。首先,什么是值类型?在发生赋值时,如 果传递的是数据本身而不是数据的引用,则称这种类型为值类型。那什么是原生值类型?所谓原 生值类型是指 golang 定义的 26 种基础类型里面符合值类型定义的类型。罗列下来,就是说 Bool、Int、Int8、Int16、Int32、Int64、Uint、Uint8、Uint16、Uint32、Uint64、 Float32、Float64、Complex64、Complex128、String、Struct 这些基础数据类型都 是值类型。当然,需要特别说明的是 Struct 类型必须在保证所有字段都是值类型的时候才是 值类型,只要有不是值类型的字段就不是值类型。

func IsValueType

func IsValueType(t reflect.Type) bool

IsValueType 返回是否是 value 类型。除了原生值类型,它们的集合类型也是值类型,但 是仅限于一层复合结构,即 []string、map[string]struct 这种,像 [][]string 则 不是值类型,map[string]map[string]string 也不是值类型,因为程序开发过程中,配 置项应当越明确越好,而多层次嵌套结构显然会造成信息的不明确,因此不能是值类型。

func RegisterConverter

func RegisterConverter(fn interface{})

RegisterConverter 注册类型转换器。

func RegisterReader

func RegisterReader(r Reader, ext ...string)

RegisterReader 注册属性列表解析器,ext 是解析器支持的文件扩展名。

func RegisterSplitter

func RegisterSplitter(name string, fn Splitter)

RegisterSplitter 注册字符串分割器。

Types

type BindOption

type BindOption func(arg *bindArg)

func Key

func Key(key string) BindOption

Key 设置属性绑定使用的 key 。

func Tag

func Tag(tag string) BindOption

Tag 设置属性绑定使用的 tag 。

type BindParam

type BindParam struct {
	Type reflect.Type // 绑定对象的类型
	Key  string       // 完整的属性名
	Path string       // 绑定对象的路径
	Tag  ParsedTag    // 解析后的 tag
}

func (*BindParam) BindTag

func (param *BindParam) BindTag(tag string) error

type Converter

type Converter interface{}

Converter 类型转换器,函数原型为 func(string)(type,error)。

type GetOption

type GetOption func(arg *getArg)

func Def

func Def(v string) GetOption

Def 为 Get 方法设置默认值。

type ParsedTag

type ParsedTag struct {
	Key    string // 简短属性名
	Def    string // 默认值
	HasDef bool   // 是否具有默认值
	Split  string // 字符串分割器
}

func ParseTag

func ParseTag(tag string) (ret ParsedTag, err error)

ParseTag 解析 ${key:=def}|split 格式的字符串,然后返回 key 和 def 的值。

type Properties

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

Properties There are too many formats of configuration files, and too many conflicts between them. Each format of configuration file provides its special characteristics, but usually they are not all necessary, and complementary. For example, conf disabled Java properties' expansion when reading file, but it also provides similar function when getting properties. A good rule of thumb is that treating application configuration as a tree, but not all formats of configuration files designed like this or not ideal, such as Java properties which not strictly verified. Although configuration can store as a tree, but it costs more CPU time when getting properties because it reads property node by node. So conf uses a tree to strictly verify and a flat map to store.

提供创建和读取属性列表的方法。它使用扁平的 map[string]string 结 构存储数据,属性的 key 可以是 a.b.c 或者 a[0].b 两种形式,a.b.c 表示从 map 结构中获取属性值,a[0].b 表示从切片结构中获取属性值,并且 key 是大小写敏感的。

func Bytes

func Bytes(b []byte, ext string) (*Properties, error)

Bytes 返回一个由 []byte 创建的属性列表,ext 是文件扩展名,如 .yaml、.toml 等。

func Load

func Load(file string) (*Properties, error)

Load 返回一个由属性文件创建的属性列表,file 可以是绝对路径,也可以是相对路径。

func Map

func Map(m map[string]interface{}) *Properties

Map 返回一个由 map 创建的属性列表。

func New

func New() *Properties

New 返回一个空的属性列表。

func Read

func Read(r io.Reader, ext string) (*Properties, error)

Read 返回一个由 io.Reader 创建的属性列表,ext 是文件扩展名,如 .yaml、.toml 等。

func (*Properties) Bind

func (p *Properties) Bind(i interface{}, opts ...BindOption) error

Bind 将 key 对应的属性值绑定到某个数据类型的实例上。i 必须是一个指针,只有这 样才能将修改传递出去。Bind 方法使用 tag 字符串对数据实例进行属性绑定,其语法 为 value:"${a:=b}",其中 value 表示属性绑定,${} 表示属性引用,a 表示属性 的名称,:=b 表示为属性设置默认值。而且 tag 字符串还支持在默认值中进行嵌套引用 ,即 ${a:=${b}}。当然,还有两点需要特别说明: 一是对 array、slice、map、struct 这些复合类型不能设置非空默认值,因为如果 默认值太长会影响阅读体验,而且解析起来也并不容易; 二是可以省略属性名而只有默认值,即 ${:=b},原因是某些情况下属性名可能没想好或 者不太重要,比如,得益于字符串差值的实现,这种语法可以用于动态生成新的属性值, 也有人认为这是一种对 Golang 缺少默认值语法的补充,Bug is Feature。

func (*Properties) Bytes

func (p *Properties) Bytes(b []byte, ext string) error

Bytes 从 []byte 加载属性列表,ext 是文件扩展名,如 .yaml、.toml 等。该方法会覆 盖已有的属性值。

func (*Properties) Get

func (p *Properties) Get(key string, opts ...GetOption) string

Get 获取 key 对应的属性值,注意 key 是大小写敏感的。当 key 对应的属性值存在时, 或者 key 对应的属性值不存在但设置了默认值时,Get 方法返回 string 类型的数据, 当 key 对应的属性值不存在且没有设置默认值时 Get 方法返回 nil。因此可以通过判断 Get 方法的返回值是否为 nil 来判断 key 对应的属性值是否存在。

func (*Properties) Has

func (p *Properties) Has(key string) bool

Has 返回属性 key 是否存在。

func (*Properties) Keys

func (p *Properties) Keys() []string

Keys 返回所有属性 key 的列表。

func (*Properties) Load

func (p *Properties) Load(file string) error

Load 从属性文件加载属性列表,file 可以是绝对路径,也可以是相对路径。该方法会覆盖 已有的属性值。

func (*Properties) Resolve

func (p *Properties) Resolve(s string) (string, error)

Resolve 解析字符串中包含的所有属性引用即 ${key:=def} 的内容,并且支持递归引用。

func (*Properties) Set

func (p *Properties) Set(key string, val interface{}) error

Set 设置 key 对应的属性值,如果 key 对应的属性值已经存在则 Set 方法会覆盖旧 值。Set 方法除了支持 string 类型的属性值,还支持 int、uint、bool 等其他基础 数据类型的属性值。特殊情况下,Set 方法也支持 slice 、map 与基础数据类型组合构 成的属性值,其处理方式是将组合结构层层展开,可以将组合结构看成一棵树,那么叶子结 点的路径就是属性的 key,叶子结点的值就是属性的值。注意: conf 的配置文件是补充 关系,而不是替换关系,这一条原则我也经常会搞混,尤其在和其他配置库相比较的时候。

type Reader

type Reader func(b []byte) (map[string]interface{}, error)

Reader 属性列表解析器,将字节数组解析成 map 数据。

type Splitter

type Splitter func(string) ([]string, error)

Splitter 字符串分割器,用于将字符串按逗号分割成字符串切片。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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