Skip to content
Merged
400 changes: 169 additions & 231 deletions README.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions constraint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package getenv

import (
"net"
"net/netip"
"net/url"
"time"
)

// Parsable is a constraint for the types that Env and EnvOrDefault can parse.
// Every type is supported both as a single value and as a slice.
type Parsable interface {
string | []string |
int | []int | int8 | []int8 | int16 | []int16 | int32 | []int32 | int64 | []int64 |
uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 | uintptr | []uintptr |
float32 | []float32 | float64 | []float64 |
complex64 | []complex64 | complex128 | []complex128 |
bool | []bool |
time.Time | []time.Time | time.Duration | []time.Duration |
url.URL | []url.URL |
net.IP | []net.IP | net.HardwareAddr | []net.HardwareAddr |
netip.Addr | []netip.Addr | netip.Prefix | []netip.Prefix
}

// Constraints of the number parsers.
type (
signed interface {
int | int8 | int16 | int32 | int64
}

unsigned interface {
uint | uint8 | uint16 | uint32 | uint64 | uintptr
}

float interface {
float32 | float64
}

complexNumber interface {
complex64 | complex128
}
)
97 changes: 12 additions & 85 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,40 @@
// Note: empty environment variable values are treated as "not set".
//
// Slices are split by "," and time.Time values are parsed with the time.RFC3339 layout,
// unless option.WithSeparator or option.WithTimeLayout sets another one.
// unless WithSeparator or WithTimeLayout sets another one.
// Slice elements are not trimmed, so "1, 2" is not a valid []int,
// and a trailing separator adds an empty element: "a,b," is []string{"a", "b", ""}.
//
// Types supported:
// - string
// - []string
// - int
// - []int
// - int8
// - []int8
// - int16
// - []int16
// - int32
// - []int32
// - int64
// - []int64
// - uint8
// - []uint8
// - uint16
// - []uint16
// - uint64
// - []uint64
// - uint
// - []uint
// - uintptr
// - []uintptr
// - uint32
// - []uint32
// - float32
// - []float32
// - float64
// - []float64
// - time.Time
// - []time.Time
// - time.Duration
// - []time.Duration
// - bool
// - []bool
// - url.URL
// - []url.URL
// - net.IP
// - []net.IP
// - netip.Addr
// - []netip.Addr
// - netip.Prefix
// - []netip.Prefix
// - net.HardwareAddr
// - []net.HardwareAddr
// - complex64
// - []complex64
// - complex128
// - []complex128
// The supported types are listed in Parsable.
package getenv

import (
"errors"
"fmt"
"os"
"time"

"github.com/obalunenko/getenv/internal"
"github.com/obalunenko/getenv/option"
)

var (
// ErrNotSet is an error that is returned when the environment variable is not set.
ErrNotSet = internal.ErrNotSet
ErrNotSet = errors.New("not set")
// ErrInvalidValue is an error that is returned when the environment variable is not valid.
ErrInvalidValue = internal.ErrInvalidValue
)

// Defaults for the options that were not passed.
const (
defaultSeparator = ","
defaultTimeLayout = time.RFC3339
ErrInvalidValue = errors.New("invalid value")
)

// Parsable is a constraint for the types that Env and EnvOrDefault can parse.
// The list of supported types is in the package documentation.
type Parsable = internal.EnvParsable

// Env retrieves the value of the environment variable named by the key.
// If the variable is present in the environment, the value will be parsed and returned.
// Otherwise, an error will be returned.
func Env[T Parsable](key string, options ...option.Option) (T, error) {
func Env[T Parsable](key string, options ...Option) (T, error) {
var zero T

raw, ok := os.LookupEnv(key)
if !ok || raw == "" {
return zero, fmt.Errorf("failed to get environment variable[%s]: %w", key, ErrNotSet)
return zero, fmt.Errorf("environment variable %q: %w", key, ErrNotSet)
}

val, err := internal.Parse[T](raw, newParseParams(options))
val, err := parseAs[T](raw, newParams(options))
if err != nil {
return zero, fmt.Errorf("failed to parse environment variable[%s]: %w", key, err)
return zero, fmt.Errorf("environment variable %q: %w", key, err)
}

return val, nil
Expand All @@ -105,25 +46,11 @@ func Env[T Parsable](key string, options ...option.Option) (T, error) {
// If the variable is set and its value can be parsed, the parsed value is returned.
// Otherwise, defaultVal is returned: both when the variable is not set (or empty)
// and when its value is invalid. Use Env to tell these cases apart.
func EnvOrDefault[T Parsable](key string, defaultVal T, options ...option.Option) T {
func EnvOrDefault[T Parsable](key string, defaultVal T, options ...Option) T {
val, err := Env[T](key, options...)
if err != nil {
return defaultVal
}

return val
}

// newParseParams creates new parameters from the defaults and options.
func newParseParams(opts []option.Option) internal.Parameters {
p := internal.Parameters{
Separator: defaultSeparator,
Layout: defaultTimeLayout,
}

for _, opt := range opts {
opt.Apply(&p)
}

return p
}
Loading