xconf

package module
v0.3.23 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: Unlicense Imports: 30 Imported by: 5

README

XCONF

GitHub Workflow Status Go Version Go Report Card GoDoc

README | English

Golang配置文件加载解析, goconf v2,扩充了功能支持。

Run XConf Example: run on repl.it

Run XCmd Example: run on repl.it

功能简介

  • 支持默认值配置、解析
  • 支持多种格式,内置JSON, TOML, YAML,FLAG, ENV支持,并可注册解码器扩展格式支持
  • 支持多文件、多io.Reader数据加载,支持文件继承
  • 支持由OS ENV变量数据加载配置
  • 支持由命令行参数FLAGS加载数据
  • 支持由远程URL加载配置数据
  • 支持数据覆盖合并,加载多份数据时将按照加载文件的顺序按FieldPath自动合并
  • 支持通过${READ_TIMEOUT|5s}${IP_ADDRESS}等方式绑定Env参数
  • 支持配置热加载、实时同步,内置内存热加载支持,支持异步更新通知, 支持xconf-providers: ETCD、文件系统.
  • 支持WATCH具体的FieldPath变动
  • 支持导出配置到多种配置文件
  • 支持配置HASH,便于比对配置一致性
  • FLAGSENVFieldPath支持复杂类型,支持自定义复杂类型扩展支持
  • 支持配置访问秘钥
  • 支持自定义基于Label的灰度更新
  • 支持数值别名,如:math.MaxInt,runtime.NumCPU
  • 支持",squash"将子结构体的字段提到父结构中便于做配置扩充

名词解释

  • FieldTag
    • xconf在将配置由Strut与JSON, TOML, YAML,FLAG, ENV等转换时使用的字段别名,如:示例配置中的HttpAddressFieldTaghttp_address
    • 如果没有配置xconf:"http_address",则默认会采用字段名的SnakeCase作为FieldTag,可以通过xconf.WithFieldTagConvertor指定为其他方案,如字段名的小写等,注意FieldTag策略必须与配置源中使用的字符串一致,否则会导致解析数据失败。
  • FieldPath,由FieldTag组成的Field访问路径,如示例配置中的Config.SubTest.HTTPAddressFieldPathconfig.sub_test.http_address.
  • Leaf,xconf中配置的最小单位,基础类型、slice类型都是最小单位,Struct不是配置的最小单位,会根据配置的属性字段进行赋值、覆盖。
    • 默认情况下map是xconf配置的最小单位,但是可以通过指定notleaf标签使map不作为最小单位,而是基于key进行合并.但是这种情况下map的Value依然是xconf中的最小单位,即使value是Struct也将作为配置合并的最小单位
    • 通过xconf.WithMapMerge(true)可以激活MapMerge模式,在这个模式下map及其Value都不再是配置的最小单位,配置的最小单位为基础类型和slice类型。

快速开始

定义配置结构
  • 参考xconf/tests/conf.go使用optiongen定义配置并指定--xconf=true以生成支持xconf需求的标签.
  • 自定义结构,指定xconf需求的标签
type Server struct {
	Timeouts map[string]time.Duration `xconf:"timeouts"`
}

type SubTest struct {
	HTTPAddress string            `xconf:"http_address"`
	MapNotLeaf  map[string]int    `xconf:"map_not_leaf,notleaf"`
	Map2        map[string]int    `xconf:"map2"`
	Map3        map[string]int    `xconf:"map3"`
	Slice2      []int64           `xconf:"slice2"`
	Servers     map[string]Server `xconf:"servers,notleaf"`
}

type Config struct {
	HttpAddress     string          `xconf:"http_address"`
	Map1            map[string]int  `xconf:"map1"`
	MapNotLeaf      map[string]int  `xconf:"map_not_leaf,notleaf"`
	TimeDurations   []time.Duration `xconf:"time_durations"`
	Int64Slice      []int64         `xconf:"int64_slice"`
	Float64Slice    []float64       `xconf:"float64_slice"`
	Uin64Slice      []uint64        `xconf:"uin64_slice"`
	StringSlice     []string        `xconf:"string_slice"`
	ReadTimeout     time.Duration   `xconf:"read_timeout"`
	SubTest         SubTest         `xconf:"sub_test"`
}
从文件载入配置

yaml格式为例(tests/)

http_address: :3002
read_timeout: 100s
default_empty_map:
  test1: 1
map1:
  test1: 1000000
map_not_leaf:
  test1: 1000000
int64_slice:
- 1
- 2
sub_test:
  map2:
    ${IP_ADDRESS}: 2222
  map_not_leaf:
    test2222: 2222
  servers:
    s1:
      timeouts:
        read: ${READ_TIMEOUT|5s} 

参考:tests/main/main.go,文件间的继承通过xconf_inherit_files指定,参考tests/main/c2.toml

cc := NewTestConfig(
	xconf.WithFiles("c2.toml"), // 由指定的文件加载配置
	xconf.WithReaders(bytes.NewBuffer(yamlContents),bytes.NewBuffer(tomlContents),xconf.NewRemoteReader("http://127.0.0.1:9001/test.json", time.Duration(5)*time.Second)), // 由指定的reader加载配置
	xconf.WithFlagSet(flag.CommandLine), // 指定解析flag.CommandLine,默认值
	xconf.WithEnviron(os.Environ()), // 指定解析os.Environ(),默认值
)
xconf.Parse(cc)
配置存入文件
// SaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec
func SaveToFile(fileName string) error
// SaveToWriter 将内置解析的数据dump到writer,类型为ct
func SaveToWriter(ct ConfigType, writer io.Writer) error 

// SaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec
func SaveVarToFile(valPtr interface{}, fileName string) error 

// SaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct
func SaveVarToWriter(valPtr interface{}, ct ConfigType, writer io.Writer) error 

// MustSaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec,如发生错误会panic
func MustSaveToFile(f string) 
// MustSaveToWriter 将内置解析的数据dump到writer,需指定ConfigType,如发生错误会panic
func MustSaveToWriter(ct ConfigType, writer io.Writer) 

// MustSaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec
func MustSaveVarToFile(v interface{}, f string) 

// MustSaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct
func MustSaveVarToWriter(v interface{}, ct ConfigType, w io.Writer) 

// MustSaveToBytes 将内置解析的数据以字节流返回,需指定ConfigType
func MustSaveToBytes(ct ConfigType) []byte { return xx.MustSaveToBytes(ct) }

// SaveVarToWriterAsYAML 将内置解析的数据解析到yaml,带comment
func SaveVarToWriterAsYAML(valPtr interface{}, writer io.Writer) error 

