env

package
v0.0.0-...-4970d2c Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

env

Unmarshal values from environment variables

WARNING

This repository has been moved to github.com/lestrrat-go/config/env. This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path.

SYNOPSIS

// TODO: Write an example test code, and paste it here
type Config struct {
    Foo string
    Bar int
}

func LoadConfig(c *Config) error {
    // Loads from MYAPP_FOO, MYAPP_BAR
    return env.NewDecoder(env.System).Prefix("MYAPP").Decode(c)
}

DESCRIPTION

This library can be thought of as a fork of github.com/kelseyhightower/envconfig. The code was written from scratch, but the goals are the same: We would like to support fetching configuration information from environment variables.

The author initially attempted to use the library above, but there were a few things that needed changing to adapt to the author's needs. However, that would require modifying behavior of a code base that is being used by well established user base, and the author has been around long enough that it is quite hard to chanve existing behavior :)

So here is yet another library, instead of spending days arguing if my change is good for the World or not. If the library above decides to support the differences that I describe below, this library may be deprecated/changed/whatever.

Supported Types

  • string
  • bool
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, utin64
  • float32, float64
  • time.Time, time.Duration
  • slices of the above items
  • structs
  • pointer to above types
  • nested/embedded types
  • types that implement env.Unmarshaler interface
  • maps

See env_test.go for more examples

DIFFERENCES FROM envconfig

Pointers are not auto-vivified

Using this library, the pointer-to-structs are left as nil given a struct like the following:

type SubConfig struct {
    Foo string
    Bar string
}

type Config struct {
    Sub *SubConfig
}

var c Config

// Make sure that SubConfig cannot be populated
os.Unsetenv("SUB_FOO")
os.Unsetenv("SUB_BAR")

env.Unmarshal(&c)

// If SubConfig is _not_ populated, the struct pointed by
// the c.Sub pointer is left as nil
if c.Sub != nil {
    panic("c.Sub should be nil!")
}

The reason for this is to detect uninitialized fields.

Flexible Source

This is a very minor detail, but you can specify where you get your environment variables from by passing a Source to the decoder object.

env.NewDecoder(env.System).Decode(&c)

Here, we are using the System decoder, which just calls os.LookupEnv. But if you implement the env.Source interface, you can derive your variables from any source you would like.

This may come in handy if you want to do some elaborate testing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(v interface{}) error

Types

type Decoder

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

func NewDecoder

func NewDecoder(src Source) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

func (*Decoder) Prefix

func (d *Decoder) Prefix(s string) *Decoder

type Source

type Source interface {
	LookupEnv(string) (string, bool)
}
var System Source = SourceFunc(os.LookupEnv)

type SourceFunc

type SourceFunc func(string) (string, bool)

func (SourceFunc) LookupEnv

func (sf SourceFunc) LookupEnv(s string) (string, bool)

type Unmarshaler

type Unmarshaler interface {
	UnmarshalEnv(string) error
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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