Documentation ¶
Overview ¶
Package config provides functions while implemented by Viper and gcfg. See more on https://pkg.go.dev/github.com/spf13/viper
Example ¶
Use global unique config instance
package main import ( "fmt" "github.com/lovelacelee/clsgo/v1/config" "github.com/lovelacelee/clsgo/v1/utils" ) func clean() { utils.DeletePath("logs") utils.DeleteFiles(utils.Cwd(), "/*.yaml$") utils.DeleteFiles(utils.Cwd(), "/*.xml$") } func main() { // import "github.com/lovelacelee/clsgo/config" config.Init("clsgo") fmt.Print(config.Cfg.Get("project.name")) clean() }
Output: clsgo
Index ¶
- func ClsConfig(filename string, projectname string, monitoring bool) (cfg *viper.Viper)
- func ClsConfigDefault(cfg Config, name string, gen bool) error
- func Get(pattern string, def ...interface{}) (x *gvar.Var)
- func GetBoolWithDefault(cfg string, def bool) bool
- func GetDurationWithDefault(cfg string, def time.Duration) time.Duration
- func GetFloat32WithDefault(cfg string, def float32) float32
- func GetFloat64WithDefault(cfg string, def float64) float64
- func GetInt64WithDefault(cfg string, def int64) int64
- func GetIntSliceWithDefault(cfg string, def []int) []int
- func GetIntWithDefault(cfg string, def int) int
- func GetStringSliceWithDefault(cfg string, def []string) []string
- func GetStringWithDefault(cfg string, def string) string
- func Init(project string)
- func JsonDecode(data interface{}, options ...Options) (interface{}, error)
- func JsonDecodeTo(data interface{}, v interface{}, options ...Options) (err error)
- func JsonEncode(value interface{}) ([]byte, error)
- func JsonEncodeString(value interface{}) (string, error)
- func JsonIsValidDataType(dataType string) bool
- func JsonMarshal(v interface{}) (marshaledBytes []byte, err error)
- func JsonMarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)
- func JsonMustEncode(value interface{}) []byte
- func JsonMustEncodeString(value interface{}) string
- func JsonUnmarshal(data []byte, v interface{}) (err error)
- func JsonValid(data interface{}) bool
- func XmlDecode(content []byte) (map[string]interface{}, error)
- func XmlDecodeWithoutRoot(content []byte) (map[string]interface{}, error)
- func XmlEncode(m map[string]interface{}, rootTag ...string) ([]byte, error)
- func XmlEncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error)
- func XmlToJson(content []byte) ([]byte, error)
- type Config
- type Json
- type JsonFile
- type Options
- type XML
- func (xml *XML) AddTitle(node string, attrs ...string) *XML
- func (xml *XML) CreateElement(tag string, path string, value string, comment string, attrs ...XMLAttr) *XMLElement
- func (xml *XML) Dump(prefix, suffix string)
- func (xml *XML) Find(fmt string) *XMLElement
- func (xml *XML) FindAll(fmt string) []*XMLElement
- func (xml *XML) Get(tag string) *XMLElement
- func (xml *XML) GetAll(tag string) []*XMLElement
- func (xml *XML) Save(filenamepath ...string) error
- func (xml *XML) String() string
- type XMLAttr
- type XMLElement
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClsConfig ¶
Param: <monitoring> will start a routine to watch file changes, and reload it. and the goroutine never ends. If <monitoring> is true and <filename> config file not exist, it will be create by default. <filename> does not include extension.
Example ¶
package main import ( "fmt" "github.com/lovelacelee/clsgo/v1/config" "github.com/lovelacelee/clsgo/v1/utils" ) func clean() { utils.DeletePath("logs") utils.DeleteFiles(utils.Cwd(), "/*.yaml$") utils.DeleteFiles(utils.Cwd(), "/*.xml$") } func main() { cfg := config.ClsConfig("server", "http", true) fmt.Print(cfg.Get("project.name")) clean() }
Output: http
func ClsConfigDefault ¶
With all necessary nodes
func GetBoolWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetDurationWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetFloat32WithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetFloat64WithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetInt64WithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetIntSliceWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetIntWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetStringSliceWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func GetStringWithDefault ¶
GetWithDefault return the match result form config file, Retrun default value(def) if not found
func JsonDecode ¶
func JsonDecodeTo ¶
func JsonEncode ¶
func JsonEncodeString ¶
func JsonIsValidDataType ¶
func JsonMarshal ¶
func JsonMarshalIndent ¶
func JsonMustEncode ¶
func JsonMustEncode(value interface{}) []byte
func JsonMustEncodeString ¶
func JsonMustEncodeString(value interface{}) string
func JsonUnmarshal ¶
func XmlDecodeWithoutRoot ¶ added in v0.1.8
func XmlEncodeWithIndent ¶ added in v0.1.8
Types ¶
type XML ¶
XML implement base on "github.com/beevik/etree"
func (*XML) AddTitle ¶
Title head is like "xml" or "xml-stylesheet", attrs is like `version="1.0" encoding="UTF-8"`, `type="text/xsl" href="style.xsl"`
func (*XML) CreateElement ¶
func (xml *XML) CreateElement(tag string, path string, value string, comment string, attrs ...XMLAttr) *XMLElement
Element Chain operation is supported
func (*XML) Find ¶
func (xml *XML) Find(fmt string) *XMLElement
Finds the first element in the XPATH path p is like "./bookstore/book[p:price='49.99']/title"
func (*XML) FindAll ¶
func (xml *XML) FindAll(fmt string) []*XMLElement
Finds all the elements in the XPATH path fmt is like "//book[@category='WEB']/title" or "./bookstore/book[1]/*"
func (*XML) GetAll ¶
func (xml *XML) GetAll(tag string) []*XMLElement
type XMLElement ¶
func NewElement ¶
func NewElement(tag string, value string, attrs ...XMLAttr) *XMLElement