props

package module
v0.0.0-...-4418dea Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

README

概要

Build Status GoDoc Language License codecov goreport

props 用于使用统一的接口来获取可能有多个来源的配置数据。他通过配置数据源优先级的来决定多个来源的同名配置项的取值。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Props

type Props interface {
	//	Add 用于将配置数据源 s,以优先级 priority,添加到 Props 对象中去.
	Add(priority uint8, s Source) Source

	//	Find 函数尝试从多个配置数据源查找名字为 key 的配置项.
	//	如果能够找到返回找到的配置项的值和true;否则,第二个返回值为false。
	//	如果多个配置数据源都存在名字为 key 的配置项,那么以优先级最高的配置项为准。
	//	String、Int64、Uint64、Bool 等函数是对 Find 函数的简单封装。
	Find(key string) (string, bool)

	//	String 尝试将找到的配置项以 string 的形式返回,如果找不到就返回 def.
	String(key string, def string) string

	//	Int64 尝试将找到的配置项转换为 int64 的形式,如果找不到或者转换失败,就返回 def.
	Int64(key string, def int64) int64

	//	Uint64 尝试将找到的配置项转换为 uint64 的形式,如果找不到或者转换失败,就返回 def.
	Uint64(key string, def uint64) uint64

	//	Bool 尝试将找到的配置项转换为 bool 的形式,如果找不到或者转换失败,就返回 def.
	//	当前支持的数据映射方式为:
	// 	"1", "t", "T", "true", "TRUE", "True" 会被映射为 true;
	//	"0", "f", "F", "false", "FALSE", "False" 会被映射为 false。
	//	如果配置项的值为其他的值,作为转换失败处理。
	Bool(key string, def bool) bool

	//	将字符串 s 中的所有 ${key} 形式的可扩展变量,替换为本 Props 对象内部的配置项 key 的值,并最终返回替换后的结果.
	//	如果某个 key 的配置项在 Props 对象中不存在,将原样保留 ${key};
	//	如果某个 key 的配置项的值里面还含有 ${xxx},那么也会自动展开;
	//	如果变量存在循环引用现象,返回失败;
	Expand(s string) (string, error)
}

Props 定义了配置数据的操作集合. 该接口提供了添加配置数据源,配置项查找,配置项读取,使用配置项扩展变量等多个函数.

func NewProps

func NewProps() Props

NewProps 用于创建一个新的 Props 对象.

type Source

type Source interface {
	Find(key string) (string, bool)
}

Source 定义了配置数据来源. 一个数据来源必须提供一个 Find 方法用于支持从该来源读取配置数据

type SourceCLI

type SourceCLI struct {
	SourceCustom
}

SourceCLI 用于将命令行输入参数作为配置数据源.

func NewSourceCLI

func NewSourceCLI() *SourceCLI

NewSourceCLI 创建一个 SourceCLI 对象.

func (*SourceCLI) Accept

func (s *SourceCLI) Accept(prefix string)

Accept 将逐个扫描所有的命令行的输入参数,并将以 `prefix` 为前缀的参数作为配置数据源的配置项. 如果有多个前缀,那么可以调用 `Accept函数多次`。

type SourceCustom

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

SourceCustom 表示用户自定义的配置数据源.

func NewSourceCustom

func NewSourceCustom() *SourceCustom

NewSourceCustom 创建一个 SourceCustom 的数据源对象.

func (SourceCustom) Find

func (s SourceCustom) Find(key string) (string, bool)

Find 为 Source 的接口实现,具体功能参见 `Source`.

func (*SourceCustom) Set

func (s *SourceCustom) Set(key string, val string)

Set 用于将 key 和 value 放入 SourceCustom 对象中,方便后续通过 Props 的接口获取.

type SourceProperties

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

func NewSourceProperties

func NewSourceProperties() *SourceProperties

func (SourceProperties) Find

func (s SourceProperties) Find(key string) (string, bool)

func (*SourceProperties) Load

func (s *SourceProperties) Load(reader io.Reader) error

func (*SourceProperties) LoadFile

func (s *SourceProperties) LoadFile(filename string) error

Jump to

Keyboard shortcuts

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