package envconfig
Import Path
github.com/sethvargo/go-envconfig (on go.dev)
Dependency Relation
imports 14 packages, and imported by one package
Involved Source Files
decoding.go
Package envconfig populates struct fields based on environment variable
values (or anything that responds to "Lookup"). Structs declare their
environment dependencies using the "env" tag with the key being the name of
the environment variable, case sensitive.
type MyStruct struct {
A string `env:"A"` // resolves A to $A
B string `env:"B,required"` // resolves B to $B, errors if $B is unset
C string `env:"C,default=foo"` // resolves C to $C, defaults to "foo"
D string `env:"D,required,default=foo"` // error, cannot be required and default
E string `env:""` // error, must specify key
}
All built-in types are supported except Func and Chan. If you need to define
a custom decoder, implement Decoder:
type MyStruct struct {
field string
}
func (v *MyStruct) EnvDecode(val string) error {
v.field = fmt.Sprintf("PREFIX-%s", val)
return nil
}
In the environment, slices are specified as comma-separated values:
export MYVAR="a,b,c,d" // []string{"a", "b", "c", "d"}
In the environment, maps are specified as comma-separated key:value pairs:
export MYVAR="a:b,c:d" // map[string]string{"a":"b", "c":"d"}
For more configuration options and examples, see the documentation.
mutator.go
Package-Level Type Names (total 7)
Base64Bytes is a slice of bytes where the information is base64-encoded in
the environment variable.
Bytes returns the underlying bytes.
EnvDecode implements env.Decoder.
*Base64Bytes : Decoder
Base64Bytes : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice
Config represent inputs to the envconfig decoding.
DefaultDecodeUnset is the default value for running decoders even when no
value was given for the environment variable.
DefaultDelimiter is the default value to use for the delimiter in maps and
slices. This can be overridden on a per-field basis, which takes
precedence. The default value is ",".
DefaultNoInit is the default value for skipping initialization of
unprovided fields. The default value is false (deeply initialize all
fields and nested structs).
DefaultOverwrite is the default value for overwriting an existing value set
on the struct before processing. The default value is false.
DefaultRequired is the default value for marking a field as required. The
default value is false.
DefaultSeparator is the default value to use for the separator in maps.
This can be overridden on a per-field basis, which takes precedence. The
default value is ":".
Lookuper is the lookuper implementation to use. If not provided, it
defaults to the OS Lookuper.
Mutators is an optional list of mutators to apply to lookups.
Target is the destination structure to decode. This value is required, and
it must be a pointer to a struct.
func ProcessWith(ctx context.Context, c *Config) error
Decoder is an interface that custom types/fields can implement to control how
decoding takes place. For example:
type MyType string
func (mt MyType) EnvDecode(val string) error {
return "CUSTOM-"+val
}
( Decoder) EnvDecode(val string) error
*Base64Bytes
*HexBytes
HexBytes is a slice of bytes where the information is hex-encoded in the
environment variable.
Bytes returns the underlying bytes.
EnvDecode implements env.Decoder.
*HexBytes : Decoder
HexBytes : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice
Lookuper is an interface that provides a lookup for a string-based key.
Lookup searches for the given key and returns the corresponding string
value. If a value is found, it returns the value and true. If a value is
not found, it returns the empty string and false.
*github.com/parquet-go/parquet-go.File
golang.org/x/text/internal/catmsg.Dictionary (interface)
golang.org/x/text/message/catalog.Dictionary (interface)
reflect.StructTag
Lookuper : golang.org/x/text/internal/catmsg.Dictionary
Lookuper : golang.org/x/text/message/catalog.Dictionary
func MapLookuper(m map[string]string) Lookuper
func MultiLookuper(lookupers ...Lookuper) Lookuper
func OsLookuper() Lookuper
func PrefixLookuper(prefix string, l Lookuper) Lookuper
func MultiLookuper(lookupers ...Lookuper) Lookuper
func PrefixLookuper(prefix string, l Lookuper) Lookuper
Mutator is the interface for a mutator function. Mutators act like middleware
and alter values for subsequent processing. This is useful if you want to
mutate the environment variable value before it's converted to the proper
type.
Mutators are only called on defined values (or when decodeunset is true).
EnvMutate is called to alter the environment variable value.
- `originalKey` is the unmodified environment variable name as it was defined
on the struct.
- `resolvedKey` is the fully-resolved environment variable name, which may
include prefixes or modifications from processing. When there are
no modifications, this will be equivalent to `originalKey`.
- `originalValue` is the unmodified environment variable's value before any
mutations were run.
- `currentValue` is the currently-resolved value, which may have been
modified by previous mutators and may be modified in the future by
subsequent mutators in the stack.
The function returns (in order):
- The new value to use in both future mutations and final processing.
- A boolean which indicates whether future mutations in the stack should be
applied.
- Any errors that occurred.
MutatorFunc
func MustProcess[T](ctx context.Context, i T, mus ...Mutator) T
func Process(ctx context.Context, i any, mus ...Mutator) error
Package-Level Functions (total 8)
LegacyMutatorFunc is a helper that eases the transition from the previous
MutatorFunc signature. It wraps the previous-style mutator function and
returns a new one. Since the former mutator function had less data, this is
inherently lossy.
Deprecated: Use [MutatorFunc] instead.
MapLookuper looks up environment configuration from a provided map. This is
useful for testing, especially in parallel, since it does not require you to
mutate the parent environment (which is stateful).
MultiLookuper wraps a collection of lookupers. It does not combine them, and
lookups appear in the order in which they are provided to the initializer.
Type Parameters:
T: any
MustProcess is a helper that calls [Process] and panics if an error is
encountered. Unlike [Process], the input value is returned, making it ideal
for anonymous initializations:
var env = envconfig.MustProcess(context.Background(), &struct{
Field string `env:"FIELD,required"`
})
This is not recommend for production services, but it can be useful for quick
CLIs and scripts that want to take advantage of envconfig's environment
parsing at the expense of testability and graceful error handling.
OsLookuper returns a lookuper that uses the environment ([os.LookupEnv]) to
resolve values.
PrefixLookuper looks up environment configuration using the specified prefix.
This is useful if you want all your variables to start with a particular
prefix like "MY_APP_".
Process decodes the struct using values from environment variables. See
[ProcessWith] for a more customizable version.
As a special case, if the input for the target is a [*Config], then this
function will call [ProcessWith] using the provided config, with any mutation
appended.
ProcessWith executes the decoding process using the provided [Config].
Package-Level Constants (total 12)
const ErrInvalidEnvvarName internalError = "invalid environment variable name" const ErrInvalidMapItem internalError = "invalid map item" const ErrLookuperNil internalError = "lookuper cannot be nil" const ErrMissingKey internalError = "missing key" const ErrMissingRequired internalError = "missing required value" const ErrNoInitNotPtr internalError = "field must be a pointer to have noinit" const ErrNotPtr internalError = "input must be a pointer" const ErrNotStruct internalError = "input must be a struct" const ErrPrefixNotStruct internalError = "prefix is only valid on struct types" const ErrPrivateField internalError = "cannot parse private fields" const ErrRequiredAndDefault internalError = "field cannot be required and have a default value" const ErrUnknownOption internalError = "unknown option"![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |