cconfig

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Package cconfig provides utilities to read app config files. See New and Loader for more documentation and example.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader added in v0.3.4

type Loader interface {
	// Load reads the config values defined under the given key (TOML table) and sets them into the dest struct.
	// For example:
	// # prod.toml
	// key1 = "val1"
	//
	// # config.go
	// type MyConfig struct {
	//   Key1 string `toml:"key1"`
	// }
	//
	// func LoadMyConfig(loader cconfig.Loader) (MyConfig, error) {
	//   var config MyConfig
	//
	//   err := loader.Load("my_config", &config)
	//   if err != nil {
	//     return MyConfig{}, cerrors.New(err, "failed to load configs for my_config", nil)
	//   }
	//
	//   return config, nil
	// }
	Load(key string, dest interface{}) error
}

Loader provides methods to load config files into structs.

func New

func New(fp Path, ov Overrides) (Loader, error)

New provides an implementation of Loader that reads a config file at the given file path. It supports extending the config file at the given path by using an 'extends' key. For example, the config file may be extended like so:

# base.toml key1 = "val1"

# prod.toml extends = "base.toml key2 = "val2"

If New is called with the path to prod.toml, it loads both key2 (from prod.toml) and key1 (from base.toml) since prod.toml extends base.toml.

The extends key can support multiple files like so: extends = ["base.toml", "secrets.toml"]

If a config key is present in multiple files, New returns an error. For example, if prod.toml sets a value for 'key1' that has already been set in base.toml, an error will be returned. To enable key overrides see NewWithKeyOverrides.

func NewWithKeyOverrides added in v0.3.4

func NewWithKeyOverrides(fp Path, overrides Overrides) (Loader, error)

NewWithKeyOverrides works exactly the same way as New except it supports key overrides. For example, this is a valid config: # base.toml key1 = "val1"

# prod.toml extends = "base.toml key1 = "val2"

If prod.toml is loaded, key1 will be set to "val2" since it has been overridden in prod.toml.

type Overrides added in v0.7.0

type Overrides string

Overrides defines a ';' separated string of config overrides in TOML format

type Path added in v0.3.4

type Path string

Path defines the path to the config file.

Directories

Path Synopsis
Package cconfigtest provides helper methods to test the cconfig package
Package cconfigtest provides helper methods to test the cconfig package

Jump to

Keyboard shortcuts

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