typenv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2020 License: Unlicense Imports: 4 Imported by: 1

README

Typenv

go.dev go report codecov

Typenv is a minimalistic, zero dependency, typed environment variables library. It does support the following types:

  • Bool
  • Duration
  • Float64
  • Float32
  • Int64
  • Int32
  • Int8
  • Int
  • String

Why typenv?

Handling environment variables in other type than strings or even handling fallback values is not a simple task, typenv makes easy to handle string, boolean, integer, float and time duration environment variables and also allows you to easily set default values for then.

How to use it

Basic usage

Go get the library:

go get diegomarangoni.dev/typenv

And use it:

if typenv.Bool("DEBUG") {
	// do something
}

If the environment is NOT set, the zero value of the type will return, in this case false.

With default value

You can fallback to a default value in case the environment variable is not set.

if typenv.Bool("DEBUG", true) {
	// do something
}

If the environment is NOT set, the return value will be true.

With global default value

You can also set a global default value for a environment. Useful when you use the same environment with same default value several times.

func init() {
	typenv.SetGlobalDefault(
		typenv.E(typenv.Bool, "DEBUG", true),
	)
}

...

if typenv.Bool("DEBUG") {
	// do something
}

If the environment is NOT set, the return value will be true.

Overriding global default value

You still can override the global default value by setting a local default.

func init() {
	typenv.SetGlobalDefault(
		typenv.E(typenv.Bool, "DEBUG", true),
	)
}

...

if typenv.Bool("DEBUG", false) {
	// do something
}

If environment is NOT set, the return value will be false, even if global default is telling that is true.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(name string, defaults ...bool) bool

Bool returns given registered environment variable and mark as used. `true` for those values: y, yes, true, 1; `false` for anything else.

func Duration

func Duration(name string, defaults ...time.Duration) time.Duration

Duration returns given registered environment variable and mark as used

func E

func E(fn interface{}, name string, val interface{}) e

E returns a instance of `e` struct

func Float32

func Float32(name string, defaults ...float32) float32

Float32 returns given registered environment variable and mark as used

func Float64

func Float64(name string, defaults ...float64) float64

Float64 returns given registered environment variable and mark as used

func Int

func Int(name string, defaults ...int) int

Int returns given registered environment variable and mark as used

func Int32

func Int32(name string, defaults ...int32) int32

Int32 returns given registered environment variable and mark as used

func Int64

func Int64(name string, defaults ...int64) int64

Int64 returns given registered environment variable and mark as used

func Int8

func Int8(name string, defaults ...int8) int8

Int8 returns given registered environment variable and mark as used

func SetGlobalDefault

func SetGlobalDefault(l ...e)

SetGlobalDefault register a environment variable global defaul value

func String

func String(name string, defaults ...string) string

String returns given registered environment variable and mark as used

Types

This section is empty.

Jump to

Keyboard shortcuts

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