Documentation
¶
Overview ¶
Package conf reads configuration from any format file, including Java properties, yaml, toml, etc.
Index ¶
- Variables
- func BindValue(p *Properties, v reflect.Value, param BindParam) error
- func IsPrimitiveValueType(t reflect.Type) bool
- func IsValueType(t reflect.Type) bool
- func RegisterConverter(fn interface{})
- func RegisterReader(r Reader, ext ...string)
- func RegisterSplitter(name string, fn Splitter)
- type BindOption
- type BindParam
- type Converter
- type GetOption
- type ParsedTag
- type Properties
- func (p *Properties) Bind(i interface{}, opts ...BindOption) error
- func (p *Properties) Bytes(b []byte, ext string) error
- func (p *Properties) Get(key string, opts ...GetOption) string
- func (p *Properties) Has(key string) bool
- func (p *Properties) Keys() []string
- func (p *Properties) Load(file string) error
- func (p *Properties) Resolve(s string) (string, error)
- func (p *Properties) Set(key string, val interface{}) error
- type Reader
- type Splitter
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotExist = errors.New("not exist")
)
Functions ¶
func IsPrimitiveValueType ¶
IsPrimitiveValueType 返回是否是原生值类型。首先,什么是值类型?在发生赋值时,如 果传递的是数据本身而不是数据的引用,则称这种类型为值类型。那什么是原生值类型?所谓原 生值类型是指 golang 定义的 26 种基础类型里面符合值类型定义的类型。罗列下来,就是说 Bool、Int、Int8、Int16、Int32、Int64、Uint、Uint8、Uint16、Uint32、Uint64、 Float32、Float64、Complex64、Complex128、String、Struct 这些基础数据类型都 是值类型。当然,需要特别说明的是 Struct 类型必须在保证所有字段都是值类型的时候才是 值类型,只要有不是值类型的字段就不是值类型。
func IsValueType ¶
IsValueType 返回是否是 value 类型。除了原生值类型,它们的集合类型也是值类型,但 是仅限于一层复合结构,即 []string、map[string]struct 这种,像 [][]string 则 不是值类型,map[string]map[string]string 也不是值类型,因为程序开发过程中,配 置项应当越明确越好,而多层次嵌套结构显然会造成信息的不明确,因此不能是值类型。
func RegisterReader ¶
RegisterReader 注册属性列表解析器,ext 是解析器支持的文件扩展名。
Types ¶
type BindOption ¶
type BindOption func(arg *bindArg)
type BindParam ¶
type ParsedTag ¶
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 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) 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 的配置文件是补充 关系,而不是替换关系,这一条原则我也经常会搞混,尤其在和其他配置库相比较的时候。