config

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config describes the structure of a Variable and lists variables for the current Environment.

During the init-phase of the application, the Environment variable is populated with the result of os.Environ.

Exporting variables Using an Exporter, Variable structs can be written a provided io.Writer, for a given format.

Encoding Variables can be converted to a textual representation and back, using Decoder and Encoder instances.

An Encoding is the combination of an Encoder and Decoder, which can be registered against a given format.

Additional encoders can be added through the RegisterEncoding function. This allows for separation of concerns internally, but also for plugins to implement their own encoding.

The function NewEncoding allows to get the encoder that is registered against the provided format. This function will return an error when no encoding is known for the given format, even if the encoding is registered later on.

To process Variables for a given format, without being concerned about whether the format exists, the function WithEncoding can be used. Since an Encoding may be registered at runtime, this strategy allows to queue processing of Variables before the Encoding is available. The moment the Encoding is registered, all corresponding EncodingCallback functions are invoked. When the Encoding is already present, this happens the moment WithEncoding is invoked.

When explicitly checking against an available Encoding, the functions HasEncoding and GetEncodings will be of use.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownEncoding is returned when an unknown encoding is requested using NewEncoding.
	ErrUnknownEncoding = errors.New("unknown encoding requested")

	// ErrIllFormattedVariable is returned when a payload contains a line that cannot be resolved to a Variable.
	ErrIllFormattedVariable = errors.New("variable is incorrectly formatted")

	// DefaultEncoding is the default encoding format.
	DefaultEncoding = "text"
)
View Source
var Environment = make(Variables, 0)

Environment holds the environment variables expressed in Variables.

Functions

func GetEncodings

func GetEncodings() []string

GetEncodings gets a list of available Encoding formats.

func HasEncoding added in v0.6.0

func HasEncoding(format string) bool

HasEncoding determines whether an encoding for the given format has been registered.

func RegisterEncoding

func RegisterEncoding(name string, encoding Encoding)

RegisterEncoding registers the given Encoding in a registry for NewEncoding instances. Additionally, it triggers available EncodingCallback entries for the given name.

func WithEncoding

func WithEncoding(name string, callback EncodingCallback)

WithEncoding registers an EncodingCallback to execute when the Encoding with the given name is/will be registered.

Types

type Decoder

type Decoder interface {
	Decode(payload []byte) (Variables, error)
}

Decoder allows to Decode a byte sequence into a list of config.Variable structs.

type Encoder

type Encoder interface {
	Encode(variables ...*Variable) ([]byte, error)
}

Encoder allows to Encode config.Variable structs into a byte sequence.

type Encoding

type Encoding interface {
	Encoder
	Decoder
}

Encoding is a shared interface for Encoder and Decoder.

func NewEncoding

func NewEncoding(format string) (Encoding, error)

NewEncoding creates a new Encoding for the given format.

type EncodingCallback

type EncodingCallback func(enc Encoding)

EncodingCallback is a callback for when an Encoding has become available.

type Exporter

type Exporter interface {
	Export(variable ...*Variable)
}

Exporter describes structs that are able to export Variable entries.

func NewExporter

func NewExporter(format string, writer io.Writer) Exporter

NewExporter creates a new Exporter for the given format, using the given io.Writer as target.

Example
exp := NewExporter("text", os.Stdout)
exp.Export(&Variable{
	Key:   "FOO",
	Value: "Foo",
})
Output:

FOO=Foo
Example (Json)
exp := NewExporter("json", os.Stdout)
exp.Export(&Variable{
	Key:   "FOO",
	Value: "Foo",
})
Output:

{
	"FOO": "Foo"
}

type Variable

type Variable struct {
	Key   string
	Value string
}

Variable is a struct representing a configuration entry by Key and Value.

type Variables

type Variables []*Variable

Variables are a list of configuration Variable entries.

Jump to

Keyboard shortcuts

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