config

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package config provides very easy to use and extensible configuration management capabilities.

Index

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.

func FromEnv

func FromEnv() Env

FromEnv returns a Source which will apply its config from the environment variables available to the current process.

func (Env) Apply

func (src Env) Apply(store Store) error

Apply implements the Source interface.

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.

func (*FileReader) Read

func (r *FileReader) Read(b []byte) (int, error)

Read implements the Read 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.

func FromJson

func FromJson(r io.Reader) Json

FromJson returns a source which will apply its config from JSON values parsed from the given io.Reader.

func (Json) Apply

func (src Json) Apply(store Store) (err error)

Apply implements the Source interface.

type Manager

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

Manager

func Read

func Read(srcs ...Source) (*Manager, error)

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

func (*Manager) Unmarshal

func (m *Manager) Unmarshal(v any) error

Unmarshal

type Map

type Map map[string]any

Map is an ordinary map[string]any but implements the Store and Source interfaces.

func (Map) Apply

func (m Map) Apply(store Store) error

Apply implements the Source interface. It recursively walks the underlying map to find key value pairs to set on the given store.

func (Map) Set

func (m Map) Set(k key.Keyer, v any) error

Set implements the Store interface.

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

type Source interface {
	Apply(Store) error
}

Source defines valid config sources as those who can serialize themselves into a key value like structure.

func MultiSource added in v0.17.0

func MultiSource(srcs ...Source) Source

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 Store

type Store interface {
	Set(key.Keyer, any) error
}

Store represents a general key value structure.

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.

func (*TextTemplateRenderer) Read

func (ttr *TextTemplateRenderer) Read(b []byte) (int, error)

Read implements the read interface.

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

type UnexpectedKeyValueTypeError struct {
	Key          string
	ExpectedType string
}

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

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.

type Yaml

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

Yaml represents a Source where its underlying format is YAML.

func FromYaml

func FromYaml(r io.Reader) Yaml

FromYaml returns a source which will apply its config from YAML values parsed from the given io.Reader.

func (Yaml) Apply

func (src Yaml) Apply(store Store) (err error)

Apply implements the Source interface.

Directories

Path Synopsis
Package key provides types for strongly typed keys in key value pairs.
Package key provides types for strongly typed keys in key value pairs.

Jump to

Keyboard shortcuts

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