config

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: GPL-3.0 Imports: 12 Imported by: 6

README

Config Reader

Go package for reading cofig file by JSON, XML, YAML.

Installation

go get github.com/go-trellis/config
imports

import gopkg.in/yaml.v2

Usage

Config

not supported "*.xml": now go encoding/xml is not supported map[string]interface{}

  • dot separator to get values, and if return nil, you should set default value
  • A: ${X.Y.Z} for finding out X.Y.Z's value and setting into A. See copy example:See config
  • You can do like this: c.GetString("a.b.c") Or c.GetString("a.b.c", "default")
  • You can write notes into the json file.
  • Supported: .json, .yaml
c, e := NewConfig(name)
c.GetString("a.b.c")
Feature
// Config manager data functions
type Config interface {
	// get a object
	GetInterface(key string, defValue ...interface{}) (res interface{})
	// get a string
	GetString(key string, defValue ...string) (res string)
	// get a bool
	GetBoolean(key string, defValue ...bool) (b bool)
	// get a int
	GetInt(key string, defValue ...int) (res int)
	// get a float
	GetFloat(key string, defValue ...float64) (res float64)
	// get list of objects
	GetList(key string) (res []interface{})
	// get list of strings
	GetStringList(key string) []string
	// get list of bools
	GetBooleanList(key string) []bool
	// get list of ints
	GetIntList(key string) []int
	// get list of float64s
	GetFloatList(key string) []float64
	// get time duration by (int)(uint), exp: 1s, 1day
	GetTimeDuration(key string, defValue ...time.Duration) time.Duration
	// get byte size by (int)(uint), exp: 1k, 1m
	GetByteSize(key string) *big.Int
	// get map value
	GetMap(key string) Options
	// get key's config
	GetConfig(key string) Config
	// get key's values if values can be Config, or panic
	GetValuesConfig(key string) Config
	// set key's value into config
	SetKeyValue(key string, value interface{}) (err error)
	// get all config
	Dump() (bs []byte, err error)
	// get all keys
	GetKeys() []string
	// deep copy configs
	Copy() Config
}
More Example

[See More Example]

Reader Repo
// Reader reader repo
type Reader interface {
	// read file into model
	Read(model interface{}) error
	// dump configs' cache
	Dump(model interface{}) ([]byte, error)
	// parse data to model
	ParseData(data []byte, model interface{}) error
}
r := NewReader(ReaderType, filename)
if err := r.Read(model); err != nil {
	return
}
Readers
jReader := NewJSONReader() or NewJSONReader(ReaderOptionFilename(filename))
xReader := NewXMLReader()  or NewXMLReader(ReaderOptionFilename(filename))
yReader := NewYAMLReader() or NewYAMLReader(ReaderOptionFilename(filename))

if not set filename with reader option function, you can't use Read function, it will return: no such file or directory

  • .json = NewJSONReader()

  • .xml = NewXMLReader()

  • .yaml | .yml = NewYAMLReader()

  • if you want to use a fuzzy reader by filename's suffix

sReader := NewSuffixReader(ReaderOptionFilename(filename))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotMap                 = errors.New("interface is not a map")
	ErrValueNil               = errors.New("value is nil")
	ErrInvalidKey             = errors.New("invalid key")
	ErrInvalidFilePath        = errors.New("invalid file path")
	ErrUnknownSuffixes        = errors.New("unknown file with suffix")
	ErrNotSupportedReaderType = errors.New("not supported reader type")
)

Errors

Functions

func DeepCopy added in v1.2.0

func DeepCopy(value interface{}) interface{}

DeepCopy 深度拷贝

func ParseJSONConfig added in v1.3.0

func ParseJSONConfig(data []byte, model interface{}) error

ParseJSONConfig 解析Json配置

func ParseXMLConfig added in v1.3.0

func ParseXMLConfig(data []byte, model interface{}) error

ParseXMLConfig 解析yaml的配置信息

func ParseYAMLConfig added in v1.3.0

func ParseYAMLConfig(data []byte, model interface{}) error

ParseYAMLConfig 解析yaml的配置信息

func ReadJSONFile added in v1.3.0

func ReadJSONFile(filepath string, model interface{}) error

ReadJSONFile 读取Json文件数据到Models

func ReadXMLFile added in v1.3.0

func ReadXMLFile(name string) ([]byte, error)

