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 ¶
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" )
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
HasEncoding determines whether an encoding for the given format has been registered.
func RegisterEncoding ¶
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 Encoding ¶
Encoding is a shared interface for Encoder and Decoder.
func NewEncoding ¶
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 ¶
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" }