可用选项

  • WithFiles : 指定加载的文件,配置覆盖顺序依赖传入的文件顺序
  • WithReaders: 指定加载的io.Reader,配置覆盖顺序依赖传入的io.Reader顺序。
  • WithFlagSet: 指定解析的FlagSet,默认是全局的flag.CommandLine,如指定为nil则不会将参数自动创建到FlagSet,同样也不会解析FlagSet内数据。
  • WithFlagArgs: 指定FlagSet解析的参数数据,默认为os.Args[1:]
  • WithFlagValueProvider: FlagSet支持的类型有限,xconf/xflag/vars中扩展了部分类型,参考[Flag 与 Env支持]
  • WithEnviron: 指定环境变量值
  • WithErrorHandling:指定错误处理方式,同flag.CommandLine处理方式
  • WithLogDebug: 指定debug日志输出
  • WithLogWarning: 指定warn日志输出
  • WithFieldTagConvertor: 当无法通过TagName获取FieldTag时,通过该方法转换,默认SnakeCase.
  • WithTagName: FieldTag字段来源的Tag名,默认xconf
  • WithTagNameDefaultValue: 默认值使用的Tag名称 ,默认default
  • WithParseDefault:是否解析默认值,默认true,推荐使用optiongen生成默认配置数据
  • WithDebug: 调试模式,会输出详细的解析流程日志
  • WithDecoderConfigOption: 调整mapstructure参数,xconf使用mapstructure进行类型转换
  • FieldPathDeprecated: 弃用的配置,解析时不会报错,但会打印warning日志
  • ErrEnvBindNotExistWithoutDefault: EnvBind时如果Env中不存在指定的key而且没有指定默认值时报错
  • FieldFlagSetCreateIgnore: 指定的FieldPath或者类型名在没有Flag Provider的时候,不打印报警日志

Flag 与 Env支持

  • 支持Flag中通过xconf_files指定配置文件
  • xconf/xflag/vars中扩展了部分类型如下:
    • float32,float64
    • int,int8,int16,int32,int64
    • uint,uint8,uint16,uint32,uint64
    • []float32,[]float64
    • []int,[]int8,[]int16,[]int32,[]int64
    • []uint,[]uint8,[]uint16,[]uint32,[]uint64
    • []string
    • []Duration
    • map[stirng]string,map[int]int,map[int64]int64,map[int64]string,map[stirng]int,map[stirng]int64,map[stirng]Duration
  • 扩展类型Slice与Map配置
    • slcie的定义方式为元素通过vars.StringValueDelim分割,默认为,,如:--time_durations=5s,10s,100s
    • map的定位方式为K、V通过vars.StringValueDelim分割,默认为,,如:--sub_test.map_not_leaf=k1,1,k2,2,k3,3
  • 自定义扩展
    • 扩展需要实现flag.Getter接口,可以通过实现Usage() string实现自定义的Usage信息。
      const JsnoPrefix = "json@"
      
      type serverProvider struct {
          s    string
          set  bool
          data *map[string]Server
      }
      
      func (sp *serverProvider) String() string {
          return sp.s
      }
      func (sp *serverProvider) Set(s string) error {
          sp.s = s
          if sp.set == false {
              *sp.data = make(map[string]Server)
          }
          if !strings.HasPrefix(s, JsnoPrefix) {
              return errors.New("server map need json data with prefix:" + JsnoPrefix)
          }
          s = strings.TrimPrefix(s, JsnoPrefix)
          return json.Unmarshal([]byte(s), sp.data)
      }
      func (sp *serverProvider) Get() interface{} {
          ret := make(map[string]interface{})
          for k, v := range *sp.data {
              ret[k] = v
          }
          return ret
      }
      func (sp *serverProvider) Usage() string {
          return fmt.Sprintf("server map, json format")
      }
      func newServerProvider(v interface{}) flag.Getter {
          return &serverProvider{data: v.(*map[string]Server)}
      }
      
      
    • 注册扩展
      • vars.SetProviderByFieldPath通过FieldPath设定扩展
      • vars.SetProviderByFieldType通过字段类型名称设定扩展
        cc := &Config{}
        jsonServer := `json@{"s1":{"timeouts":{"read":5000000000},"timeouts_not_leaf":{"write":5000000000}}}`
        x := xconf.New(
            xconf.WithFlagSet(flag.NewFlagSet("xconf-test", flag.ContinueOnError)),
            xconf.WithFlagArgs("--sub_test.servers="+jsonServer), //数据设定到flag中
            xconf.WithEnviron("sub_test_servers="+jsonServer), //数据设定到env中
        )
        vars.SetProviderByFieldPath("sub_test.servers", newServerProvider) // 根据字段FieldPath设定Provider
        vars.SetProviderByFieldType("map[string]Server", newServerProvider) // 根据字段类型名设定Provider
    
  • Keys 通过xconf.DumpInfo获取配置支持的FLAG与ENV名称,如下所示,其中Y表示为Option配置项,D为Deprecated字段,M为xconf内部字段。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FLAG                              ENV                                         TYPE            USAGE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--default_empty_map               TEST_PREFIX_DEFAULT_EMPTY_MAP               map[string]int  |Y| xconf/xflag/vars, key and value split by ,
--float64_slice                   TEST_PREFIX_FLOAT64_SLICE                   []float64       |Y| xconf/xflag/vars, value split by , (default [101.191 202.202 303.303])
--http_address                    TEST_PREFIX_HTTP_ADDRESS                    string          |Y| http_address (default "127.0.0.1")
--int64_slice                     TEST_PREFIX_INT64_SLICE                     []int64         |Y| xconf/xflag/vars, value split by , (default [101 202 303])
--int8                            TEST_PREFIX_INT8                            int8            |Y| int8 (default 1)
--map1                            TEST_PREFIX_MAP1                            map[string]int  |Y| k,v使用,分割 (default map[test1:100 test2:200])
--map_not_leaf                    TEST_PREFIX_MAP_NOT_LEAF                    map[string]int  |D| Deprecated: 使用Map1 (default map[test1:100 test2:200])
--max_int                         TEST_PREFIX_MAX_INT                         int             |Y| max_int (default 0)
--max_uint64                      TEST_PREFIX_MAX_UINT64                      uint64          |Y| max_uint64 (default 0)
--option_usage                    TEST_PREFIX_OPTION_USAGE                    string          |Y| option_usage (default "在这里描述一些应用级别的配置规则")
--process_count                   TEST_PREFIX_PROCESS_COUNT                   int8            |Y| process_count (default 1)
--read_timeout                    TEST_PREFIX_READ_TIMEOUT                    Duration        |Y| read_timeout (default 5s)
--redis.redis_address             TEST_PREFIX_REDIS_REDIS_ADDRESS             string          |Y| redis.redis_address (default "127.0.0.1:6637")
--redis_as_pointer.redis_address  TEST_PREFIX_REDIS_AS_POINTER_REDIS_ADDRESS  string          |Y| redis_as_pointer.redis_address
--redis_timeout.read_timeout      TEST_PREFIX_REDIS_TIMEOUT_READ_TIMEOUT      Duration        |Y| redis_timeout.read_timeout (default 0s)
--string_slice                    TEST_PREFIX_STRING_SLICE                    []string        |Y| xconf/xflag/vars, value split by , (default [test1 test2 test3])
--sub_test.http_address           TEST_PREFIX_SUB_TEST_HTTP_ADDRESS           string          |Y| sub_test.http_address
--sub_test.map2                   TEST_PREFIX_SUB_TEST_MAP2                   map[string]int  |Y| xconf/xflag/vars, key and value split by ,
--sub_test.map3                   TEST_PREFIX_SUB_TEST_MAP3                   map[string]int  |Y| xconf/xflag/vars, key and value split by ,
--sub_test.map_not_leaf           TEST_PREFIX_SUB_TEST_MAP_NOT_LEAF           map[string]int  |Y| xconf/xflag/vars, key and value split by ,
--sub_test.slice2                 TEST_PREFIX_SUB_TEST_SLICE2                 []int64         |Y| xconf/xflag/vars, value split by ,
--test_bool                       TEST_PREFIX_TEST_BOOL                       bool            |Y| test_bool (default false)
--test_bool_true                  TEST_PREFIX_TEST_BOOL_TRUE                  bool            |Y| test_bool_true (default true)
--time_durations                  TEST_PREFIX_TIME_DURATIONS                  []Duration      |Y| 延迟队列 (default [1s 1s])
--uin64_slice                     TEST_PREFIX_UIN64_SLICE                     []uint64        |Y| xconf/xflag/vars, value split by , (default [101 202 303])
--xconf_flag_files                TEST_PREFIX_XCONF_FLAG_FILES                string          |M| xconf files provided by flag, file slice, split by ,
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在这里描述一些应用级别的配置规则
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ENV绑定

