gonk

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: AGPL-3.0 Imports: 8 Imported by: 8

README

gonk

Go Reference

Simple configuration tool for golang. Supports loading from yaml files and environment variables.

Documentation

Overview

Package gonk is a configuration loader built using reflection. Given a public struct with public fields tagged `config`, gonk will load values from the given sources in the order specified. a struct pointer tagged with `config:"name"` fields or a slice, LoadConfig can be used to load values from the [Loader]s into the struct.

type Example struct {
	Field1 string `config:"1"`
    Field2 string `config:"anotherVar"`
}

func main() {
    config :=new(Example)
    err := LoadConfig(config, EnvLoader("prefix"))
}

In this example, gonk will look for environment variables named `PREFIX_1` and `PREFIX_ANOTHERVAR`. If one or more values are not available in at least one source, gonk will return an error, unless the field is marked with the optional tag.

gonk supports loading nested structs as well, provided they are also publicly exported

Field tags may be more complex. The syntax is:

pattern: { [ path components ]... tag [ ,options... ] }

path components: allows for defining nested keys by specifying the path components separated with a '.'.

tag: name of the key to lookup by the loader. Loaders may concatenate previous path components into the name they
look up (ie environment loader will prefix all path components, upper cased and separated with an '_').

options: optional	specifies if the key can be omitted by a specific loader.

For example:

type Example struct {
	FirstField   int    `config:"app.my-int"`
	SecondField  string `config:"my-string"`
	NestedStruct struct {
		AnotherField int `config:"my-int"` // Will load `app.my-int`
	} `config:"app"`
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(dest any, loaders ...Loader) error

LoadConfig loads configuration into a struct pointer or slice. Pass one or more loaders as arguments to provide sources to load from. Loaders will be applied in the order given here. Produces a joined error containing all errors encountered whilst loading, or nil if the loading was succesfull. Missing fields marked as optional will not be reported as errors.

Types

type EnvLoader

type EnvLoader string

EnvLoader will load configuration from environment variables. Supports only string and int types. Does not support loading slice elements. Nested data will concatenate all previous path elements into one env name, separated by '_'. If the undeerlying string is not "", this will be used as a prefix. The entire name is uppercased. If a name contains a hyphen, such as with a default field (ie FieldA will become field-a which then becomes FIELD_A). string

func (EnvLoader) Load added in v0.1.1

func (prefix EnvLoader) Load(node reflect.Value, tag tagData) (reflect.Value, error)

Load is called internally by gonk to load a value at each node.

type InvalidValueError added in v0.2.0

type InvalidValueError string // InvalidValueError occurs when a value loaded in a loader is not compatible with the type of value requested.

func (InvalidValueError) Error added in v0.2.0

func (msg InvalidValueError) Error() string

type Loader

type Loader interface {
	Load(node reflect.Value, tag tagData) (reflect.Value, error)
}

func NewYamlLoader added in v0.1.1

func NewYamlLoader(file string) (Loader, error)

NewYamlLoader returns a loader for the yaml file specified in the path. If the file does not exist or cannot be unmarshalled, this returns those errors transparently.

type MapLoader

type MapLoader map[string]any

MapLoader wraps a map[string]any to allow loading data from it into a struct. For a given key or key path it will traverse the map to the matching node, unwrap the type and set it

func (MapLoader) Load added in v0.1.1

func (m MapLoader) Load(node reflect.Value, tag tagData) (reflect.Value, error)

Load is called when getting values from a node. It assigns a value to the passed reflect.Value, based on the tag data given.

type ValueNotPresentError added in v0.2.0

type ValueNotPresentError string // ValueNotPresentError occurs when a value is requested from a loader that does not exist in that source

func (ValueNotPresentError) Error added in v0.2.0

func (msg ValueNotPresentError) Error() string

type ValueNotSupportedError added in v0.2.0

type ValueNotSupportedError string // ValueNotSupportedError occurs when a value of a non-supported type is requested from a loader

func (ValueNotSupportedError) Error added in v0.2.0

func (msg ValueNotSupportedError) Error() string

Jump to

Keyboard shortcuts

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