icfg

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2019 License: MIT Imports: 11 Imported by: 0

README

icfg

简单的配置读取和管理, flag和env变量保存在全局变量flagKV和envKV中,所有*Config都可 以访问,LoadCfg读取文件的配置只属于该*Config. 配置获取时的搜索顺序依次为 flag>config>env,最先搜索flag, SetDefaultKey设置的变量保存在envKV中.

支持接口

全局函数
  • NewConfig() *Config
  • TimeStamp() string
  • SetDefaultKey(k string, v interface{})
  • LoadEnv(keys []string) // Load env vars
  • Parse() //Parse flag vars
  • BoolVar(name string, value bool, usage string)
  • StringVar(name string, value string, usage string)
  • IntVar(name string, value int, usage string)
  • Int64Var(name string, value int64, usage string)
  • Uint64Var(name string, value uint64, usage string)
  • Float64Var(name string, value float64, usage string)
以下函数属于*Config, 但是包级别也有一样的函数集,封装了一个默认*Config
  • LoadCfg(path ...string) error
  • Ele(path string) (v *Element, ok bool)
  • Set(path string, value interface{})
  • Bool(path string) bool
  • String(path string) string
  • Int(path string) int
  • IntArray(path string) []int
  • IntMap(path string) map[string]int
  • Int64(path string) int64
  • Uint64(path string) uint64
  • Float(path string) float64
  • FloatArray(path string) []float64
  • FloatMap(path string) map[string]float64
  • Array(path string) []*Element
  • StrArray(path string) []string
  • Map(path string) map[string]*Element
  • StrMap(path string) map[string]string
  • Match(pattern string) *Element
Test
  • config.json
{
  "host": "127.0.0.1",
  "peer_name": "IPeer",
  "network": {
    "name": "INetwork",
    "listeners": [
      {
        "protocol":"udp",
        "port": "1008",
        "name": "udp_listener"
      },
      {
        "protocol":"tcp",
        "port": "1009",
        "name": "tcp_listener"

      },
      {
        "protocol":"kcp",
        "port": "1010",
        "name": "kcp_listener"
      }
    ]
  },
  "A": {
    "B": {
      "C": "Boom"
    }
  },
  "int_arr": [0,1,2,3,4,5,6,7,8,9,10,11],
  "float_arr": [0.1, 0.2, 0.3],
  "match": {
    "sub_match": {
      "acb": 1,
      "bac": 2,
      "cab": 3
    },
    "submatch": {
      "a": 1
    }
  },
  "maps" : {
    "str": {
      "a":"a"
    },
    "float": {
      "f":0.01
    },
    "int": {
      "i":1
    }
  }
}
  • Simple Test