ReadXMLFile 读取yaml文件的配置信息

func ReadYAMLFile added in v1.3.0

func ReadYAMLFile(name string) ([]byte, error)

ReadYAMLFile 读取yaml文件的配置信息

Types

type AdapterConfig added in v1.3.0

type AdapterConfig struct {
	ConfigFile   string
	ConfigString string
	ConfigStruct interface{}
	// contains filtered or unexported fields
}

AdapterConfig default config adapter

func (*AdapterConfig) Copy added in v1.3.0

func (p *AdapterConfig) Copy() Config

Copy return a copy

func (*AdapterConfig) Dump added in v1.3.0

func (p *AdapterConfig) Dump() (bs []byte, err error)

Dump return p.configs' bytes

func (*AdapterConfig) GetBoolean added in v1.3.0

func (p *AdapterConfig) GetBoolean(key string, defValue ...bool) (b bool)

GetBoolean return a bool object in p.configs by key

func (*AdapterConfig) GetBooleanList added in v1.3.0

func (p *AdapterConfig) GetBooleanList(key string) []bool

GetBooleanList return a list of booleans in p.configs by key

func (*AdapterConfig) GetByteSize added in v1.3.0

func (p *AdapterConfig) GetByteSize(key string) *big.Int

GetByteSize return time in p.configs by key

func (*AdapterConfig) GetConfig added in v1.3.0

func (p *AdapterConfig) GetConfig(key string) Config

GetConfig return object config in p.configs by key

func (*AdapterConfig) GetFloat added in v1.3.0

func (p *AdapterConfig) GetFloat(key string, defValue ...float64) (res float64)

GetFloat return a float object in p.configs by key

func (*AdapterConfig) GetFloatList added in v1.3.0

func (p *AdapterConfig) GetFloatList(key string) []float64

GetFloatList return a list of floats in p.configs by key

func (*AdapterConfig) GetInt added in v1.3.0

func (p *AdapterConfig) GetInt(key string, defValue ...int) (res int)

GetInt return a int object in p.configs by key

func (*AdapterConfig) GetIntList added in v1.3.0

func (p *AdapterConfig) GetIntList(key string) []int

GetIntList return a list of ints in p.configs by key

func (*AdapterConfig) GetInterface added in v1.3.0

func (p *AdapterConfig) GetInterface(key string, defValue ...interface{}) (res interface{})

GetInterface return a interface object in p.configs by key

func (*AdapterConfig) GetKeyValue added in v1.5.0

func (p *AdapterConfig) GetKeyValue(key string) (vm interface{}, err error)

GetKeyValue get value with key

func (*AdapterConfig) GetKeys added in v1.3.0

func (p *AdapterConfig) GetKeys() []string

GetKeys get map keys

func (*AdapterConfig) GetList added in v1.3.0

func (p *AdapterConfig) GetList(key string) (res []interface{})

GetList return a list of interface{} in p.configs by key

func (*AdapterConfig) GetMap added in v1.3.0

func (p *AdapterConfig) GetMap(key string) Options

GetMap get map value

func (*AdapterConfig) GetString added in v1.3.0

func (p *AdapterConfig) GetString(key string, defValue ...string) (res string)

GetString return a string object in p.configs by key

func (*AdapterConfig) GetStringList added in v1.3.0

func (p *AdapterConfig) GetStringList(key string) []string

GetStringList return a list of strings in p.configs by key

func (*AdapterConfig) GetTimeDuration added in v1.3.0

func (p *AdapterConfig) GetTimeDuration(key string, defValue ...time.Duration) time.Duration

GetTimeDuration return time in p.configs by key

func (*AdapterConfig) GetValuesConfig added in v1.3.0

func (p *AdapterConfig) GetValuesConfig(key string) Config

GetValuesConfig get key's values if values can be Config, or panic

func (*AdapterConfig) SetKeyValue added in v1.3.0

func (p *AdapterConfig) SetKeyValue(key string, value interface{}) (err error)

SetKeyValue set key value into p.configs

func (*AdapterConfig) ToObject added in v1.5.0

func (p *AdapterConfig) ToObject(key string, model interface{}) error

ToObject unmarshal values to object

type Config