支持解析ENV变量名称,如下例:


var yamlTest2 = []byte(`
http_address: :3002
read_timeout: 100s
default_empty_map:
  test1: 1
map1:
  test1: 1000000
map_not_leaf:
  test1: 1000000
int64_slice:
- 1
- 2
sub_test:
  map2:
    ${IP_ADDRESS}: 2222
  map_not_leaf:
    test2222: 2222
  servers:
    s1:
      timeouts:
        read: ${READ_TIMEOUT|5s} 
`)

func TestEnvBind(t *testing.T) {
	Convey("env bind", t, func(c C) {
		cc := &Config{}
		x := xconf.NewWithoutFlagEnv()
		So(x.UpdateWithFieldPathValues("http_address", "${XCONF_HOST}:${XCONF_PORT}"),ShouldBeNil)
		err := x.Parse(cc)
		So(err, ShouldBeNil)
		So(cc.HttpAddress, ShouldEqual, "")
		host := "127.0.0.1"
		port := "9001"
		os.Setenv("XCONF_HOST", host)
		os.Setenv("XCONF_PORT", port)
		So(cc.HttpAddress, ShouldEqual, "")
		So(x.UpdateWithReader(bytes.NewBuffer(yamlTest2)), ShouldBeNil)
		So(x.UpdateWithFieldPathValues("http_address", "${XCONF_HOST}:${XCONF_PORT}"), ShouldBeNil)
		latest, err := x.Latest()
		So(err, ShouldBeNil)
		cc = latest.(*Config)
		So(cc.HttpAddress, ShouldEqual, host+":"+port)
		So(cc.SubTest.Servers["s1"].Timeouts["read"], ShouldEqual, time.Duration(5)*time.Second)
	})
}
URL读取
cc := &Config{}
x := xconf.NewWithoutFlagEnv(xconf.WithReaders(xconf.NewRemoteReader("http://127.0.0.1:9001/test.yaml", time.Duration(1)*time.Second)))

动态更新

基于配置文件
	testBytesInMem := "memory_test_key"
	mem, err := xmem.New()
	panicErr(err)
	// xconf/kv提供了基于ETCD/FILE/MEMORY的更新机制
	// 可自行实现xconf的Loader接口或者对接到xmem,借助xmem的机制实现配置更新
	xconf.WatchUpdate(testBytesInMem, mem)
	updated := make(chan *Config, 1)
	go func() {
		for {
			select {
			case v := <-x.NotifyUpdate():
				updated <- v.(*Config)
			}
		}
	}()
基于FieldPath
err := xconf.WatchFieldPath("sub_test.http_address", func(from, to interface{}) {
	fmt.Printf("sub_test.http_address changed from %v to %v ", from, to)
})
panicErr(err)

可以通过以下方法实现基于文件或Buffer的更新,更新结果通过xconf.NotifyUpdate异步获取,或通过xconf.Latest同步获取。

  • UpdateWithFiles(files ...string) (err error)
  • UpdateWithReader(readers ...io.Reader) (err error)

可以通过以下方法实现基于FieldPath的配置更新,更新结果通过xconf.NotifyUpdate异步获取,或通过xconf.Latest同步获取。

  • UpdateWithFlagArgs(flagArgs ...string) (err error)
  • UpdateWithEnviron(environ ...string) (err error)
  • UpdateWithFieldPathValues(kv ...string) (err error)
绑定最新配置
xconf.Latest()
Atomic自动更新

使用optiongen定义配置并指定--xconf=true生成支持XConf的配置会默认生成Atomic更新支持:


func (cc *Config) AtomicSetFunc() func(interface{}) { return AtomicConfigSet }

var atomicConfig unsafe.Pointer

func AtomicConfigSet(update interface{}) {
	atomic.StorePointer(&atomicConfig, (unsafe.Pointer)(update.(*Config)))
}
func AtomicConfig() ConfigVisitor {
	current := (*Config)(atomic.LoadPointer(&atomicConfig))
	if current == nil {
		atomic.CompareAndSwapPointer(&atomicConfig, nil, (unsafe.Pointer)(newDefaultConfig()))
		return (*Config)(atomic.LoadPointer(&atomicConfig))
	}
	return current
}

解析时提供AtomicConfig()即可,当配置更新的时候回自动回调AtomicConfigSet方法进行指针替换。

func TestAtomicVal(t *testing.T) {
	Convey("atomic val", t, func(c C) {
		x := xconf.NewWithoutFlagEnv()
		So(x.Parse(AtomicConfig()), ShouldBeNil)
		So(x.UpdateWithFieldPathValues("http_address", "10.10.10.10"), ShouldBeNil)
		So(AtomicConfig().HttpAddress, ShouldEqual, "10.10.10.10")
	})
}

使用示例

由URL加载加密的配置
package main

import (
	"time"

	"github.com/sandwich-go/xconf"
	"github.com/sandwich-go/xconf/secconf"
	"github.com/sandwich-go/xconf/tests"
)

func main() {
	// 远程加密配置
	urlReader := xconf.NewRemoteReader("127.0.0.1:9001", time.Duration(1)*time.Second)
	key, _ := xconf.ParseEnvValue("${XXXTEA_KEY}|1dxz29pew", false)
	urlReaderSec := secconf.Reader(urlReader, secconf.StandardChainDecode(secconf.NewDecoderXXTEA([]byte(key))))

	// 解析
	xconf.Parse(tests.AtomicConfig(), xconf.WithReaders(urlReaderSec))
}

