env

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 6 Imported by: 1

README

env: read and write envs with generics

coverage Go Reference Go Report Card

env allows parsing environment values directly to and from native Go types and any custom types that implement the encoding.TextUnmarshaler and/or encoding.TextMarshaler interfaces.

go get github.com/lucafmarques/env

env works with the following native types out of the box:

  • bool
  • string
  • int, variants and aliases
  • uint, variants and aliases
  • float32 and float64
  • complex64 and complex128
  • []T where T is any of the above types
  • encoding.TextUnmarshaler

Custom types must implement encoding.TextUnmarshaler and/or encoding.TextMarshaler to work with env.

Examples

native.go custom.go
package main

import (
    "time"

    "github.com/lucafmarques/env"
)

func main() {
    // MustGet panics if the env isn't set
    intV := env.MustGet[int]("INTEGER")
    strV := env.MustGet[string]("STRING")
    timeV, err := env.Get[time.Time]("TIME")
    // ...
    env.MustSet("COMPLEX", complex128(420))
    err = env.Set("UINT32", uint32(69))
}
package main

import (
    "bytes"
    "fmt"

    "github.com/lucafmarques/env"
)

type log struct {
    Format string
    Level string
}

func (l log) MarshalText() ([]byte, error) {
    s := fmt.Sprintf("%s,%s", e.Format, e.Level) 
    return []byte(s), nil
}

func (l *log) UnmarshalText(d []byte) error {
    v := bytes.Split(d, []byte(","))
    if len(v) != 2 {
        return fmt.Errorf("can't unrmarshal")
    }
    l.Format = string(v[0])
    l.Level = string(v[1])
    return nil
}

func main() {
    logV := env.MustGet[log]("LOG_FORMAT"))
    logV = log{Format: "JSON", Level: "INFO"}
    err := env.Set("LOG_FORMAT", logV)	
}

Documentation

Overview

Package env provides a simple and flexible way of interacting with ENVs directly from and to Go types.

The flexibility of the package stems from the encoding.TextMarshaler and encoding.TextUnmarshaler interfaces, which allows ENVs to be parsed to non-native and user-defined types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnset represents a missing ENV.
	ErrUnset error = errors.New("unset env")
	// ErrMarshaler represents a failure parsing any type to a string ENV.
	ErrMarshaler error = errors.New("type doesn't implement encoding.TextMarshaler")
	// ErrUnmarshaler represents a failure parsing an existing ENV to the any type.
	ErrUnmarshaler error = errors.New("type doesn't implement encoding.TextUnmarshaler")
)

Functions

func Get

func Get[T any](key string, fallback ...T) (value T, err error)

Get attempts to retrieve an ENV and parse it to the given type T.

func MustGet

func MustGet[T any](key string) T

MustGet panics if Get errors.

func MustSet

func MustSet[T any](key string, value T)

MustSet panics if Set errors.

func Set

func Set[T any](key string, value T) error

Set attempts to set an ENV from any type T.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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