Documentation ¶
Overview ¶
Package goenv supports unmarshalling objects from environment variables by defining tags appended to fields. The idea here is that there are a lot of applications whose config objects are serialised from environment variable values.
Consider the following example
type CassandraConfig struct { Hosts []string `env: "CASSANDRA_HOSTS"` Port int `env: "CASSANDRA_PORT"` Consistency string `env: "CASSANDRA_CONSISTENCY"` } func main() { // setting up the config unmarshaller := DefaultEnvMarshaler { Environment: NewOsEnvReader(), } config := CassandraConfig{} unmarshaller.Unmarshal(&config) // application logic // ... }
We believe that the above is pretty straightforward and has a similar flavor to the `encoding/json` library.
At this juncture, the unmarshalling is not thread-safe. Explicit synchronisation logic is needed to achieve atomicity in code.
Package goenv defines an object that parses values from strings.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultEnvMarshaler ¶
type DefaultEnvMarshaler struct {
Environment EnvReader
}
DefaultEnvMarshaler - An unmarshaller that uses the DefaultParser and a specific environment reader to unmarshal primitive and derived values.
func (*DefaultEnvMarshaler) Unmarshal ¶
func (marshaler *DefaultEnvMarshaler) Unmarshal(i interface{}) error
Unmarshal - Unmarshals a given value from environment variables. It accepts a pointer to a given object, and either succeeds in unmarshalling the object or returns an error.
Usage:
import "github.com/evilwire/go-env" type CassandraConfig struct { Hosts []string `env: "CASSANDRA_HOSTS"` Port int `env: "CASSANDRA_PORT"` Consistency string `env: "CASSANDRA_CONSISTENCY"` } func main() { // setting up the config unmarshaller := goenv.DefaultEnvMarshaler { Environment: goenv.NewOsEnvReader(), } config := CassandraConfig{} unmarshaller.Unmarshal(&config) // application logic // ... }
type DefaultParser ¶
type DefaultParser struct{}
DefaultParser - A default way to parse a string into a specific primitive or pointer.
func (*DefaultParser) ParseType ¶
ParseType - Parses a string value for a specific type given by reflect.Type. For example, ParseType might accept str="2" and reflect.Type=reflect.Uint and parses the uint value of 2 returned as reflect.Value.
In this particular case, we parse all numeric types, pointers, strings, booleans, arrays and slices. The method handles Durations differently, though under the hood, the type is treated the same way as int64. In particular, we parse durations of the form `1m3s` and more generally, expects the string to be parse-able via ParseDuration.
If the object isn't one of the supported types, it throws an error.
func (*DefaultParser) Unmarshal ¶
func (marshaler *DefaultParser) Unmarshal(val string, i interface{}) error
Unmarshal - Unmarshals a string into any one of the string-parseable types, which include (pointers of) numeric types, strings, booleans, arrays and slices. The method also handles Duration separately.
The method throws an error if the underlying interface is unsettable (see https://golang.org/pkg/reflect/#Value.CanSet), or if parsing for the value resulted in error
type EnvReader ¶
type EnvReader interface { // look up the value for a particular env variable // returning false if the variable is not registered LookupEnv(string) (string, bool) // returns whether or not env variables are set in // the environment; returning a collection of env // variables that are missing HasKeys([]string) (bool, []string) }
EnvReader is an interface for expressing the ability to look up values from the environment via environment variables (LookupEnv) and the ability to query the existence of many environment variables at once.
type EnvUnmarshaler ¶
EnvUnmarshaler is an interface for any object that defines the UnmarshalEnv method, i.e. a method that accepts an EnvReader and can unmarshal from environment variable values from the EnvReader
type Marshaler ¶
type Marshaler interface {
Unmarshal(interface{}) error
}
Marshaler - An interface for any object that implements the Unmarshal method.
type OsEnvReader ¶
type OsEnvReader struct {
// contains filtered or unexported fields
}
OsEnvReader is an environment variable reader that implements that EnvReader interface by using the os.LookupEnv method.
func NewOsEnvReader ¶
func NewOsEnvReader() *OsEnvReader
NewOsEnvReader creates a new instance of OsEnvReader
func (*OsEnvReader) HasKeys ¶
func (env *OsEnvReader) HasKeys(keys []string) (bool, []string)
HasKeys - Returns whether or not a set of environment variables have corresponding values along with a list of environment variables that do not have values.
func (*OsEnvReader) LookupEnv ¶
func (env *OsEnvReader) LookupEnv(key string) (string, bool)
LookupEnv - Lookup a certain environment variable by name. Returns the value of the environment variable if the variable exists and has an assigned value. Otherwise, returns an unspecific value, and the exists flag is set to false.