使用限制

私有字段

如果配置中定义了私有字段,或对XConf隐藏的字段(xconf标签指定为-), 在使用动态更新特性时如下使用限制。

  • 在主动调用Latest绑定最新的配置
  • Atomic主动绑定模式下,配置更新
Flag
  • 自动创建并定义到FlagSet中的配置字段仅限于xconf/xflag支持的类型。
  • 复杂类型如:"map[string][]time.Durtaion","map[string]*Server"等无法完成自动化创建,会有WARNGING日志打印,同时可以通过WithFlagCreateIgnoreFiledPath主动忽略这些字段。
  • 无法自动创建到FlagSet中的字段无法通过--help或者Usage()获取到字段的信息及默认值。

XConf根据Parse时无法缓存私有、隐藏字段数据,为了防止逻辑层访问配置与配置更新可能存在的数据多协程访问问题,在Atomic被动更新或者主动调用Latest绑定的时候,传入的结构构造了一个新的配置结构体,导致此时得到的数据将不包含私有字段和隐藏字段。

对于私有数据或者隐藏字段,在使用动态更新特性时,建议在主动调用Latest后或设定的InstallCallbackOnAtomicXXXXXXXXXSet回调逻辑中再次赋值。

xcmd 命令行支持

xcmd依托xconf自动完成flag参数创建,绑定,解析等操作,同时支持自定义flag,支持中间件,支持子命令. 参考:xcmd/main/main.go

help命令扩展

  • --help=yaml
    • 将当前解析的配置以yaml格式打印到终端
  • --help=./test.yaml
    • 将当前解析的配置以yaml格式打印到指定的文件,如文件不存在会自动创建

由于help命令截断了配置解析流程,所以help扩展命令输出的配置内容为传入的结构本身的内容(默认值),不包含指定的文件,FLAG,ENV等等中的内容。

Documentation

Index

Constants

View Source
const (
	// MetaKeyFlagFiles 元数据,flag中使用,用于通过flag指定需加载的配置文件列表
	// 多个文件以,分割,如server --xconf_flag_files=base.yaml,testflight.yaml
	MetaKeyFlagFiles = "xconf_flag_files"

	// MetaKeyInheritFiles 元数据,配置文件内使用,用于指定继承的文件
	// 如toml中配置:xconf_inherit_files=[etcd.yaml,production.yaml],则当前配置会继承etcd.yaml,production.yaml文件
	MetaKeyInheritFiles = "xconf_inherit_files"

	// MetaKeyGrayLabel 元数据,灰度发布支持,发布配置的的时候指定配置生次奥的label
	// xconf运行时可以通过WithAppLabelList指定label,当MetaKeyGrayLabel不为空且至少含有一个AppLabelList中的label时配置会应用到当前实例
	MetaKeyGrayLabel = "xconf_gray_rule_label"

	// MetaKeyLatestHash 元数据,预留用于配置版本的比对、校验
	MetaKeyLatestHash = "xconf_latest_hash"

	// MetaKeyInheritFilesDeprecatedFromGoconf 同MetaKeyInheritFiles,兼容goconf
	MetaKeyInheritFilesDeprecatedFromGoconf = "inherit_files"
)
View Source
const (
	// HashPrefix hash字段前缀
	HashPrefix = "xconf@"
	// DefaultInvalidHashString 默认hash值
	DefaultInvalidHashString = HashPrefix + "hash_invalid"
)

Variables

View Source
var DefaultKeyDelim = "."

DefaultKeyDelim 默认的FilePath分割符

View Source
var DefaultTagName = "xconf"

DefaultTagName 默认读取的tag名

View Source
var DefaultValueTagName = "default"

DefaultValueTagName default value 默认读取的tag名

View Source
var ErrHelp = flag.ErrHelp

ErrHelp is the error returned if the -help or -h flag is invoked but no such flag is defined.

View Source
var ValueGetter = os.LookupEnv

ValueGetter Env value provider func.

Functions

func AtomicOptionsSet

func AtomicOptionsSet(update interface{})

AtomicOptionsSet atomic setter for *Options

func DumpInfo

func DumpInfo()

DumpInfo debug数据

func FieldMap added in v0.2.6

func FieldMap(valPtr interface{}, x *XConf) map[string]StructFieldPathInfo

FieldMap 获取对象的Field对象Map

func FieldPathList added in v0.2.6

func FieldPathList(valPtr interface{}, x *XConf) (ret []string)

FieldPathList 获取对象的FieldPath列表

func FlagTypeStr added in v0.2.7

func FlagTypeStr(x *XConf, name string) (tag string)

FlagTypeStr 获取子弹标记,Y代表xconf解析管理的配置,M标识xconf内置配置,D标识Deprecated,-表示为非xconf管理的配置

func Hash

func Hash() (s string)

Hash 返回当前最新数据的hash字符串

func HashStructure

func HashStructure(v interface{}) (s string)

HashStructure 返回指定配置的hash字符串

func InstallCallbackOnAtomicOptionsSet

func InstallCallbackOnAtomicOptionsSet(callback func(cc OptionsInterface) bool)

InstallCallbackOnAtomicOptionsSet install callback

func InstallOptionsWatchDog

func InstallOptionsWatchDog(dog func(cc *Options))

InstallOptionsWatchDog the installed func will called when NewOptions called

func IsErrHelp added in v0.3.5

func IsErrHelp(err error) bool

IsErrHelp 检查错误是否是ErrHelp

func Latest

func Latest() (interface{}, error)

Latest 将xconf内缓存的配置数据绑定到Parse时传入类型,逻辑层需要将返回的interface{}转换到相应的配置指针

func Merge added in v0.3.9

func Merge(opts ...Option) error

Merge 合并配置

func MustParse added in v0.3.4

func MustParse(valPtr interface{}, opts ...Option)

MustParse 解析配置到传入的参数中,如发生错误则直接panic

func MustSaveToBytes

func MustSaveToBytes(ct ConfigType) []byte

MustSaveToBytes 将内置解析的数据以字节流返回,需指定ConfigType

func MustSaveToFile

func MustSaveToFile(f string)

MustSaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec,如发生错误会panic

func MustSaveToWriter

func MustSaveToWriter(ct ConfigType, writer io.Writer)

MustSaveToWriter 将内置解析的数据dump到writer,需指定ConfigType,如发生错误会panic

func MustSaveVarToFile

func MustSaveVarToFile(v interface{}, f string)

MustSaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec

func MustSaveVarToWriter

func MustSaveVarToWriter(v interface{}, ct ConfigType, w io.Writer)

MustSaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct

func NewRemoteReader

func NewRemoteReader(url string, timeout time.Duration, headerkv ...string) io.Reader

NewRemoteReader 返回一个远程Reader,指定url及超时时间

func NotifyUpdate

func NotifyUpdate() <-chan interface{}

