fig

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 License: Apache-2.0 Imports: 12 Imported by: 22

README

fig

fig是一个轻量化的配置读取工具

安装

go get github.com/xfali/fig

使用

加载配置内容
config := fig.New()
err := config.ReadValue(strings.NewReader(test_ctx_str))
if err != nil {
    b.Fatal(err)
}

或者

config, err := fig.LoadJsonFile("config.json")
if err != nil {
    t.Fatal(err)
}
config, err := fig.LoadYamlFile("config.yaml")
if err != nil {
    t.Fatal(err)
}
通过key获取属性值(字符串)
v := config.Get("DataSources.default.DriverName", "")
通过key获得反序列化对象
port := 0
err = config.GetValue("ServerPort", &port)

读取环境变量

使用模板函数env读取环境变量:

  • 如果env参数为1个,如环境变量不存在则返回错误
  • 如果env参数为2个,如环境变量不存在则赋值为第二个参数(默认值)
DataSources:
  default:
    DriverNameGet0: "{{ env "CONTEXT_TEST_ENV" }}"
    DriverNameGet1: "{{ env "CONTEXT_TEST_ENV" "func1_return" }}"

也可以在配置中使用{{.Env.ENV_NAME}}来配置读取环境变量值,fig在加载时自动使用ENV_NAME的值替换相应内容:

  • 如环境变量不存在则返回错误
DataSources:
  default:
    DriverName: "{{.Env.CONTEXT_TEST_ENV}}"

工具方法

方法 说明
fig.GetString 获得string类型属性值
fig.GetBool 获得bool类型属性值
fig.GetInt 获得int类型属性值
fig.GetUint 获得uint类型属性值
fig.GetInt64 获得int64类型属性值
fig.GetUint64 获得uint64类型属性值
fig.GetFloat32 获得float32类型属性值
fig.GetFloat64 获得float64类型属性值

用法:

config, _ := fig.LoadJsonFile("config.json")

v := fig.GetBool(config)("LogResponse", false)

floatValue := fig.GetFloat32(config)("Value.float", 0)

tag

属性值tag

fig提供直接填充struct的field的方法,使用tag:"fig"来标识属性名称:

type TestStruct struct {
	dummy1      int
	Port        int  `fig:"ServerPort"`
	LogResponse bool `fig:"LogResponse"`
	dummy2      int
	FloatValue  float32 `fig:"Value.float"`
	DriverName  string  `fig:"DataSources.default.DriverName"`
	dummy3      int
}
属性前缀tag

可以使用tag:"figPx"表明属性的前缀,在此之后的所有fig tag都会自动增加此前缀:

type TestStruct2 struct {
	x           string `figPx:"DataSources.default"`
	MaxIdleConn int
	DvrName     string `fig:"DriverName"`
}

使用fig.Fill方法根据tag填充struct:

config, _ := fig.LoadJsonFile("config.json")
test := TestStruct{}
err := fig.Fill(config, &test)
t.log(test)

使用限制

目前不允许使用包含“-”的名称作为field,否则无法正常解析(请使用下划线“_”代替)。

Documentation

Index

Constants

View Source
const (
	TagPrefixName = "figPx"
	TagName       = "fig"
)

Variables

This section is empty.

Functions

func Fill

func Fill(prop Properties, result interface{}) error

param: prop 属性 param: result 填充的struct result: result如果不为struct的指针返回错误,填充时异常返回错误

func FillEx added in v0.0.2

func FillEx(prop Properties, result interface{}, withField bool) error

param: prop 属性 param: result 填充的struct param: withField 是否根据field name填充 result: result如果不为struct的指针返回错误,填充时异常返回错误

func FillExWithTagName added in v0.0.3

func FillExWithTagName(prop Properties, result interface{}, withField bool, tagPxName, tagName string) error

param: prop 属性 param: result 填充的struct param: withField 是否根据field name填充 param: tagPxName tag前缀名,后续都使用tagPxName定义的名称做前缀 param: tagName tag名 result: result如果不为struct的指针返回错误,填充时异常返回错误

func FillExWithTagNames added in v0.1.3

func FillExWithTagNames(prop Properties, result interface{}, withField bool, tagPxNames, tagNames []string) error

param: prop 属性 param: result 填充的struct param: withField 是否根据field name填充 param: tagPxNames tag前缀名,后续都使用tagPxName定义的名称做前缀 param: tagNames tag名 result: result如果不为struct的指针返回错误,填充时异常返回错误

func GetBool

func GetBool(props Properties) func(key string, defaultValue bool) bool

func GetEnvs added in v0.1.1

func GetEnvs() map[string]string

func GetFloat32

func GetFloat32(props Properties) func(key string, defaultValue float32) float32

func GetFloat64

func GetFloat64(props Properties) func(key string, defaultValue float64) float64

func GetInt

func GetInt(props Properties) func(key string, defaultValue int) int

func GetInt64

func GetInt64(props Properties) func(key string, defaultValue int64) int64

func GetString

func GetString(props Properties) func(key string, defaultValue string) string

