config

package
v0.0.0-...-cda2eac Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: GPL-3.0 Imports: 13 Imported by: 9

README

Config Reader

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

Installation

go get trellis.tech/trellis/common.v0/config
imports

import gopkg.in/yaml.v3

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                 = errcode.New("interface is not a map")
	ErrInvalidKey             = errcode.New("invalid key")
	ErrInvalidFilePath        = errcode.New("invalid file path")
	ErrUnknownSuffixes        = errcode.New("unknown file with suffix")
	ErrNotSupportedReaderType = errcode.New("not supported reader type")
)

Errors

Functions

func DeepCopy

func DeepCopy(value interface{}) interface{}

DeepCopy 深度拷贝

func ParseJSONData

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

ParseJSONData 解析Json配置

func ParseXMLConfig

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

ParseXMLConfig 解析yaml的配置信息

func ParseYAMLData

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

ParseYAMLData 解析yaml的配置信息

func ParseYAMLFileToModel

func ParseYAMLFileToModel(name string, model interface{}) error

ParseYAMLFileToModel 读取yaml文件的配置信息

func ReadFile

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

func ReadJSONFileToModel

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

ReadJSONFileToModel 读取Json文件数据到Models

Types

type AdapterConfig

type AdapterConfig struct {
	ConfigFile   string
	ConfigString string
	ConfigStruct interface{}

	EnvPrefix  string
	EnvAllowed bool
	// contains filtered or unexported fields
}

AdapterConfig default config adapter

func (*AdapterConfig) Copy

func (p *AdapterConfig) Copy() Config

Copy return a copy

func (*AdapterConfig) Dump

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

Dump return p.configs' bytes

func (*AdapterConfig) GetBoolean

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

GetBoolean return a bool object in p.configs by key

func (*AdapterConfig) GetBooleanList

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

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

func (*AdapterConfig) GetByteSize

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

GetByteSize return time in p.configs by key

func (*AdapterConfig) GetConfig

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

GetConfig return object config in p.configs by key

func (*AdapterConfig) GetFloat

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

GetFloat return a float object in p.configs by key

func (*AdapterConfig) GetFloatList

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

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

func (*AdapterConfig) GetInt

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

GetInt return a int object in p.configs by key

func (*AdapterConfig) GetIntList

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

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

func (*AdapterConfig) GetInterface

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

GetInterface return a interface object in p.configs by key

func (*AdapterConfig) GetKeyValue

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

GetKeyValue get value with key

func (*AdapterConfig) GetKeys

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

GetKeys get map keys

func (*AdapterConfig) GetList

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

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

func (*AdapterConfig) GetMap

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

GetMap get map value

func (*AdapterConfig) GetString

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

GetString return a string object in p.configs by key

func (*AdapterConfig) GetStringList

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

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

func (*AdapterConfig) GetTimeDuration

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

GetTimeDuration return time in p.configs by key

func (*AdapterConfig) GetValuesConfig

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

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

func (*AdapterConfig) IsEmpty

func (p *AdapterConfig) IsEmpty() bool

func (*AdapterConfig) Object

func (p *AdapterConfig) Object(model interface{}, opts ...ObjOption) error

func (*AdapterConfig) SetKeyValue

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

SetKeyValue set key value into p.configs

func (*AdapterConfig) ToObject

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

ToObject unmarshal values to object

type Config

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

Config manager data functions

func NewAdapterConfig

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

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

NewConfigOptions 从操作函数解析Config

type ObjOption

type ObjOption func(*ObjOptions)

func ObjOptionKey

func ObjOptionKey(key string) ObjOption

type ObjOptions

type ObjOptions struct {
	Key string
}

type OptionFunc

type OptionFunc func(*AdapterConfig)

OptionFunc 处理函数

func OptionENVAllowed

func OptionENVAllowed() OptionFunc

OptionENVAllowed 允许获取系统环境变量

func OptionENVPrefix

func OptionENVPrefix(prefix string) OptionFunc

OptionENVPrefix 设置环境变量已自定义字符串开始

func OptionFile

func OptionFile(filename string) OptionFunc

OptionFile 解析配置文件Option函数

func OptionString

func OptionString(rt ReaderType, cStr string) OptionFunc

OptionString 字符串解析配置Option函数

func OptionStruct

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

OptionStruct 结构体解析配置Option函数

type Options

type Options map[string]interface{}

Options initial params

func (*Options) ToConfig

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)
	// ParseData 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

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 supported: .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

type ReaderOptionFunc func(*ReaderOptions)

ReaderOptionFunc declare reader option function

func ReaderOptionFilename

func ReaderOptionFilename(filename string) ReaderOptionFunc

ReaderOptionFilename set reader filename

type ReaderOptions

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