NotifyUpdate 底层配置更新,返回的是全量的数据指针

func OptionsOptionDeclareWithDefault

func OptionsOptionDeclareWithDefault() interface{}

OptionsOptionDeclareWithDefault go-lint

func Parse

func Parse(valPtr interface{}, opts ...Option) error

Parse 解析配置到传入的参数中

func ParseDefault

func ParseDefault(valPtr interface{}, opts ...Option) (err error)

ParseDefault 根据opts指定的TagNameDefaultValue解析字段默认值并绑定到valPtr

func ParseEnvValue

func ParseEnvValue(val string) (newVal string, err error)

ParseEnvValue parse ENV var value from input string, support default value.

func RegisterCodec

func RegisterCodec(ct ConfigType, d DecodeFunc, e EncodeFunc)

RegisterCodec 注册自定义的Codec

func SaveToFile

func SaveToFile(fileName string) error

SaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec

func SaveToWriter

func SaveToWriter(ct ConfigType, writer io.Writer) error

SaveToWriter 将内置解析的数据dump到writer,类型为ct

func SaveVarToFile

func SaveVarToFile(valPtr interface{}, fileName string) error

SaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec

func SaveVarToWriter

func SaveVarToWriter(valPtr interface{}, ct ConfigType, writer io.Writer) error

SaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct

func SaveVarToWriterAsYAML added in v0.3.5

func SaveVarToWriterAsYAML(valPtr interface{}, writer io.Writer) error

SaveVarToWriterAsYAML 将内置解析的数据解析到yaml,带comment

func StringAlias added in v0.2.6

func StringAlias(alias map[string]string, aliasFunc map[string]func(string) string) mapstructure.DecodeHookFunc

func UpdateWithEnviron

func UpdateWithEnviron(environ ...string) (err error)

UpdateWithEnviron 提供环境变量合法配置更新数据,通过NotifyUpdate异步通知更新或通过Latest同步获取

func UpdateWithFieldPathValues

func UpdateWithFieldPathValues(kv ...string) (err error)

UpdateWithFieldPathValues 根据字段FieldPath更新数据, 支持的字段类型依赖于xflag,通过NotifyUpdate异步通知更新或通过Latest同步获取

func UpdateWithFiles

func UpdateWithFiles(files ...string) (err error)

UpdateWithFiles 通过文件更新配置,通过NotifyUpdate异步通知更新或通过Latest同步获取

func UpdateWithFlagArgs

func UpdateWithFlagArgs(flagArgs ...string) (err error)

UpdateWithFlagArgs 提供FlagSet合法参数更新数据,通过NotifyUpdate异步通知更新或通过Latest同步获取

func UpdateWithReader

func UpdateWithReader(readers ...io.Reader) (err error)

UpdateWithReader 通过reader更新配置,通过NotifyUpdate异步通知更新或通过Latest同步获取

func Usage added in v0.2.2

func Usage()

Usage usage info

func WatchFieldPath

func WatchFieldPath(fieldPath string, changed OnFieldUpdated)

WatchFieldPath 关注特定的字段变化

func WatchUpdate

func WatchUpdate(confPath string, loader kv.Loader)

WatchUpdate confPath不会自动绑定env value,如果需要watch的路径与环境变量相关,先通过ParseEnvValue自行解析替换处理错误

Types

type AtomicSetterProvider added in v0.2.6

type AtomicSetterProvider interface {
	AtomicSetFunc() func(interface{})
}

AtomicSetterProvider Atomic设置配置的方法提供接口,配置层提供改方法供XConf自动设定最新配置

type ConfigType

type ConfigType string

ConfigType 编解码类型

const ConfigTypeJSON ConfigType = ".json"

ConfigTypeJSON json编解码类型

const ConfigTypeTOML ConfigType = ".toml"

ConfigTypeTOML toml编解码类型

const ConfigTypeYAML ConfigType = ".yaml"

ConfigTypeYAML yaml编解码类型

type DecodeFunc

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

DecodeFunc 解码方法签名

func GetDecodeFunc

func GetDecodeFunc(ext string) DecodeFunc

GetDecodeFunc 获取解码方法

type DecoderConfigOption

type DecoderConfigOption = func(*mapstructure.DecoderConfig)

DecoderConfigOption A DecoderConfigOption can be passed to Unmarshal to configure mapstructure.DecoderConfig options

type EncodeFunc

type EncodeFunc func(v map[string]interface{}) ([]byte, error)

EncodeFunc 编码方法签名

func GetEncodeFunc

func GetEncodeFunc(ext string) EncodeFunc

GetEncodeFunc 获取编码方法

type ErrorHandling

type ErrorHandling int

ErrorHandling 错误处理类型

const (
	// ContinueOnError 发生错误继续运行,Parse会返回错误
	ContinueOnError ErrorHandling = iota
	// ExitOnError 发生错误后退出
	ExitOnError
	// PanicOnError 发生错误后主动panic
	PanicOnError
)

type FieldTagConvertor

type FieldTagConvertor = func(fieldName string) string

FieldTagConvertor filed名称转换方法

type GetOptionUsage added in v0.2.6

type GetOptionUsage interface {
	GetOptionUsage() string
}

GetOptionUsage 配置层提供改方法供XConf获取配置说明打印Usage

type LogFunc

type LogFunc = func(string)

LogFunc 日志方法

type OnFieldUpdated

type OnFieldUpdated func(fieldPath string, from, to interface{})

OnFieldUpdated 字段发生变化方法签名

type Option

type Option func(cc *Options) Option

Option option func

func WithAppLabelList

func WithAppLabelList(v ...string) Option

WithAppLabelList 应用层Label,用于灰度发布场景

func WithDebug

func WithDebug(v bool) Option

WithDebug debug模式下输出调试信息

func WithDecoderConfigOption

func WithDecoderConfigOption(v ...DecoderConfigOption) Option

WithDecoderConfigOption xconf内部依赖mapstructure,改方法用户用户层自定义mapstructure解析参数,参考:https://github.com/mitchellh/mapstructure

func WithEnvBindShouldErrorWhenFailed added in v0.2.5

func WithEnvBindShouldErrorWhenFailed(v bool) Option

WithEnvBindShouldErrorWhenFailed EnvBind时如果Env中不存在指定的key而且没有指定默认值时是否返回错误

func WithEnviron

func WithEnviron(v ...string) Option

WithEnviron Parse解析的环境变量,默认os.Environ(),内部转换为FlagSet处理,可通过--help获取当前支持的FlagSet与Env参数定义

func WithEnvironPrefix added in v0.2.8

func WithEnvironPrefix(v string) Option

WithEnvironPrefix 绑定ENV前缀,防止ENV名称覆盖污染

func WithErrorHandling

func WithErrorHandling(v ErrorHandling) Option

WithErrorHandling 错误处理模式

func WithErrorUnused added in v0.3.15

func WithErrorUnused(v bool) Option

