Documentation
¶
Overview ¶
Package config provides very easy to use and extensible configuration management capabilities.
Index ¶
- type EmptyKeyChainError
- type Env
- type FileReader
- type InvalidJsonError
- type InvalidYamlError
- type Json
- type Manager
- type Map
- type RenderTextTemplateOption
- type Source
- type Store
- type TextTemplateExecError
- type TextTemplateParseError
- type TextTemplateRenderer
- type TypeCoercionError
- type UnexpectedKeyValueTypeError
- type UnknownKeyerError
- type Yaml
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EmptyKeyChainError ¶
type EmptyKeyChainError struct {
Value any
}
EmptyKeyChainError
func (EmptyKeyChainError) Error ¶
func (e EmptyKeyChainError) Error() string
Error implements the error interface.
type Env ¶
type Env struct {
// contains filtered or unexported fields
}
Env represents a Source where its underlying values are extracted from environment variables.
type FileReader ¶
type FileReader struct {
// contains filtered or unexported fields
}
FileReader is an io.Reader that handles opening a file for reading automatically.
func NewFileReader ¶
func NewFileReader(fs fs.FS, path string) *FileReader
NewFileReader configures a FileReader.
func (*FileReader) Close ¶
func (r *FileReader) Close() error
Close implements the io.Closer interface.
type InvalidJsonError ¶
type InvalidJsonError struct {
// contains filtered or unexported fields
}
InvalidJsonError occurs if the underlying io.Reader contains invalid JSON.
func (InvalidJsonError) Error ¶
func (e InvalidJsonError) Error() string
Error implements the error interface.
func (InvalidJsonError) Unwrap ¶
func (e InvalidJsonError) Unwrap() error
Unwrap implmeents the implicit interface used by errors.Is and errors.As.
type InvalidYamlError ¶
type InvalidYamlError struct {
// contains filtered or unexported fields
}
InvalidYamlError occurs if the underlying io.Reader contains invalid YAML.
func (InvalidYamlError) Error ¶
func (e InvalidYamlError) Error() string
Error implements the error interface.
func (InvalidYamlError) Unwrap ¶
func (e InvalidYamlError) Unwrap() error
Unwrap implmeents the implicit interface used by errors.Is and errors.As.
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
Json represents a Source where its underlying format is JSON.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager
func Read ¶
Read Subsequent sources override previous sources.
Example ¶
src := Map{ "hello": "world", } m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello)
Output: world
Example (Env) ¶
os.Setenv("HELLO", "world") defer os.Unsetenv("HELLO") src := FromEnv() m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"HELLO"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello)
Output: world
Example (FileReader) ¶
r := NewFileReader(os.DirFS("."), "testdata/config.yaml") src := FromYaml(r) m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello)
Output: world
Example (Json) ¶
r := strings.NewReader(`{ "hello": "world", "xs":[ 1, 2, 3 ], "a":{ "b": 1.2 } }`) src := FromJson(r) m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` Xs []int `config:"xs"` A struct { B float64 `config:"b"` } `config:"a"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello) fmt.Println(cfg.Xs) fmt.Println(cfg.A.B)
Output: world [1 2 3] 1.2
Example (Map) ¶
src := Map{ "hello": "world", "xs": []int{1, 2, 3}, "a": map[string]any{ "b": 1.2, }, } m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` Xs []int `config:"xs"` A struct { B float64 `config:"b"` } `config:"a"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello) fmt.Println(cfg.Xs) fmt.Println(cfg.A.B)
Output: world [1 2 3] 1.2
Example (TextTemplateRenderer) ¶
r := strings.NewReader(`hello: {{ myName }}`) ttr := RenderTextTemplate( r, TemplateFunc("myName", func() string { return "bob" }), ) src := FromYaml(ttr) m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello)
Output: bob
Example (TextTemplateRenderer_CustomDelims) ¶
r := strings.NewReader(`hello: (( myName ))`) ttr := RenderTextTemplate( r, TemplateDelims("((", "))"), TemplateFunc("myName", func() string { return "bob" }), ) src := FromYaml(ttr) m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello)
Output: bob
Example (Yaml) ¶
r := strings.NewReader(` hello: world xs: - 1 - 2 - 3 a: b: 1.2 `) src := FromYaml(r) m, err := Read(src) if err != nil { fmt.Println(err) return } var cfg struct { Hello string `config:"hello"` Xs []int `config:"xs"` A struct { B float64 `config:"b"` } `config:"a"` } err = m.Unmarshal(&cfg) if err != nil { fmt.Println(err) return } fmt.Println(cfg.Hello) fmt.Println(cfg.Xs) fmt.Println(cfg.A.B)
Output: world [1 2 3] 1.2
type Map ¶
Map is an ordinary map[string]any but implements the Store and Source interfaces.
type RenderTextTemplateOption ¶
type RenderTextTemplateOption func(*TextTemplateRenderer)
RenderTextTemplateOption represents options for configuring the TextTemplateRenderer.
func TemplateDelims ¶
func TemplateDelims(left, right string) RenderTextTemplateOption
TemplateDelims sets the action delimiters to the specified strings. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}.
func TemplateFunc ¶
func TemplateFunc(name string, f any) RenderTextTemplateOption
TemplateFunc registers the given function, f, for use in the config template via the given name.
type Source ¶
Source defines valid config sources as those who can serialize themselves into a key value like structure.
func MultiSource ¶ added in v0.17.0
MultiSource returns a Source that's the logical concatenation of the provided [Source]s. They're applied sequentially. If any of the [Source]s return a non-nil error, Apply will return that error.
type TextTemplateExecError ¶
type TextTemplateExecError struct {
Cause error
}
TextTemplateExecError occurs when a template fails to execute. Most likely cause is using template functions returning an error or panicing.
func (TextTemplateExecError) Error ¶
func (e TextTemplateExecError) Error() string
Error implements the error interface.
func (TextTemplateExecError) Unwrap ¶
func (e TextTemplateExecError) Unwrap() error
Unwrap implements the implicit interface used by errors.Is and errors.As.
type TextTemplateParseError ¶
type TextTemplateParseError struct {
Cause error
}
TextTemplateParseError occurs when the config template fails to be parsed.
func (TextTemplateParseError) Error ¶
func (e TextTemplateParseError) Error() string
Error implements the error interface.
func (TextTemplateParseError) Unwrap ¶
func (e TextTemplateParseError) Unwrap() error
Unwrap implements the implicit interface used by errors.Is and errors.As.
type TextTemplateRenderer ¶
type TextTemplateRenderer struct {
// contains filtered or unexported fields
}
TextTemplateRenderer is an io.Reader that renders a text/template from a given io.Reader. The rendered template can then be read via TextTemplateRenderer.Read.
func RenderTextTemplate ¶
func RenderTextTemplate(r io.Reader, opts ...RenderTextTemplateOption) *TextTemplateRenderer
RenderTextTemplate configures a TextTemplateRenderer.
type TypeCoercionError ¶
type TypeCoercionError struct { Cause error // contains filtered or unexported fields }
TypeCoercionError occurs when attempting to unmarshal a config value to a struct field whose type does not match the config value type, up to, coercion.
func (TypeCoercionError) Error ¶
func (e TypeCoercionError) Error() string
Error implements the error interface.
func (TypeCoercionError) Unwrap ¶
func (e TypeCoercionError) Unwrap() error
Unwrap implements the implicit interface for usage with errors.Is and errors.As.
type UnexpectedKeyValueTypeError ¶
UnexpectedKeyValueTypeError represents the situation when a user tries setting a key to a different type than it had previously been set to.
func (UnexpectedKeyValueTypeError) Error ¶
func (e UnexpectedKeyValueTypeError) Error() string
Error implements the error interface.
type UnknownKeyerError ¶
type UnknownKeyerError struct {
// contains filtered or unexported fields
}
UnknownKeyerError
func (UnknownKeyerError) Error ¶
func (e UnknownKeyerError) Error() string
Error implements the error interface.