type Config interface {
	// get a object
	GetInterface(key string, defValue ...interface{}) (res interface{})
	// get a string
	GetString(key string, defValue ...string) (res string)
	// get a bool
	GetBoolean(key string, defValue ...bool) (b bool)
	// get a int
	GetInt(key string, defValue ...int) (res int)
	// get a float
	GetFloat(key string, defValue ...float64) (res float64)
	// get list of objects
	GetList(key string) (res []interface{})
	// get list of strings
	GetStringList(key string) []string
	// get list of bools
	GetBooleanList(key string) []bool
	// get list of ints
	GetIntList(key string) []int
	// get list of float64s
	GetFloatList(key string) []float64
	// get time duration by (int)(uint), exp: 1s, 1day
	GetTimeDuration(key string, defValue ...time.Duration) time.Duration
	// get byte size by (int)(uint), exp: 1k, 1m
	GetByteSize(key string) *big.Int
	// get map value
	GetMap(key string) Options
	// get key's config
	GetConfig(key string) Config
	// ToObject unmarshal values to object
	ToObject(key string, model interface{}) error
	// get key's values if values can be Config, or panic
	GetValuesConfig(key string) Config
	// set key's value into config
	SetKeyValue(key string, value interface{}) (err error)
	// get all config
	Dump() (bs []byte, err error)
	// get all keys
	GetKeys() []string
	// deep copy configs
	Copy() Config
}

Config manager data functions

func NewAdapterConfig added in v1.3.0

func NewAdapterConfig(filepath string) (Config, error)

NewAdapterConfig return default config adapter name is file's path

func NewConfig

func NewConfig(name string) (Config, error)

NewConfig return Config by file's path, judge path's suffix, supported .json, .yml, .yaml

func NewConfigOptions added in v1.3.0

func NewConfigOptions(opts ...OptionFunc) (Config, error)

NewConfigOptions 从操作函数解析Config

type OptionFunc added in v1.5.0

type OptionFunc func(*AdapterConfig)

OptionFunc 处理函数

func OptionFile added in v1.3.0

func OptionFile(filename string) OptionFunc

OptionFile 解析配置文件Option函数

func OptionString added in v1.3.0

func OptionString(rt ReaderType, cStr string) OptionFunc

OptionString 字符串解析配置Option函数

func OptionStruct added in v1.5.0

func OptionStruct(rt ReaderType, st interface{}) OptionFunc

OptionStruct 结构体解析配置Option函数

type Options

type Options map[string]interface{}

Options initial params

func (*Options) ToConfig added in v1.5.0

func (p *Options) ToConfig(rts ...ReaderType) Config

ToConfig Options to config, default YAML reader

type Reader

type Reader interface {
	// read file into model
	Read(model interface{}) error
	// dump configs' cache
	Dump(model interface{}) ([]byte, error)
	// parse data to model
	ParseData(data []byte, model interface{}) error
}

Reader reader repo

func NewJSONReader

func NewJSONReader(opts ...ReaderOptionFunc) Reader

NewJSONReader return a json reader

func NewReader added in v1.3.0

func NewReader(rt ReaderType, filename string) (Reader, error)

NewReader return a reader by ReaderType

func NewSuffixReader

func NewSuffixReader(opts ...ReaderOptionFunc) (reader Reader, err error)

NewSuffixReader return a suffix reader supportted: .json, .xml, .yaml, .yml

func NewXMLReader

func NewXMLReader(opts ...ReaderOptionFunc) Reader

NewXMLReader return xml config reader

func NewYAMLReader

func NewYAMLReader(opts ...ReaderOptionFunc) Reader

NewYAMLReader return a yaml reader

type ReaderOptionFunc added in v1.5.0

type ReaderOptionFunc func(*ReaderOptions)

ReaderOptionFunc declare reader option function

func ReaderOptionFilename added in v1.5.0

func ReaderOptionFilename(filename string) ReaderOptionFunc

ReaderOptionFilename set reader filename

type ReaderOptions added in v1.5.0

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

ReaderOptions reader options

type ReaderType

type ReaderType int

ReaderType define reader type

const (
	// ReaderTypeSuffix judge by file suffix
	ReaderTypeSuffix ReaderType = iota
	// ReaderTypeJSON json reader type
	ReaderTypeJSON
	// ReaderTypeYAML yaml reader type
	ReaderTypeYAML
	// ReaderTypeXML xml reader type
	ReaderTypeXML
)

Jump to

Keyboard shortcuts

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