WithErrorUnused 当配置中出现未用到的字段时是否认为是错误

func WithFieldPathRemoved added in v0.2.5

func WithFieldPathRemoved(v ...string) Option

WithFieldPathRemoved 弃用的配置,目标结构中已经删除,但配置文件中可能存在,解析时不会认为是错误,会将该配置丢弃,并打印WARNING日志

func WithFieldTagConvertor

func WithFieldTagConvertor(v FieldTagConvertor) Option

WithFieldTagConvertor 字段名转换到FiledPath时优先使用TagName指定的名称,否则使用该函数转换

func WithFiles

func WithFiles(v ...string) Option

WithFiles option func for filed Files

func WithFlagArgs

func WithFlagArgs(v ...string) Option

WithFlagArgs FlagSet解析使用的Args列表,默认为os.Args[1:],如指定为空则不会触发FlagSet的定义和解析逻辑

func WithFlagCreateIgnoreFiledPath added in v0.2.5

func WithFlagCreateIgnoreFiledPath(v ...string) Option

WithFlagCreateIgnoreFiledPath 不创建到FlagSet中的字段FieldPath

func WithFlagSet

func WithFlagSet(v *flag.FlagSet) Option

WithFlagSet Parse使用的FlagSet,xconf会自动在flag中创建字段定义,如指定为空则不会创建

func WithLogDebug

func WithLogDebug(v LogFunc) Option

WithLogDebug DEBUG日志

func WithLogWarning

func WithLogWarning(v LogFunc) Option

WithLogWarning WARNING日志

func WithMapMerge

func WithMapMerge(v bool) Option

WithMapMerge map是否开启merge模式,详情见文档

func WithOptionUsage added in v0.2.5

func WithOptionUsage(v string) Option

WithOptionUsage option func for filed OptionUsage

func WithOptionUsagePoweredBy added in v0.3.11

func WithOptionUsagePoweredBy(v string) Option

WithOptionUsagePoweredBy --help中显示Power by

func WithParseDefault

func WithParseDefault(v bool) Option

WithParseDefault 是否解析struct标签中的default数据,解析规则参考xflag支持

func WithParseMetaKeyFlagFiles added in v0.2.6

func WithParseMetaKeyFlagFiles(v bool) Option

WithParseMetaKeyFlagFiles 是否解析flag中的MetaKeyFlagFiles指定的文件

func WithReaders

func WithReaders(v ...io.Reader) Option

WithReaders Parse时会由指定的Reader中加载配置

func WithReplaceFlagSetUsage added in v0.2.6

func WithReplaceFlagSetUsage(v bool) Option

WithReplaceFlagSetUsage 是否替换FlagSet的Usage,使用xconf内置版本

func WithStringAlias added in v0.2.6

func WithStringAlias(v map[string]string) Option

WithStringAlias 值别名

func WithStringAliasFunc added in v0.2.6

func WithStringAliasFunc(v map[string]func(s string) string) Option

WithStringAliasFunc 值别名计算逻辑

func WithTagName

func WithTagName(v string) Option

WithTagName xconf使用的字段TAG名称,默认:xconf

func WithTagNameForDefaultValue added in v0.2.5

func WithTagNameForDefaultValue(v string) Option

WithTagNameForDefaultValue 默认值TAG名称,默认default

type Options

type Options struct {
	OptionUsage string `xconf:"option_usage"`
	// annotation@NewFunc(comment="Parse时会由指定的File中加载配置")
	Files []string `xconf:"files"`
	// annotation@Readers(comment="Parse时会由指定的Reader中加载配置")
	Readers []io.Reader `xconf:"readers" usage:"Parse时会由指定的Reader中加载配置"`
	// annotation@FlagSet(comment="Parse使用的FlagSet,xconf会自动在flag中创建字段定义,如指定为空则不会创建")
	FlagSet *flag.FlagSet `xconf:"flag_set" usage:"Parse使用的FlagSet,xconf会自动在flag中创建字段定义,如指定为空则不会创建"`
	// annotation@FlagArgs(comment="FlagSet解析使用的Args列表,默认为os.Args[1:],如指定为空则不会触发FlagSet的定义和解析逻辑")
	FlagArgs []string `` /* 145-byte string literal not displayed */
	// annotation@Environ(comment="Parse解析的环境变量,默认os.Environ(),内部转换为FlagSet处理,可通过--help获取当前支持的FlagSet与Env参数定义")
	Environ []string `` /* 164-byte string literal not displayed */
	// annotation@ErrorHandling(comment="错误处理模式")
	ErrorHandling ErrorHandling `xconf:"error_handling" usage:"错误处理模式"`
	// annotation@TagName(comment="xconf使用的字段TAG名称,默认:xconf")
	TagName string `xconf:"tag_name" usage:"xconf使用的字段TAG名称,默认:xconf"`
	// annotation@DecoderConfigOption(comment="xconf内部依赖mapstructure,改方法用户用户层自定义mapstructure解析参数,参考:https://github.com/mitchellh/mapstructure")
	DecoderConfigOption []DecoderConfigOption `` /* 178-byte string literal not displayed */
	// annotation@MapMerge(comment="map是否开启merge模式,详情见文档")
	MapMerge bool `xconf:"map_merge" usage:"map是否开启merge模式,详情见文档"`
	// annotation@FieldTagConvertor(comment="字段名转换到FiledPath时优先使用TagName指定的名称,否则使用该函数转换")
	FieldTagConvertor FieldTagConvertor `` /* 130-byte string literal not displayed */
	// annotation@FieldPathRemoved(comment="弃用的配置,目标结构中已经删除,但配置文件中可能存在,解析时不会认为是错误,会将该配置丢弃,并打印WARNING日志")
	FieldPathRemoved []string `` /* 195-byte string literal not displayed */
	// annotation@Debug(comment="debug模式下输出调试信息")
	Debug bool `xconf:"debug" usage:"debug模式下输出调试信息"`
	// annotation@LogDebug(comment="DEBUG日志")
	LogDebug LogFunc `xconf:"log_debug" usage:"DEBUG日志"`
	// annotation@LogWarning(comment="WARNING日志")
	LogWarning LogFunc `xconf:"log_warning" usage:"WARNING日志"`
	// annotation@AppLabelList(comment="应用层Label,用于灰度发布场景")
	AppLabelList []string `xconf:"app_label_list" usage:"应用层Label,用于灰度发布场景"`
	// annotation@EnvBindShouldErrorWhenFailed(comment="EnvBind时如果Env中不存在指定的key而且没有指定默认值时是否返回错误")
	EnvBindShouldErrorWhenFailed bool `` /* 141-byte string literal not displayed */
	// annotation@FlagCreateIgnoreFiledPath(comment="不创建到FlagSet中的字段FieldPath")
	// todo: 可以通过tag中指定flagoff规避这个字段的支持
	FlagCreateIgnoreFiledPath []string `xconf:"flag_create_ignore_filed_path" usage:"不创建到FlagSet中的字段FieldPath"`
	// annotation@ParseDefault(comment="是否解析struct标签中的default数据,解析规则参考xflag支持")
	ParseDefault bool `xconf:"parse_default" usage:"是否解析struct标签中的default数据,解析规则参考xflag支持"`
	// annotation@TagNameForDefaultValue(comment="默认值TAG名称,默认default")
	TagNameForDefaultValue string `xconf:"tag_name_for_default_value" usage:"默认值TAG名称,默认default"`
	// annotation@ReplaceFlagSetUsage(comment="是否替换FlagSet的Usage,使用xconf内置版本")
	ReplaceFlagSetUsage bool `xconf:"replace_flag_set_usage" usage:"是否替换FlagSet的Usage,使用xconf内置版本"`
	// annotation@ParseMetaKeyFlagFiles(comment="是否解析flag中的MetaKeyFlagFiles指定的文件")
	// 当一个app中有多个根配置,只能有一个根配置解析flag中的配置文件
	ParseMetaKeyFlagFiles bool `xconf:"parse_meta_key_flag_files" usage:"是否解析flag中的MetaKeyFlagFiles指定的文件"`
	// annotation@EnvironPrefix(comment="绑定ENV前缀,防止ENV名称覆盖污染")
	EnvironPrefix string `xconf:"environ_prefix" usage:"绑定ENV前缀,防止ENV名称覆盖污染"`
	// annotation@OptionUsagePoweredBy(comment="--help中显示Power by")
	OptionUsagePoweredBy string `xconf:"option_usage_powered_by" usage:"--help中显示Power by"`
	// annotation@ErrorUnused(comment="当配置中出现未用到的字段时是否认为是错误")
	ErrorUnused bool `xconf:"error_unused" usage:"当配置中出现未用到的字段时是否认为是错误"`
	// annotation@StringAlias(comment="值别名")
	StringAlias map[string]string `xconf:"string_alias" usage:"值别名"`
	// annotation@StringAliasFunc(comment="值别名计算逻辑")
	StringAliasFunc map[string]func(s string) string `xconf:"string_alias_func" usage:"值别名计算逻辑"`
}

