env

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package env implements pulling environment variables into a struct.

This file is based on code from caarlos0/env, available at github.com/caarlos0/env

Copyright (c) 2015-2024 Carlos Alexandro Becker Licensed under the MIT License. See the LICENSE file in the original project for details.

Modifications made by Kenzo Spaulding, 2024

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParseValue           = errors.New("unable to parse")
	ErrStructPtr            = errors.New("not struct pointer error")
	ErrNoSupportedTagOption = errors.New("tag option not supported")
	ErrVarIsNotSet          = errors.New("required environment variable is not set")
	ErrEmptyVar             = errors.New("environment variable should not be empty")
	ErrLoadFileContent      = errors.New("could not load content of file from variable")
	ErrNoParser             = errors.New("no parser found")
)

Functions

func Must

func Must[T any](t T, err error) T

Must panic is if err is not nil, and returns t otherwise.

func Parse

func Parse(v interface{}) error

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

func ParseAs

func ParseAs[T any]() (T, error)

ParseAs parses the given struct type containing `env` tags and loads its values from environment variables.

func ParseAsWithOptions

func ParseAsWithOptions[T any](opts Options) (T, error)

ParseWithOptions parses the given struct type containing `env` tags and loads its values from environment variables.

func ParseWithOptions

func ParseWithOptions(v interface{}, opts Options) error

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

func ToMap

func ToMap(env []string) map[string]string

ToMap Converts list of env vars as provided by os.Environ() to map you can use as Options.Environment field

Types

type FieldParams

type FieldParams struct {
	OwnKey          string
	Key             string
	DefaultValue    string
	HasDefaultValue bool
	Required        bool
	LoadFile        bool
	Unset           bool
	NotEmpty        bool
	Expand          bool
	Init            bool
	Ignored         bool
}

FieldParams contains information about parsed field tags.

func GetFieldParams

func GetFieldParams(v interface{}) ([]FieldParams, error)

GetFieldParams parses a struct containing `env` tags and returns information about tags it found.

func GetFieldParamsWithOptions

func GetFieldParamsWithOptions(v interface{}, opts Options) ([]FieldParams, error)

GetFieldParamsWithOptions parses a struct containing `env` tags and returns information about tags it found.

type OnSetFn

type OnSetFn func(tag string, value interface{}, isDefault bool)

OnSetFn is a hook that can be run when a value is set.

type Options

type Options struct {
	// Environment keys and values that will be accessible for the service.
	Environment map[string]string

	// TagName specifies another tag name to use rather than the default 'env'.
	TagName string

	// PrefixTagName specifies another prefix tag name to use rather than the default 'envPrefix'.
	PrefixTagName string

	// DefaultValueTagName specifies another default tag name to use rather than the default 'envDefault'.
	DefaultValueTagName string

	// RequiredIfNoDef automatically sets all fields as required if they do not
	// declare 'envDefault'.
	RequiredIfNoDef bool

	// OnSet allows to run a function when a value is set.
	OnSet OnSetFn

	// Prefix define a prefix for every key.
	Prefix string

	// UseFieldNameByDefault defines whether or not `env` should use the field
	// name by default if the `env` key is missing.
	// Note that the field name will be "converted" to conform with environment
	// variable names conventions.
	UseFieldNameByDefault bool

	// Custom parse functions for different types.
	FuncMap map[reflect.Type]ParserFunc
	// 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 `Options`' `FuncMap`.

Jump to

Keyboard shortcuts

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