unc TestICFG(t *testing.T) {
	icfg.StringVar("system", "icfg", "system name")
	icfg.StringVar("config", "./config_demo.json", "json config path")
	icfg.LoadEnv([]string{"GOPATH", "GOROOT"})
	icfg.Parse()
	cfg := icfg.String("config")
	icfg.LoadCfg(cfg)

	fmt.Println(icfg.String("system")) // icfg
	fmt.Println(icfg.String("GOROOT")) // /opt/soft/go
	fmt.Println(icfg.IntMap("maps.int")) // map[i:1]
	fmt.Println(icfg.FloatMap("maps.float")) // map[f:0.01]
	fmt.Println(icfg.StrMap("maps.str")) //map[a:a]
	fmt.Println(icfg.IntArray("int_arr")) // [0 1 2 3 4 5 6 7 8 9 10 11]
	fmt.Println(icfg.Int("int_arr.1")) // 1

	pattern := "int_arr.[^1234567]"
	fmt.Println(icfg.Match(pattern).IntMap()) // map[int_arr.0:0 int_arr.9:9 int_arr.8:8]
	m := icfg.Map("maps.int")
	fmt.Println(m["i"].Int())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(path string) bool

func BoolVar

func BoolVar(name string, value bool, usage string)

func Float

func Float(path string) float64

func Float64Var

func Float64Var(name string, value float64, usage string)

func FloatArray

func FloatArray(path string) []float64

func FloatMap

func FloatMap(path string) map[string]float64

func Int

func Int(path string) int

func Int64

func Int64(path string) int64

func Int64Var

func Int64Var(name string, value int64, usage string)

func IntArray

func IntArray(path string) []int

func IntMap

func IntMap(path string) map[string]int

func IntVar

func IntVar(name string, value int, usage string)

func LoadCfg

func LoadCfg(path ...string) error

Global config

func LoadEnv

func LoadEnv(keys []string)

func Map

func Map(path string) map[string]*Element

func Parse

func Parse()

func Set

func Set(path string, value interface{})

func SetDefaultKey

func SetDefaultKey(k string, v interface{})

func StrArray

func StrArray(path string) []string

func StrMap

func StrMap(path string) map[string]string

func String

func String(path string) string

func StringVar

func StringVar(name string, value string, usage string)

func TimeStamp

func TimeStamp() string

func Uint64

func Uint64(path string) uint64

func Uint64Var

func Uint64Var(name string, value uint64, usage string)

Types

type Config

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

func NewConfig

func NewConfig() *Config

func (*Config) Array

func (c *Config) Array(path string) []*Element

func (*Config) Bool

func (c *Config) Bool(path string) bool

func (*Config) CfgString

func (c *Config) CfgString() string

func (*Config) Dump

func (c *Config) Dump() error

func (*Config) Ele

func (c *Config) Ele(path string) (v *Element, ok bool)

Ele search level flag>config>env

func (*Config) Float

func (c *Config) Float(path string) float64

func (*Config) FloatArray

func (c *Config) FloatArray(path string) []float64

func (*Config) FloatMap

func (c *Config) FloatMap(path string) map[string]float64

func (*Config) Int

func (c *Config) Int(path string) int

func (*Config) Int64

func (c *Config) Int64(path string) int64

func (*Config) IntArray

func (c *Config) IntArray(path string) []int

func (*Config) IntMap

func (c *Config) IntMap(path string) map[string]int

func (*Config) LoadCfg

func (c *Config) LoadCfg(path ...string) error

func (*Config) Map

func (c *Config) Map(path string) map[string]*Element

func (*Config) Match

func (c *Config) Match(pattern string) *Element

func (*Config) Set

func (c *Config) Set(path string, value interface{})

func (*Config) StrArray

func (c *Config) StrArray(path string) []string

func (*Config) StrMap

func (c *Config) StrMap(path string) map[string]string

func (*Config) String

func (c *Config) String(path string) string

func (*Config) Uint64

func (c *Config) Uint64(path string) uint64

type Element

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

func Array

func Array(path string) []*Element

func Ele

func Ele(path string) (v *Element, ok bool)

Ele search level flag>config>env

func Match

func Match(pattern string) *Element

func (*Element) Array

func (e *Element) Array() []*Element

func (*Element) Bool

func (e *Element) Bool() bool

func (*Element) Float

func (e *Element) Float() float64

func (*Element) FloatArray

func (e *Element) FloatArray() []float64

func (*Element) FloatMap

func (e *Element) FloatMap() map[string]float64

func (*Element) Int

func (e *Element) Int() int

func (*Element) Int64

func (e *Element) Int64() int64

func (*Element) IntArray

func (e *Element) IntArray() []int

func (*Element) IntMap

func (e *Element) IntMap() map[string]int

func (*Element) Map

func (e *Element) Map() map[string]*Element

func (*Element) StrArray

func (e *Element) StrArray() []string

func (*Element) StrMap

func (e *Element) StrMap() map[string]string

func (*Element) String

func (e *Element) String() string

func (*Element) Type

func (e *Element) Type() string

func (*Element) Uint64

func (e *Element) Uint64() uint64

Jump to

Keyboard shortcuts

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