duckcfg

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 9 Imported by: 0

README

duckCfg

A simpler and more unified way to read configuration files.

中文

GoDoc

Usage

go get github.com/520MianXiangDuiXiang520/duckCfg
package duckcfg_test

import (
    "fmt"
    "github.com/520MianXiangDuiXiang520/duckCfg"
)

func GetIntFormatDefault() {
    err := duckcfg.InitConfig("./test_data/01.yaml")
    if err != nil {
        panic(err)
    }
    timeout := duckcfg.Cfg().GetIntFormatDefault("db.mongo.conn_timeout", 0)
    fmt.Println(timeout) // 3
}


If you want to read from JSON, you only need to modify the file name.

Q&A

Q: What if my key contains a dot?

  • Use a backslash to escape it, e.g., if your key is db.mongo, you can use db\.mongo to read it. The same applies to keys containing a backslash.

Q: My configuration file format is not supported.

  • You can define a FConfigLoader and use the Register function to register it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorFailToLoadConfig = errors.New("fail to load config")
	ErrorLoadConfigFail   = errors.New("load config fail")
	ErrorBadPathStr       = errors.New("bad path str")
	ErrorKeyNotFound      = errors.New("key not found")
	ErrorTypeMismatch     = errors.New("type mismatch")
)

Functions

func InitConfig

func InitConfig(path string) error

func JsonLoader

func JsonLoader(path string) (any, error)

func Register

func Register(fileType string, loader FConfigLoader)

Register 为某种文件类型注册一个加载器

func YamlLoader

func YamlLoader(path string) (any, error)

Types

type FConfigLoader

type FConfigLoader func(path string) (any, error)

FConfigLoader 加载器,将配置文件数据反序列化成 interface{} 并返回

type IConfig

type IConfig interface {

	// GetString retrieve the string value for the
	// specified key from the configuration file.
	GetString(k string) (r string, err error)

	// GetStringDefault retrieve the string value for the
	// specified key from the configuration file,
	// or return the default value if it doesn't exist.
	GetStringDefault(k, d string) string

	// GetInt Retrieve the integer value for the
	// specified key from the configuration file.
	GetInt(k string) (r int, err error)

	// GetIntDefault Retrieve the integer value for the
	// specified key from the configuration file,
	// or return the default value if it doesn't exist.
	GetIntDefault(k string, d int) int

	// GetInt64 Retrieve the int64 value for the specified
	// key from the configuration file.
	GetInt64(k string) (r int64, err error)

	// GetInt64Default Retrieve the int64 value for the
	// specified key from the configuration file,
	// or return the default value if it doesn't exist.
	GetInt64Default(k string, d int64) int64

	// GetFloat Retrieve the float value for the specified
	// key from the configuration file.
	GetFloat(k string) (r float64, err error)

	// GetFloatDefault Retrieve the float value for the
	// specified key from the configuration file,
	// or return the default value if it doesn't exist.
	GetFloatDefault(k string, d float64) float64

	// GetIntFormat If the value for the specified key
	// in the configuration file is of type float64, int64, or int,
	// force conversion to int and return.
	GetIntFormat(k string) (r int, err error)

	// GetIntFormatDefault similar to GetIntFormat,
	// except that if the key does not exist or the conversion fails,
	// the default value is returned
	GetIntFormatDefault(k string, d int) (r int)

	// GetFloatFormat If the value for the specified key in the
	// configuration file is of type float64, int64, or int,
	// force conversion to float64 and return.
	GetFloatFormat(k string) (r float64, err error)

	// GetFloatFormatDefault similar to GetFloatFormat,
	// except that if the key does not exist or the conversion fails,
	// the default value is returned
	GetFloatFormatDefault(k string, d float64) (r float64)

	// GetBool Retrieve the boolean value for the
	// specified key from the configuration file.
	GetBool(k string) (r bool, err error)

	// GetBoolDefault Retrieve the boolean value for the
	// specified key from the configuration file,
	// or return the default value if it doesn't exist.
	GetBoolDefault(k string, d bool) bool

	// GetVal Read the value corresponding to key from
	//the configuration file and deserialize it to the
	// parameter to,to should be a pointer
	GetVal(k string, to any) (r any, err error)

	// GetStrings Read the string list for the
	// specified key from the configuration file.
	GetStrings(k string) (r []string, err error)

	// GetIntegers Read the integers list for the
	// specified key from the configuration file.
	GetIntegers(k string) (r []int64, err error)

	// GetFloats Read the float list for the
	// specified key from the configuration file.
	GetFloats(k string) (r []float64, err error)

	// GetBooleans Read the boolean list for the
	// specified key from the configuration file.
	GetBooleans(k string) (r []bool, err error)

	// GetMap Read the mapt for the
	// specified key from the configuration file.
	GetMap(k string) (r map[string]any, err error)
}

IConfig interface defines a set of generic methods for getting values from configuration files.

func Cfg

func Cfg() IConfig

Jump to

Keyboard shortcuts

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