func GetUint

func GetUint(props Properties) func(key string, defaultValue uint) uint

func GetUint64

func GetUint64(props Properties) func(key string, defaultValue uint64) uint64

func SetLog added in v0.1.1

func SetLog(log logFunc)

配置fig的内置log

Types

type DefaultProperties

type DefaultProperties struct {
	Value *Value
	Env   map[string]string
	// contains filtered or unexported fields
}

func New

func New(opts ...Opt) *DefaultProperties

func (*DefaultProperties) ExecTemplate

func (ctx *DefaultProperties) ExecTemplate(r io.Reader) (io.Reader, error)

func (*DefaultProperties) Get

func (ctx *DefaultProperties) Get(key string, defaultValue string) string

A.B.C

func (*DefaultProperties) GetValue

func (ctx *DefaultProperties) GetValue(key string, result interface{}) error

依赖于ValueReader的序列化和反序列化方式

func (*DefaultProperties) ReadValue added in v0.1.1

func (ctx *DefaultProperties) ReadValue(r io.Reader) error

func (*DefaultProperties) SetValueLoader added in v0.1.1

func (ctx *DefaultProperties) SetValueLoader(l ValueLoader)

func (*DefaultProperties) SetValueReader

func (ctx *DefaultProperties) SetValueReader(r ValueReader)

type Deserializer added in v0.1.1

type Deserializer interface {
	Deserialize(v string, result interface{}) error
}

type Errors added in v0.1.3

type Errors []error

func (*Errors) AddError added in v0.1.3

func (es *Errors) AddError(e error) *Errors

func (Errors) Empty added in v0.1.3

func (es Errors) Empty() bool

func (Errors) Error added in v0.1.3

func (es Errors) Error() string

type JsonLoader added in v0.1.1

type JsonLoader struct{}

func NewJsonLoader added in v0.1.1

func NewJsonLoader() *JsonLoader

func (*JsonLoader) Deserialize added in v0.1.1

func (v *JsonLoader) Deserialize(value string, result interface{}) error

func (*JsonLoader) Serialize added in v0.1.1

func (v *JsonLoader) Serialize(o interface{}) (string, error)

type JsonReader

type JsonReader struct{}

func NewJsonReader

func NewJsonReader() *JsonReader

func (*JsonReader) Read

func (v *JsonReader) Read(r io.Reader) (*Value, error)

type Opt

type Opt func(ctx *DefaultProperties) error

func SetValue

func SetValue(r io.Reader) Opt

func SetValueLoader added in v0.1.1

func SetValueLoader(l ValueLoader) Opt

func SetValueReader

func SetValueReader(r ValueReader) Opt

type Properties

type Properties interface {
	// 配置ValueReader
	SetValueReader(r ValueReader)

	// 读取value
	ReadValue(r io.Reader) error

	// 设置ValueLoader值提取器
	SetValueLoader(l ValueLoader)

	// param: key属性名称
	// param: defaultValue: 默认值
	// return: 属性值,如不存在返回默认值
	Get(key string, defaultValue string) string

	// param: key属性名称
	// param: result: 填充对象指针
	// return: 正常返回nil,否则返回错误
	GetValue(key string, result interface{}) error
}
var Default Properties = New()

func LoadFile

func LoadFile(filename string, reader ValueReader, loader ValueLoader) (Properties, error)

func LoadJsonFile

func LoadJsonFile(filename string) (Properties, error)

func LoadYamlFile

func LoadYamlFile(filename string) (Properties, error)

func MergeProperties added in v0.1.1

func MergeProperties(props ...Properties) Properties

type Serializer added in v0.1.1

type Serializer interface {
	Serialize(o interface{}) (string, error)
}

type SettableProperties added in v0.1.1

type SettableProperties struct {
	DefaultProperties
}

func NewSettableProperties added in v0.1.1

func NewSettableProperties(opts ...Opt) *SettableProperties

func (*SettableProperties) Delete added in v0.1.1

func (p *SettableProperties) Delete(key string)

func (*SettableProperties) Set added in v0.1.1

func (p *SettableProperties) Set(key string, value interface{}) error

type Value

type Value = map[string]interface{}

type ValueLoader added in v0.1.1

type ValueLoader interface {
	Serializer
	Deserializer
}

type ValueReader

type ValueReader interface {
	Read(r io.Reader) (*Value, error)
}

type YamlLoader added in v0.1.1

type YamlLoader struct{}

func NewYamlLoader added in v0.1.1

func NewYamlLoader() *YamlLoader

func (*YamlLoader) Deserialize added in v0.1.1

func (v *YamlLoader) Deserialize(value string, result interface{}) error

func (*YamlLoader) Serialize added in v0.1.1

func (v *YamlLoader) Serialize(o interface{}) (string, error)

type YamlReader

type YamlReader struct{}

func NewYamlReader

func NewYamlReader() *YamlReader

func (*YamlReader) Read

func (v *YamlReader) Read(r io.Reader) (*Value, error)

Jump to

Keyboard shortcuts

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