Documentation ¶
Index ¶
- Constants
- func Abs(path string) string
- func AllSettings() map[string]any
- func Bool(path string) bool
- func Duration(path string) time.Duration
- func Float64(path string) float64
- func Get(path string) any
- func GetIP(useIPv6 bool) string
- func Int(path string) int
- func IntSlice(path string) []int
- func IsSet(path string) bool
- func Join(ps ...string) string
- func ParseEnv(src []byte) []byte
- func String(path string) string
- func StringMap(path string) map[string]string
- func StringSlice(path string) []string
- func Time(path string, layout string) time.Time
- func TryLoadEnvFromFile(scan, mod string)
- type AppConfiguration
- type Configurable
- type Configuration
- func (c *Configuration) Abs(path string) string
- func (c *Configuration) AllSettings() map[string]any
- func (c *Configuration) AppName() string
- func (c *Configuration) AsGlobal() *Configuration
- func (c *Configuration) Bool(path string) bool
- func (c *Configuration) Copy() *Configuration
- func (c *Configuration) CutFromOperator(kf *koanf.Koanf) *Configuration
- func (c *Configuration) Duration(path string) time.Duration
- func (c *Configuration) Each(path string, cb func(root string, sub *Configuration))
- func (c *Configuration) Exists() bool
- func (c *Configuration) Float64(path string) float64
- func (c *Configuration) Get(key string) any
- func (c *Configuration) GetBaseDir() string
- func (c *Configuration) Int(path string) int
- func (c *Configuration) IntSlice(path string) []int
- func (c *Configuration) IsSet(path string) bool
- func (c *Configuration) Load() *Configuration
- func (c *Configuration) Map(path string, cb func(root string, sub *Configuration))
- func (c *Configuration) Merge(b []byte) error
- func (c *Configuration) Namespace() string
- func (c *Configuration) Parser() *Parser
- func (c *Configuration) ParserOperator() *koanf.Koanf
- func (c *Configuration) Root() *Configuration
- func (c *Configuration) SetBaseDir(dir string)
- func (c *Configuration) String(path string) string
- func (c *Configuration) StringMap(path string) map[string]string
- func (c *Configuration) StringSlice(path string) []string
- func (c *Configuration) Sub(path string) *Configuration
- func (c *Configuration) Time(path string, layout string) time.Time
- func (c *Configuration) Unmarshal(dst any) (err error)
- func (c *Configuration) Version() string
- type Option
- type Parser
- func (l *Parser) AllKeys() []string
- func (l *Parser) Get(key string) any
- func (l *Parser) IsSet(key string) bool
- func (l *Parser) LoadFileWithEnv(path string) error
- func (l *Parser) MergeStringMap(cfg map[string]any) error
- func (l *Parser) Operator() *koanf.Koanf
- func (l *Parser) Set(key string, value any)
- func (l *Parser) Sub(key string) (*Parser, error)
- func (l *Parser) ToBytes(p koanf.Parser) ([]byte, error)
- func (l *Parser) ToStringMap() map[string]any
- func (l *Parser) Unmarshal(key string, dst any) (err error)
- func (l *Parser) UnmarshalExact(key string, intoCfg any) (err error)
- type TLS
Constants ¶
const (
// KeyDelimiter is used as the default key delimiter in the default koanf instance.
KeyDelimiter = "."
)
Variables ¶
This section is empty.
Functions ¶
func StringSlice ¶
func Time ¶
Time return time by layout, eg: 2006-01-02 15:04:05
if config is init from a map value of time.Time, the layout will be: `2006-01-02 15:04:05 -0700 MST`
func TryLoadEnvFromFile ¶ added in v0.4.0
func TryLoadEnvFromFile(scan, mod string)
TryLoadEnvFromFile try load env from files
Types ¶
type AppConfiguration ¶ added in v0.0.3
type AppConfiguration struct {
*Configuration
}
AppConfiguration is the application level configuration,include all of component's configurations
type Configurable ¶
type Configurable interface { // Apply set up property or field value by configuration // // cnf is the Configuration of the component, and it's sub configuration of root // path is the relative path to root,if root is the component,path will be empty // Apply also use for lazy load scene // notice: if any error in apply process,you should choose use panic to expose error,and stop application. Apply(cnf *Configuration) error }
Configurable can initial by framework
type Configuration ¶
type Configuration struct { Development bool // contains filtered or unexported fields }
Configuration hold settings of the component.
func New ¶
func New(opts ...Option) *Configuration
New create an application configuration instance.
New as global by default, if you want to create a local configuration, set global to false. Initialization such as:
cnf := conf.New().Load()
func NewFromBytes ¶
func NewFromBytes(b []byte, opts ...Option) *Configuration
NewFromBytes create from byte slice,return with a parser, But you'd better use Load()
func NewFromParse ¶ added in v0.0.3
func NewFromParse(parser *Parser, opts ...Option) *Configuration
NewFromParse create from parser
func NewFromStringMap ¶ added in v0.1.0
func NewFromStringMap(data map[string]any, opts ...Option) *Configuration
NewFromStringMap create from a string map
func (*Configuration) Abs ¶
func (c *Configuration) Abs(path string) string
Abs returns the absolute path by the base dir if path is a relative path,if path is empty return empty.
func (*Configuration) AllSettings ¶
func (c *Configuration) AllSettings() map[string]any
func (*Configuration) AppName ¶
func (c *Configuration) AppName() string
AppName indicates the application's name
returns the path 'appName' value if exists
func (*Configuration) AsGlobal ¶
func (c *Configuration) AsGlobal() *Configuration
AsGlobal set the Configuration as global
func (*Configuration) Bool ¶
func (c *Configuration) Bool(path string) bool
func (*Configuration) Copy ¶
func (c *Configuration) Copy() *Configuration
func (*Configuration) CutFromOperator ¶
func (c *Configuration) CutFromOperator(kf *koanf.Koanf) *Configuration
CutFromOperator return a new copied Configuration but replace the parser by koanf operator.
func (*Configuration) Each ¶ added in v0.1.0
func (c *Configuration) Each(path string, cb func(root string, sub *Configuration))
Each iterates the slice path of the Configuration.Slice item can be a map or a slice. if the item is a map, the root value will be the first key in the map. if the item is a slice, the root value will be empty. slice example:
path: - slice: "sv" - slice: "sv2" - slice: "sv3"
map example:
path: - key: sub: "sv"
because koanf should be map value, so not support like:
path: - string - string2
func (*Configuration) Exists ¶ added in v0.4.0
func (c *Configuration) Exists() bool
Exists check if the default configuration file exists
func (*Configuration) Float64 ¶
func (c *Configuration) Float64(path string) float64
func (*Configuration) Get ¶
func (c *Configuration) Get(key string) any
func (*Configuration) GetBaseDir ¶
func (c *Configuration) GetBaseDir() string
GetBaseDir return the application dir
func (*Configuration) Int ¶
func (c *Configuration) Int(path string) int
func (*Configuration) IntSlice ¶
func (c *Configuration) IntSlice(path string) []int
func (*Configuration) IsSet ¶
func (c *Configuration) IsSet(path string) bool
IsSet check if the key is set
func (*Configuration) Load ¶
func (c *Configuration) Load() *Configuration
Load parse configuration from file
func (*Configuration) Map ¶ added in v0.4.0
func (c *Configuration) Map(path string, cb func(root string, sub *Configuration))
Map iterates the map path of the Configuration.It is sort by sort.Strings, no guarantee of the order in the configuration file. Each key in the path must be a map value, otherwise it will panic.
func (*Configuration) Merge ¶
func (c *Configuration) Merge(b []byte) error
Merge an input config stream, parameter b is YAML stream
types operation:
- map[string]any: merge
- []any: override
func (*Configuration) Namespace ¶ added in v0.2.0
func (c *Configuration) Namespace() string
Namespace indicates the application's namespace
func (*Configuration) Parser ¶
func (c *Configuration) Parser() *Parser
Parser return configuration operator
func (*Configuration) ParserOperator ¶
func (c *Configuration) ParserOperator() *koanf.Koanf
ParserOperator return the underlying parser that converts bytes to map
func (*Configuration) Root ¶
func (c *Configuration) Root() *Configuration
Root return root configuration if it came from Sub method
func (*Configuration) SetBaseDir ¶
func (c *Configuration) SetBaseDir(dir string)
SetBaseDir return the application dir
func (*Configuration) String ¶
func (c *Configuration) String(path string) string
func (*Configuration) StringSlice ¶
func (c *Configuration) StringSlice(path string) []string
func (*Configuration) Sub ¶
func (c *Configuration) Sub(path string) *Configuration
Sub return a new Configuration by a sub node.return current node if path empty,panic if path not found.
sub node keeps the same root configuration of the current node.
func (*Configuration) Unmarshal ¶ added in v0.0.2
func (c *Configuration) Unmarshal(dst any) (err error)
Unmarshal map data of config into a struct, values are merged.
Tags on the fields of the structure must be properly set. Notice: if use nested struct, should tag `,inline` and can't use pointer: like
type Config struct { BaseConfig `yaml:",inline"` // should not *BaseConfig }
func (*Configuration) Version ¶
func (c *Configuration) Version() string
Version indicates the application's sem version
returns the path 'version' value, no set return empty
type Option ¶
type Option func(*options)
Option the function to apply configuration option
func WithBaseDir ¶ added in v0.0.3
WithBaseDir init base directory where configuration files location, usually is the directory which application executable file is in parameter s can be an absolute path or relative path.
func WithGlobal ¶ added in v0.0.3
WithGlobal indicate weather use as global configuration
func WithIncludeFiles ¶ added in v0.0.3
WithIncludeFiles init include files
The configuration in the attached file will overwrite the master configuration file and will be ignored for invalid files. you can set a configuration for dev ENV,but attach instance only effect in local file configuration
func WithLocalPath ¶ added in v0.0.3
WithLocalPath init local instance file path A s is file path
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser loads configuration.
func NewParserFromBuffer ¶
NewParserFromBuffer creates a new Parser by reading the given yaml buffer.
func NewParserFromFile ¶
NewParserFromFile creates a new Parser by reading the given file.
func NewParserFromOperator ¶ added in v0.4.0
func NewParserFromOperator(k *koanf.Koanf) *Parser
NewParserFromOperator creates a parser from a koanf.Koanf.
func NewParserFromStringMap ¶
NewParserFromStringMap creates a parser from a map[string]any.
func (*Parser) AllKeys ¶
AllKeys returns all keys holding a value, regardless of where they are set. Nested keys are returned with a KeyDelimiter separator.
func (*Parser) IsSet ¶
IsSet checks to see if the key has been set in any of the data locations. IsSet is case-insensitive for a key.
func (*Parser) LoadFileWithEnv ¶ added in v0.4.0
LoadFileWithEnv loads the given file and env, and merges it into the config.
func (*Parser) MergeStringMap ¶
MergeStringMap merges the configuration from the given map with the existing config. Note that the given map may be modified.
func (*Parser) Operator ¶ added in v0.2.6
func (l *Parser) Operator() *koanf.Koanf
Operator returns the koanf instance.
func (*Parser) Sub ¶
Sub returns new Parser instance representing a sub-config of this instance. It returns an error is the sub-config is not a map (use Get()) or if none exists.
func (*Parser) ToBytes ¶
ToBytes takes a Parser implementation and marshals the config map into bytes, for example, to TOML or JSON bytes.
func (*Parser) ToStringMap ¶
ToStringMap creates a map[string]any from a Parser.
type TLS ¶ added in v0.2.6
type TLS struct { CA string `json:"ca" yaml:"ca"` Cert string `json:"cert" yaml:"cert"` Key string `json:"key" yaml:"key"` InsecureSkipVerify bool `json:"insecureSkipVerify" yaml:"insecureSkipVerify"` }
TLS is the TLS configuration for TLS connections TLS content can be file or cert string,depend on your application access notice: TLS is experimental,and only support file path in configuration
func NewTLS ¶ added in v0.2.6
func NewTLS(cnf *Configuration) *TLS
NewTLS creates a new TLS configuration. It will initialize the same defaults as the tls.Config struct.
func (*TLS) Apply ¶ added in v0.2.6
func (t *TLS) Apply(cnf *Configuration)