env

package module
v7.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: MIT Imports: 10 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAStructPtr is returned if you pass something that is not a pointer to a
	// Struct to Parse
	ErrNotAStructPtr = errors.New("env: expected a pointer to a Struct")
)

nolint: gochecknoglobals

View Source
var (

	// GenericMapParser is the definition of the environmnet parsing function for
	// an environment variable to generic Map (map[string]interface{})
	// This parser assumes that the map values are in the form:
	// 		[Key1]=[Value1],[Key2]=[Value2],[Key3]=[Value3]
	// Key/Value pairs are split based on the ',' symbol and each Key and Value is
	// separated by the '=' symbol
	GenericMapParser = func(v string) (interface{}, error) {

		parsedMap := make(map[string]interface{})

		for _, kvPair := range strings.Split(v, ",") {
			mapValues := strings.Split(kvPair, "=")
			if len(mapValues) != 2 {
				return nil, fmt.Errorf("invalid format found in the map environment variable: %s", kvPair)
			}

			parsedMap[mapValues[0]] = mapValues[1]
		}

		return parsedMap, nil
	}
)

Functions

func ExtendedTypeParsers

func ExtendedTypeParsers() map[reflect.Type]ParserFunc

ExtendedTypeParsers puts all of the parsers listed into a single map that can be passed to the primary environment for including these parsers to parse those environment variables

func Parse

func Parse(v interface{}, opts ...Options) error

Parse parses a struct containing `env` tags and loads its values from environment variables.

Example
type inner struct {
	Foo string `env:"FOO" envDefault:"foobar"`
}
type config struct {
	Home         string `env:"HOME,required"`
	Port         int    `env:"PORT" envDefault:"3000"`
	IsProduction bool   `env:"PRODUCTION"`
	Inner        inner
}
os.Setenv("HOME", "/tmp/fakehome")
var cfg config
if err := Parse(&cfg); err != nil {
	fmt.Println("failed:", err)
}
fmt.Printf("%+v", cfg)
Output:

{Home:/tmp/fakehome Port:3000 IsProduction:false Inner:{Foo:foobar}}

func ParseWithFuncs

func ParseWithFuncs(v interface{}, funcMap map[reflect.Type]ParserFunc, opts ...Options) error

ParseWithFuncs is the same as `Parse` except it also allows the user to pass in custom parsers.

Example
type thing struct {
	desc string
}

type conf struct {
	Thing thing `env:"THING"`
}

os.Setenv("THING", "my thing")

c := conf{}

err := ParseWithFuncs(&c, map[reflect.Type]ParserFunc{
	reflect.TypeOf(thing{}): func(v string) (interface{}, error) {
		return thing{desc: v}, nil
	},
})
if err != nil {
	fmt.Println(err)
}
fmt.Println(c.Thing.desc)
Output:

my thing

Types

type EnvParser

type EnvParser interface {
	ParseEnv() error
}

EnvParser defines the interface that allows an object o implement their own custom parsing logic, similar to the json Unmarshal capability

type Options

type Options struct {
	// Environment keys and values that will be accessible for the service.
	Environment map[string]string
	// TagName specifies another tagname to use rather than the default env.
	TagName string
	// contains filtered or unexported fields
}

Options for the parser.

type ParserFunc

type ParserFunc func(v string) (interface{}, error)

ParserFunc defines the signature of a function that can be used within `CustomParsers`

Jump to

Keyboard shortcuts

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