Options should use NewOptions to initialize it

func NewOptions

func NewOptions(opts ...Option) *Options

NewOptions new Options

func (*Options) ApplyOption

func (cc *Options) ApplyOption(opts ...Option) []Option

ApplyOption apply multiple new option and return the old ones sample: old := cc.ApplyOption(WithTimeout(time.Second)) defer cc.ApplyOption(old...)

func (*Options) AtomicSetFunc

func (cc *Options) AtomicSetFunc() func(interface{})

AtomicSetFunc used for XConf

func (*Options) GetAppLabelList

func (cc *Options) GetAppLabelList() []string

func (*Options) GetDebug

func (cc *Options) GetDebug() bool

func (*Options) GetDecoderConfigOption

func (cc *Options) GetDecoderConfigOption() []DecoderConfigOption

func (*Options) GetEnvBindShouldErrorWhenFailed added in v0.2.5

func (cc *Options) GetEnvBindShouldErrorWhenFailed() bool

func (*Options) GetEnviron

func (cc *Options) GetEnviron() []string

func (*Options) GetEnvironPrefix added in v0.2.8

func (cc *Options) GetEnvironPrefix() string

func (*Options) GetErrorHandling

func (cc *Options) GetErrorHandling() ErrorHandling

func (*Options) GetErrorUnused added in v0.3.15

func (cc *Options) GetErrorUnused() bool

func (*Options) GetFieldPathRemoved added in v0.2.5

func (cc *Options) GetFieldPathRemoved() []string

func (*Options) GetFieldTagConvertor

func (cc *Options) GetFieldTagConvertor() FieldTagConvertor

func (*Options) GetFiles

func (cc *Options) GetFiles() []string

func (*Options) GetFlagArgs

func (cc *Options) GetFlagArgs() []string

func (*Options) GetFlagCreateIgnoreFiledPath added in v0.2.5

func (cc *Options) GetFlagCreateIgnoreFiledPath() []string

func (*Options) GetFlagSet

func (cc *Options) GetFlagSet() *flag.FlagSet

func (*Options) GetLogDebug

func (cc *Options) GetLogDebug() LogFunc

func (*Options) GetLogWarning

func (cc *Options) GetLogWarning() LogFunc

func (*Options) GetMapMerge

func (cc *Options) GetMapMerge() bool

func (*Options) GetOptionUsage added in v0.2.5

func (cc *Options) GetOptionUsage() string

all getter func

func (*Options) GetOptionUsagePoweredBy added in v0.3.11

func (cc *Options) GetOptionUsagePoweredBy() string

func (*Options) GetParseDefault

func (cc *Options) GetParseDefault() bool

func (*Options) GetParseMetaKeyFlagFiles added in v0.2.6

func (cc *Options) GetParseMetaKeyFlagFiles() bool

func (*Options) GetReaders

func (cc *Options) GetReaders() []io.Reader

func (*Options) GetReplaceFlagSetUsage added in v0.2.6

func (cc *Options) GetReplaceFlagSetUsage() bool

func (*Options) GetStringAlias added in v0.2.6

func (cc *Options) GetStringAlias() map[string]string

func (*Options) GetStringAliasFunc added in v0.2.6

func (cc *Options) GetStringAliasFunc() map[string]func(s string) string

func (*Options) GetTagName

func (cc *Options) GetTagName() string

func (*Options) GetTagNameForDefaultValue added in v0.2.5

func (cc *Options) GetTagNameForDefaultValue() string

type OptionsInterface

type OptionsInterface interface {
	OptionsVisitor
	ApplyOption(...Option) []Option
}

OptionsInterface visitor + ApplyOption interface for Options

type OptionsVisitor

type OptionsVisitor interface {
	GetOptionUsage() string
	GetFiles() []string
	GetReaders() []io.Reader
	GetFlagSet() *flag.FlagSet
	GetFlagArgs() []string
	GetEnviron() []string
	GetErrorHandling() ErrorHandling
	GetTagName() string
	GetDecoderConfigOption() []DecoderConfigOption
	GetMapMerge() bool
	GetFieldTagConvertor() FieldTagConvertor
	GetFieldPathRemoved() []string
	GetDebug() bool
	GetLogDebug() LogFunc
	GetLogWarning() LogFunc
	GetAppLabelList() []string
	GetEnvBindShouldErrorWhenFailed() bool
	GetFlagCreateIgnoreFiledPath() []string
	GetParseDefault() bool
	GetTagNameForDefaultValue() string
	GetReplaceFlagSetUsage() bool
	GetParseMetaKeyFlagFiles() bool
	GetEnvironPrefix() string
	GetOptionUsagePoweredBy() string
	GetErrorUnused() bool
	GetStringAlias() map[string]string
	GetStringAliasFunc() map[string]func(s string) string
}

OptionsVisitor visitor interface for Options

