structconf

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: MIT Imports: 8 Imported by: 0

README

go-structconf

license GoDoc Test Status Lint Status codecov Go Report Card

go-structconf is a Go library which provides a simplified interface for storing configuration data in structs.

It provides support for reading and writing configuration files from and to storages as well as simplified usage of different data encodings.

Furthermore, it allows default values on config structs to be initialized from structs of the same type.

Installing

To use go-structconf just add github.com/anexia-it/go-structconf <version> to your go.mod file.

Documentation

Overview

Package structconf provides a simplified interface for storing configuration data in structs.

It provides support for reading and writing configuration files from and to storages as well as simplified usage of different data encodings.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConfigStructIsNil indicates that the passed config struct is nil
	ErrConfigStructIsNil = errors.New("Config struct is nil")

	// ErrNotAStructPointer indicates that the passed value is not a pointer to a struct
	ErrNotAStructPointer = errors.New("Passed value is not a struct pointer")

	// ErrConfigStructTypeMismatch indicates that the passed config struct and
	// default values are not of the same type
	ErrConfigStructTypeMismatch = errors.New("Type mismatch between defaults and config struct")

	// ErrEncodingNotConfigured indicates that no encoding was configured
	ErrEncodingNotConfigured = errors.New("Encoding not configured")

	// ErrStorageNotConfigured indicates that no storage was configured
	ErrStorageNotConfigured = errors.New("Storage not configured")
)

Functions

func MergeMaps

func MergeMaps(a, b map[string]interface{}) (map[string]interface{}, error)

MergeMaps merges the passed maps The resulting map contains all keys that existed in either of the passed maps. For keys that exist in both maps, the value from the "b" map takes precedence, iff it is not a zero-value. Keys that are present in both maps are expected to be of the same type. If this is not the case, an error will be returned.

func MergeValues added in v1.0.4

func MergeValues(a, b interface{}) (interface{}, error)

MergeValues "merges" two values

The internal logic is as follows:

- If b is zero, return a - If a is zero, return b - If both values are non-zero and scalar types, convert b with a's type, if possible. - If both values are non-zero and slices or arrays, return b - If both values are non-zero and maps, merge each element of the map using the above logic

Types

type Configuration

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

Configuration represents a configuration backed by a struct

func NewConfiguration

func NewConfiguration(config interface{}, options ...Option) (*Configuration, error)

NewConfiguration initializes a new configuration with the given options

func (*Configuration) Load

func (c *Configuration) Load() error

Load loads the configuration from the underlying storage

func (*Configuration) Save

func (c *Configuration) Save() error

Save writes the configuration to the underlying storage

func (*Configuration) SetDefaults

func (c *Configuration) SetDefaults(defaults interface{}) error

SetDefaults sets the defaults value for the configuration

type Option

type Option func(*Configuration) error

Option defines the function type of Configuration options

func OptionDefaults

func OptionDefaults(defaults interface{}) Option

OptionDefaults configures the default values from a struct This requires an encoding to be configured before-hand and will return an error if no encoding was configured

func OptionEncoding

func OptionEncoding(encoding encoding.Encoding) Option

OptionEncoding configures a configuration encoding

func OptionStorage

func OptionStorage(storage storage.Storage) Option

OptionStorage configures a configuration storage

func OptionTagName

func OptionTagName(tagName string) Option

OptionTagName configures the tag names used when encoding the config struct

Directories

Path Synopsis
Package encoding provides common functionality for go-structconf encodings
Package encoding provides common functionality for go-structconf encodings
json
Package json provides the JSON encoding for go-structconf
Package json provides the JSON encoding for go-structconf
toml
Package toml provides the TOML encoding for go-structconf
Package toml provides the TOML encoding for go-structconf
yaml
Package yaml provides the YAML encoding for go-structconf
Package yaml provides the YAML encoding for go-structconf
Package storage provides common functionality for go-structconf storages
Package storage provides common functionality for go-structconf storages
aferofile
Package aferofile provides file-based configuration storage for go-structconf accessed through an afero.Fs
Package aferofile provides file-based configuration storage for go-structconf accessed through an afero.Fs
file
Package file provides file-based configuration storage for go-structconf
Package file provides file-based configuration storage for go-structconf

Jump to

Keyboard shortcuts

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