config

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const Root = ""

Root is a virtual key that accesses the entire configuration. Using it as the key when calling Provider.Get or Value.Get returns the whole configuration.

Variables

This section is empty.

Functions

This section is empty.

Types

type LookupFunc

type LookupFunc = func(string) (string, bool)

A LookupFunc behaves like os.LookupEnv: it uses the supplied string as a key into some key-value store and returns the value and whether the key was present.

type NopProvider

type NopProvider struct{}

NopProvider is a no-op provider.

func (NopProvider) Get

func (n NopProvider) Get(_ string) Value

Get returns a value with no configuration available.

func (NopProvider) Name

func (NopProvider) Name() string

Name implements Provider.

type Provider

type Provider interface {
	Name() string         // name of the configuration store
	Get(key string) Value // retrieves a portion of the configuration, see Value for details
}

Provider is an abstraction over a configuration store, such as a collection of merged YAML, JSON, or TOML files.

func NewProviderGroup

func NewProviderGroup(name string, providers ...Provider) (Provider, error)

NewProviderGroup composes multiple providers, with later providers overriding earlier ones. The merge logic is described in the package-level documentation. To preserve backward compatibility, the resulting provider disables strict unmarshalling.

Prefer using NewYAML instead of this where possible. NewYAML gives you strict unmarshalling by default and allows use of other options at the same time.

func NewScopedProvider

func NewScopedProvider(prefix string, provider Provider) Provider

NewScopedProvider wraps a provider and adds a prefix to all Get calls.

type Value

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

A Value is a subset of a provider's configuration.

func (Value) Get

func (v Value) Get(path string) Value

Get dives further into the configuration, pulling out more deeply nested values. The supplied path is split on periods, and each segment is treated as a nested map key. For example, if the current value holds the YAML configuration

foo:
  bar:
    baz: quux

then a call to Get("foo.bar") will hold the YAML mapping

baz: quux

func (Value) Populate

func (v Value) Populate(target interface{}) error

Populate unmarshals the value into the target struct, much like json.Unmarshal or yaml.Unmarshal. When populating a struct with some fields already set, data is deep-merged as described in the package-level documentation.

func (Value) Source

func (v Value) Source() string

Source returns the name of the value's provider.

func (Value) String

func (v Value) String() string

type YAML

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

YAML is a provider that reads from one or more YAML sources. Many aspects of the resulting provider's behavior can be altered by passing functional options.

By default, the YAML provider attempts to proactively catch common mistakes by enabling gopkg.in/yaml.v2's strict mode. See the package-level documentation on strict unmarshalling for details.

When populating Go structs, values produced by the YAML provider correctly handle all struct tags supported by gopkg.in/yaml.v2. See https://godoc.org/gopkg.in/yaml.v2#Marshal for details.

func NewYAML

func NewYAML(options ...YAMLOption) (*YAML, error)

NewYAML constructs a YAML provider. See the various YAMLOptions for available tweaks to the default behavior.

func (*YAML) Get

func (y *YAML) Get(key string) Value

Get retrieves a value from the configuration. The supplied key is treated as a period-separated path, with each path segment used as a map key. For example, if the provider contains the YAML

foo:
  bar:
    baz: hello

then Get("foo.bar") returns a value holding

baz: hello

To get a value holding the entire configuration, use the Root constant as the key.

func (*YAML) Name

func (y *YAML) Name() string

Name returns the name of the provider. It defaults to "YAML".

type YAMLOption

type YAMLOption interface {
	// contains filtered or unexported methods
}

A YAMLOption alters the default configuration of the YAML configuration provider.

func Expand

func Expand(lookup LookupFunc) YAMLOption

Expand enables variable expansion in all non-raw provided sources. The supplied function MUST behave like os.LookupEnv: it looks up a key and returns a value and whether the key was found. Any expansion is deferred until after all sources are merged, so it's not possible to reference different variables in different sources and have the values automatically merged.

Expand allows variable references to take two forms: $VAR or ${VAR:default}. In the first form, variable names MUST adhere to shell naming rules:

...a word consisting solely of underscores, digits, and alphabetics form
the portable character set. The first character of a name may not be a
digit.

In this form, NewYAML returns an error if any referenced variables aren't found.

In the second form, all characters between the opening curly brace and the first colon are used as the key, and all characters from the colon to the closing curly brace are used as the default value. Keys need not adhere to the shell naming rules above. If a variable isn't found, the default value is used.

$$ is expanded to a literal $.

func File

func File(name string) YAMLOption

File opens a file, uses it as a source of YAML configuration, and closes it once provider construction is complete. Priority, merge, and expansion logic are identical to Source.

func Name

func Name(name string) YAMLOption

Name customizes the name of the provider. The default name is "YAML".

func Permissive

func Permissive() YAMLOption

Permissive disables gopkg.in/yaml.v2's strict mode. It's provided for backward compatibility; to avoid a variety of common mistakes, most users should leave YAML providers in the default strict mode.

In permissive mode, duplicate keys in the same source file are allowed. Later values override earlier ones (note that duplicates are NOT merged, unlike all other merges in this package). Calls to Populate that don't use all keys present in the YAML are allowed. Finally, type conflicts are allowed when merging source files, with later values replacing earlier ones.

func RawSource

func RawSource(r io.Reader) YAMLOption

RawSource adds a source of YAML configuration. Later sources override earlier ones using the merge logic described in the package-level documentation.

Raw sources are not subject to variable expansion. To provide a source with variable expansion enabled, use the Source option.

func Source

func Source(r io.Reader) YAMLOption

Source adds a source of YAML configuration. Later sources override earlier ones using the merge logic described in the package-level documentation.

Sources are subject to variable expansion (via the Expand option). To provide a source that remains unexpanded, use the RawSource option.

func Static

func Static(val interface{}) YAMLOption

Static serializes a Go data structure to YAML and uses the result as a source. If serialization fails, provider construction will return an error. Priority, merge, and expansion logic are identical to Source.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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