func AtomicOptions

func AtomicOptions() OptionsVisitor

AtomicOptions return atomic *OptionsVisitor

type Struct

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

Struct Struct类型定义

func NewStruct

func NewStruct(s interface{}, tagName, tagNameDefaultValue string, ff FieldTagConvertor) *Struct

NewStruct 构造Struct类型

func (*Struct) Map

func (s *Struct) Map() (map[string]interface{}, map[string]StructFieldPathInfo)

Map 返回数据及字段类型信息

type StructFieldPathInfo

type StructFieldPathInfo struct {
	TagListXConf  xfield.TagList
	Tag           reflect.StructTag
	FieldName     string
	FieldNameList []string
	DefaultGot    bool
	DefaultString string
}

StructFieldPathInfo field信息

type XConf

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

XConf XConf struct

func New

func New(opts ...Option) *XConf

New 构造新的Xconf

func NewWithConf

func NewWithConf(cc *Options) *XConf

NewWithConf 由指定的配置构造XConf

func NewWithoutFlagEnv

func NewWithoutFlagEnv(opts ...Option) *XConf

NewWithoutFlagEnv 构造新的Xconf,移除FlagSet和Environ解析

func (*XConf) Copy

func (x *XConf) Copy() *XConf

Copy 返回当前XConf的拷贝

func (*XConf) DumpInfo

func (x *XConf) DumpInfo()

DumpInfo 打印调试信息

func (*XConf) Hash

func (x *XConf) Hash() (s string)

Hash 当前最新配置的Hash字符串,默认为DefaultInvalidHashString

func (*XConf) HashStructure

func (x *XConf) HashStructure(v interface{}) (s string)

HashStructure 返回指定数据的hash值

func (*XConf) Latest

func (x *XConf) Latest() (interface{}, error)

Latest 绑定当前XConf内缓存的数据到Parse时传入的类型中并以interface{}类型返回,需先调用Parse便于XConf确定配置类型

func (*XConf) Merge added in v0.3.9

func (x *XConf) Merge(opts ...Option) error

Merge 合并配置

func (*XConf) MustParse added in v0.3.4

func (x *XConf) MustParse(valPtr interface{}, opts ...Option)

MustParse 解析配置到传入的参数中,如发生错误则直接panic

func (*XConf) MustSaveToBytes

func (x *XConf) MustSaveToBytes(ct ConfigType) []byte

MustSaveToBytes 将内置解析的数据以字节流返回,需指定ConfigType

func (*XConf) MustSaveToFile

func (x *XConf) MustSaveToFile(fileName string)

MustSaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec,如发生错误会panic

func (*XConf) MustSaveToWriter

func (x *XConf) MustSaveToWriter(ct ConfigType, writer io.Writer)

MustSaveToWriter 将内置解析的数据dump到writer,需指定ConfigType,如发生错误会panic

func (*XConf) MustSaveVarToFile

func (x *XConf) MustSaveVarToFile(valPtr interface{}, fileName string)

MustSaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec,如发生错误会panic

func (*XConf) MustSaveVarToWriter

func (x *XConf) MustSaveVarToWriter(valPtr interface{}, ct ConfigType, writer io.Writer)

MustSaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct,如发生错误会panic

func (*XConf) NotifyUpdate

func (x *XConf) NotifyUpdate() <-chan interface{}

NotifyUpdate 通知更新

func (*XConf) Parse

func (x *XConf) Parse(valPtr interface{}, opts ...Option) error

Parse 解析配置到传入的参数中

func (*XConf) SaveToFile

func (x *XConf) SaveToFile(fileName string) error

SaveToFile 将内置解析的数据dump到文件,根据文件后缀选择codec

func (*XConf) SaveToWriter

func (x *XConf) SaveToWriter(ct ConfigType, writer io.Writer) error

SaveToWriter 将内置解析的数据dump到writer,类型为ct

func (*XConf) SaveVarToFile

func (x *XConf) SaveVarToFile(valPtr interface{}, fileName string) error

SaveVarToFile 将外部传入的valPtr,写入到fileName中,根据文件后缀选择codec

func (*XConf) SaveVarToWriter

func (x *XConf) SaveVarToWriter(valPtr interface{}, ct ConfigType, writer io.Writer) error

SaveVarToWriter 将外部传入的valPtr,写入到writer中,类型为ct

func (*XConf) SaveVarToWriterAsYAML added in v0.3.5

func (x *XConf) SaveVarToWriterAsYAML(valPtr interface{}, writer io.Writer) error

SaveVarToWriterAsYAML 将内置解析的数据解析到yaml,带comment

func (*XConf) StructMapStructure

func (x *XConf) StructMapStructure(s interface{}) map[string]interface{}

StructMapStructure 获取传入的s的数据的map[string]interface{}

func (*XConf) UpdateWithEnviron

func (x *XConf) UpdateWithEnviron(environ ...string) (err error)

UpdateWithEnviron 提供环境变量合法配置更新数据,异步通知更新

func (*XConf) UpdateWithFieldPathValues

func (x *XConf) UpdateWithFieldPathValues(kv ...string) (err error)

UpdateWithFieldPathValues 根据字段FieldPath更新数据, 支持的字段类型依赖于xflag

func (*XConf) UpdateWithFiles

func (x *XConf) UpdateWithFiles(files ...string) (err error)

UpdateWithFiles 提供files更新数据, 支持的字段类型依赖于xflag

func (*XConf) UpdateWithFlagArgs

func (x *XConf) UpdateWithFlagArgs(flagArgs ...string) (err error)

UpdateWithFlagArgs 提供FlagSet合法参数更新数据,异步通知更新

func (*XConf) UpdateWithReader

func (x *XConf) UpdateWithReader(readers ...io.Reader) (err error)

UpdateWithReader 提供files更新数据, 支持的字段类型依赖于xflag

func (*XConf) Usage

func (x *XConf) Usage()

Usage 打印usage信息

func (*XConf) UsageToWriter added in v0.2.6

func (x *XConf) UsageToWriter(w io.Writer, args ...string)

func (*XConf) WatchFieldPath

func (x *XConf) WatchFieldPath(fieldPath string, changed OnFieldUpdated)

WatchFieldPath 关注特定的字段变化

func (*XConf) WatchUpdate

func (x *XConf) WatchUpdate(confPath string, loader kv.Loader)

WatchUpdate confPath不会自动绑定env value,如果需要watch的路径与环境变量相关,先通过ParseEnvValue自行解析替换处理错误

func (*XConf) ZeroStructKeysTagList

func (x *XConf) ZeroStructKeysTagList(s interface{}) map[string]StructFieldPathInfo

ZeroStructKeysTagList 获取参数s的空结构的Filed信息

type XConfOptions added in v0.2.6

type XConfOptions interface {
	XConfOptions() []Option
}

XConfOptions 配置实现改接口,xconf会自动获取xconf配置覆盖默认设置

Jump to

Keyboard shortcuts

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