config

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 16 Imported by: 3

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClsConfig

func ClsConfig(filename string, projectname string, monitoring bool) (cfg *viper.Viper)

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

func ClsConfigDefault(cfg Config, name string, gen bool) error

With all necessary nodes

func Get

func Get(pattern string, def ...interface{}) (x *gvar.Var)

Functions implemented using goframe, gvar returned

func GetBoolWithDefault

func GetBoolWithDefault(cfg string, def bool) bool

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetDurationWithDefault

func GetDurationWithDefault(cfg string, def time.Duration) time.Duration

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetFloat32WithDefault

func GetFloat32WithDefault(cfg string, def float32) float32

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetFloat64WithDefault

func GetFloat64WithDefault(cfg string, def float64) float64

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetInt64WithDefault

func GetInt64WithDefault(cfg string, def int64) int64

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetIntSliceWithDefault

func GetIntSliceWithDefault(cfg string, def []int) []int

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetIntWithDefault

func GetIntWithDefault(cfg string, def int) int

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetStringSliceWithDefault

func GetStringSliceWithDefault(cfg string, def []string) []string

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func GetStringWithDefault

func GetStringWithDefault(cfg string, def string) string

GetWithDefault return the match result form config file, Retrun default value(def) if not found

func Init

func Init(project string)

Leave it to the user to initialize the configuration file

func JsonDecode

func JsonDecode(data interface{}, options ...Options) (interface{}, error)

func JsonDecodeTo

func JsonDecodeTo(data interface{}, v interface{}, options ...Options) (err error)

func JsonEncode

func JsonEncode(value interface{}) ([]byte, error)

func JsonEncodeString

func JsonEncodeString(value interface{}) (string, error)

func JsonIsValidDataType

func JsonIsValidDataType(dataType string) bool

func JsonMarshal

func JsonMarshal(v interface{}) (marshaledBytes []byte, err error)

func JsonMarshalIndent

func JsonMarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)

func JsonMustEncode

func JsonMustEncode(value interface{}) []byte

func JsonMustEncodeString

func JsonMustEncodeString(value interface{}) string

func JsonUnmarshal

func JsonUnmarshal(data []byte, v interface{}) (err error)

func JsonValid

func JsonValid(data interface{}) bool

func XmlDecode added in v0.1.8

func XmlDecode(content []byte) (map[string]interface{}, error)

func XmlDecodeWithoutRoot added in v0.1.8

func XmlDecodeWithoutRoot(content []byte) (map[string]interface{}, error)

func XmlEncode added in v0.1.8

func XmlEncode(m map[string]interface{}, rootTag ...string) ([]byte, error)

func XmlEncodeWithIndent added in v0.1.8

func XmlEncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error)

func XmlToJson

func XmlToJson(content []byte) ([]byte, error)

Types

type Config

type Config = *viper.Viper
var Cfg Config

WARNING: this will be always nil before Init() called.

type Json

type Json = gjson.Json

type JsonFile

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

func (*JsonFile) Load

func (obj *JsonFile) Load(path string) error

Open and read json file

func (*JsonFile) Parse

func (obj *JsonFile) Parse(model interface{}) error

Json to struct

func (*JsonFile) Save

func (obj *JsonFile) Save(model interface{}, filepath string) error

Struct

func (*JsonFile) String

func (obj *JsonFile) String() string

Json string

type Options

type Options = gjson.Options

type XML

type XML struct {
	Doc  *etree.Document
	File string //filepath
}

XML implement base on "github.com/beevik/etree"

func NewXML

func NewXML() *XML

func XMLFile

func XMLFile(file ...string) *XML

func XMLString

func XMLString(s ...string) *XML

func (*XML) AddTitle

func (xml *XML) AddTitle(node string, attrs ...string) *XML

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) Dump

func (xml *XML) Dump(prefix, suffix string)

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) Get

func (xml *XML) Get(tag string) *XMLElement

Select the first one match

func (*XML) GetAll

func (xml *XML) GetAll(tag string) []*XMLElement

func (*XML) Save

func (xml *XML) Save(filenamepath ...string) error

Save to file --force

func (*XML) String

func (xml *XML) String() string

type XMLAttr

type XMLAttr struct {
	K string
	V string
}

type XMLElement

type XMLElement = etree.Element

func NewElement

func NewElement(tag string, value string, attrs ...XMLAttr) *XMLElement

Jump to

Keyboard shortcuts

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