pax_global_header00006660000000000000000000000064147312543620014521gustar00rootroot0000000000000052 comment=9bf044aa0830bee858473a7bf6484cae2bf40b21 env-11.3.1/000077500000000000000000000000001473125436200123745ustar00rootroot00000000000000env-11.3.1/.editorconfig000066400000000000000000000007111473125436200150500ustar00rootroot00000000000000[*] charset = utf-8 end_of_line = lf trim_trailing_whitespace = true max_line_length = 120 [{go.mod,go.sum,*.go}] insert_final_newline = true indent_size = tab indent_style = tab tab_width = 4 [Makefile] max_line_length = off insert_final_newline = true indent_size = tab indent_style = tab tab_width = 4 [*.md] max_line_length = off trim_trailing_whitespace = false indent_size = tab indent_style = space tab_width = 2 [.mailmap] max_line_length = off env-11.3.1/.github/000077500000000000000000000000001473125436200137345ustar00rootroot00000000000000env-11.3.1/.github/FUNDING.yml000066400000000000000000000000231473125436200155440ustar00rootroot00000000000000github: [caarlos0] env-11.3.1/.github/dependabot.yml000066400000000000000000000004731473125436200165700ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: "gomod" directory: "/" schedule: interval: "daily" time: "08:00" labels: - "dependencies" - package-ecosystem: "github-actions" directory: "/" schedule: interval: "daily" time: "08:00" labels: - "dependencies" env-11.3.1/.github/workflows/000077500000000000000000000000001473125436200157715ustar00rootroot00000000000000env-11.3.1/.github/workflows/build.yml000066400000000000000000000036101473125436200176130ustar00rootroot00000000000000name: build on: push: branches: - "main" tags: - "v*" pull_request: jobs: govulncheck: uses: caarlos0/meta/.github/workflows/govulncheck.yml@main semgrep: uses: caarlos0/meta/.github/workflows/semgrep.yml@main ruleguard: uses: caarlos0/meta/.github/workflows/ruleguard.yml@main build: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] go-version: [1.18, oldstable, stable] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} - run: make setup ci - uses: codecov/codecov-action@v5 if: matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable' with: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.txt - uses: goreleaser/goreleaser-action@v6 if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable' # editorconfig-checker-disable-line with: version: latest distribution: goreleaser-pro args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} dependabot: needs: [build] runs-on: ubuntu-latest permissions: pull-requests: write contents: write if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request'}} steps: - id: metadata uses: dependabot/fetch-metadata@v2 with: github-token: "${{ secrets.GITHUB_TOKEN }}" - run: | gh pr review --approve "$PR_URL" gh pr merge --squash --auto "$PR_URL" env: PR_URL: ${{github.event.pull_request.html_url}} GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} env-11.3.1/.github/workflows/lint.yml000066400000000000000000000007771473125436200174750ustar00rootroot00000000000000name: linters on: push: pull_request: jobs: editorconfig: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: editorconfig-checker/action-editorconfig-checker@main - run: editorconfig-checker golangci: name: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: ~1.19 cache: true - name: golangci-lint uses: golangci/golangci-lint-action@v6 env-11.3.1/.gitignore000066400000000000000000000000501473125436200143570ustar00rootroot00000000000000coverage.txt bin card.png dist codecov* env-11.3.1/.golangci.yml000066400000000000000000000010531473125436200147570ustar00rootroot00000000000000linters-settings: gocritic: enabled-checks: - emptyStringTest - evalOrder - paramTypeCombine - preferStringWriter - sprintfQuotedString - stringConcatSimplify - yodaStyleExpr revive: rules: - name: line-length-limit arguments: [120] issues: exclude-rules: - path: _test\.go linters: - revive text: "line-length-limit:" linters: enable: - thelper - gofumpt - gocritic - tparallel - unconvert - unparam - wastedassign - revive env-11.3.1/.goreleaser.yml000066400000000000000000000003051473125436200153230ustar00rootroot00000000000000# yaml-language-server: $schema=https://goreleaser.com/static/schema-pro.json version: 2 includes: - from_url: url: https://raw.githubusercontent.com/caarlos0/.goreleaserfiles/main/lib.yml env-11.3.1/.mailmap000066400000000000000000000014271473125436200140210ustar00rootroot00000000000000Carlos Alexandro Becker Carlos A Becker Carlos Alexandro Becker Carlos A Becker Carlos Alexandro Becker Carlos Alexandro Becker Carlos Alexandro Becker Carlos Alexandro Becker Carlos Alexandro Becker Carlos Becker dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> actions-user github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> env-11.3.1/LICENSE.md000066400000000000000000000021071473125436200140000ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2024 Carlos Alexandro Becker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. env-11.3.1/Makefile000066400000000000000000000012111473125436200140270ustar00rootroot00000000000000SOURCE_FILES?=./... TEST_PATTERN?=. export GO111MODULE := on setup: go mod tidy .PHONY: setup build: go build .PHONY: build test: go test -v -failfast -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m .PHONY: test cover: test go tool cover -html=coverage.txt .PHONY: cover fmt: gofumpt -w -l . .PHONY: fmt lint: golangci-lint run ./... .PHONY: lint ci: build test .PHONY: ci card: wget -O card.png -c "https://og.caarlos0.dev/**env**: parse envs to structs.png?theme=light&md=1&fontSize=100px&images=https://github.com/caarlos0.png" .PHONY: card .DEFAULT_GOAL := ci env-11.3.1/README.md000066400000000000000000000127301473125436200136560ustar00rootroot00000000000000

GoReleaser Logo

A simple, zero-dependencies library to parse environment variables into structs.

###### Installation ```bash go get github.com/caarlos0/env/v11 ``` ###### Getting started ```go type config struct { Home string `env:"HOME"` } // parse var cfg config err := env.Parse(&cfg) // parse with generics cfg, err := env.ParseAs[config]() ``` You can see the full documentation and list of examples at [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11). --- ## Used and supported by

encore icon

Encore – the platform for building Go-based cloud backends.

## Usage ### Caveats > [!CAUTION] > > _Unexported fields_ will be **ignored** by `env`. > This is by design and will not change. ### Functions - `Parse`: parse the current environment into a type - `ParseAs`: parse the current environment into a type using generics - `ParseWithOptions`: parse the current environment into a type with custom options - `ParseAsWithOptions`: parse the current environment into a type with custom options and using generics - `Must`: can be used to wrap `Parse.*` calls to panic on error - `GetFieldParams`: get the `env` parsed options for a type - `GetFieldParamsWithOptions`: get the `env` parsed options for a type with custom options ### Supported types Out of the box all built-in types are supported, plus a few others that are commonly used. Complete list: - `bool` - `float32` - `float64` - `int16` - `int32` - `int64` - `int8` - `int` - `string` - `uint16` - `uint32` - `uint64` - `uint8` - `uint` - `time.Duration` - `time.Location` - `encoding.TextUnmarshaler` - `url.URL` Pointers, slices and slices of pointers, and maps of those types are also supported. You may also add custom parsers for your types. ### Tags The following tags are provided: - `env`: sets the environment variable name and optionally takes the tag options described below - `envDefault`: sets the default value for the field - `envPrefix`: can be used in a field that is a complex type to set a prefix to all environment variables used in it - `envSeparator`: sets the character to be used to separate items in slices and maps (default: `,`) - `envKeyValSeparator`: sets the character to be used to separate keys and their values in maps (default: `:`) ### `env` tag options Here are all the options available for the `env` tag: - `,expand`: expands environment variables, e.g. `FOO_${BAR}` - `,file`: instructs that the content of the variable is a path to a file that should be read - `,init`: initialize nil pointers - `,notEmpty`: make the field errors if the environment variable is empty - `,required`: make the field errors if the environment variable is not set - `,unset`: unset the environment variable after use ### Parse Options There are a few options available in the functions that end with `WithOptions`: - `Environment`: keys and values to be used instead of `os.Environ()` - `TagName`: specifies another tag name to use rather than the default `env` - `PrefixTagName`: specifies another prefix tag name to use rather than the default `envPrefix` - `DefaultValueTagName`: specifies another default tag name to use rather than the default `envDefault` - `RequiredIfNoDef`: set all `env` fields as required if they do not declare `envDefault` - `OnSet`: allows to hook into the `env` parsing and do something when a value is set - `Prefix`: prefix to be used in all environment variables - `UseFieldNameByDefault`: defines whether or not `env` should use the field name by default if the `env` key is missing - `FuncMap`: custom parse functions for custom types ### Documentation and examples Examples are live in [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11), and also in the [example test file](./example_test.go). ## Current state `env` is considered feature-complete. I do not intent to add any new features unless they really make sense, and are requested by many people. Eventual bug fixes will keep being merged. ## Badges [![Release](https://img.shields.io/github/release/caarlos0/env.svg?style=for-the-badge)](https://github.com/goreleaser/goreleaser/releases/latest) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=for-the-badge)](/LICENSE.md) [![Build status](https://img.shields.io/github/actions/workflow/status/caarlos0/env/build.yml?style=for-the-badge&branch=main)](https://github.com/caarlos0/env/actions?workflow=build) [![Codecov branch](https://img.shields.io/codecov/c/github/caarlos0/env/main.svg?style=for-the-badge)](https://codecov.io/gh/caarlos0/env) [![Go docs](https://img.shields.io/badge/godoc-reference-blue.svg?style=for-the-badge)](http://godoc.org/github.com/caarlos0/env/v11) [![Powered By: GoReleaser](https://img.shields.io/badge/powered%20by-goreleaser-green.svg?style=for-the-badge)](https://github.com/goreleaser) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=for-the-badge)](https://conventionalcommits.org) ## Related projects - [envdoc](https://github.com/g4s8/envdoc) - generate documentation for environment variables from `env` tags ## Stargazers over time [![Stargazers over time](https://starchart.cc/caarlos0/env.svg)](https://starchart.cc/caarlos0/env) env-11.3.1/env.go000066400000000000000000000517031473125436200135210ustar00rootroot00000000000000// Package env is a simple, zero-dependencies library to parse environment // variables into structs. // // Example: // // type config struct { // Home string `env:"HOME"` // } // // parse // var cfg config // err := env.Parse(&cfg) // // or parse with generics // cfg, err := env.ParseAs[config]() // // Check the examples and README for more detailed usage. package env import ( "encoding" "fmt" "net/url" "os" "reflect" "strconv" "strings" "time" "unicode" ) // nolint: gochecknoglobals var ( defaultBuiltInParsers = map[reflect.Kind]ParserFunc{ reflect.Bool: func(v string) (interface{}, error) { return strconv.ParseBool(v) }, reflect.String: func(v string) (interface{}, error) { return v, nil }, reflect.Int: func(v string) (interface{}, error) { i, err := strconv.ParseInt(v, 10, 32) return int(i), err }, reflect.Int16: func(v string) (interface{}, error) { i, err := strconv.ParseInt(v, 10, 16) return int16(i), err }, reflect.Int32: func(v string) (interface{}, error) { i, err := strconv.ParseInt(v, 10, 32) return int32(i), err }, reflect.Int64: func(v string) (interface{}, error) { return strconv.ParseInt(v, 10, 64) }, reflect.Int8: func(v string) (interface{}, error) { i, err := strconv.ParseInt(v, 10, 8) return int8(i), err }, reflect.Uint: func(v string) (interface{}, error) { i, err := strconv.ParseUint(v, 10, 32) return uint(i), err }, reflect.Uint16: func(v string) (interface{}, error) { i, err := strconv.ParseUint(v, 10, 16) return uint16(i), err }, reflect.Uint32: func(v string) (interface{}, error) { i, err := strconv.ParseUint(v, 10, 32) return uint32(i), err }, reflect.Uint64: func(v string) (interface{}, error) { i, err := strconv.ParseUint(v, 10, 64) return i, err }, reflect.Uint8: func(v string) (interface{}, error) { i, err := strconv.ParseUint(v, 10, 8) return uint8(i), err }, reflect.Float64: func(v string) (interface{}, error) { return strconv.ParseFloat(v, 64) }, reflect.Float32: func(v string) (interface{}, error) { f, err := strconv.ParseFloat(v, 32) return float32(f), err }, } ) func defaultTypeParsers() map[reflect.Type]ParserFunc { return map[reflect.Type]ParserFunc{ reflect.TypeOf(url.URL{}): parseURL, reflect.TypeOf(time.Nanosecond): parseDuration, reflect.TypeOf(time.Location{}): parseLocation, } } func parseURL(v string) (interface{}, error) { u, err := url.Parse(v) if err != nil { return nil, newParseValueError("unable to parse URL", err) } return *u, nil } func parseDuration(v string) (interface{}, error) { d, err := time.ParseDuration(v) if err != nil { return nil, newParseValueError("unable to parse duration", err) } return d, err } func parseLocation(v string) (interface{}, error) { loc, err := time.LoadLocation(v) if err != nil { return nil, newParseValueError("unable to parse location", err) } return *loc, nil } // ParserFunc defines the signature of a function that can be used within // `Options`' `FuncMap`. type ParserFunc func(v string) (interface{}, error) // OnSetFn is a hook that can be run when a value is set. type OnSetFn func(tag string, value interface{}, isDefault bool) // processFieldFn is a function which takes all information about a field and processes it. type processFieldFn func( refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams, ) error // Options for the parser. 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 // Used internally. maps the env variable key to its resolved string value. // (for env var expansion) rawEnvVars map[string]string } func (opts *Options) getRawEnv(s string) string { val := opts.rawEnvVars[s] if val == "" { val = opts.Environment[s] } return os.Expand(val, opts.getRawEnv) } func defaultOptions() Options { return Options{ TagName: "env", PrefixTagName: "envPrefix", DefaultValueTagName: "envDefault", Environment: toMap(os.Environ()), FuncMap: defaultTypeParsers(), rawEnvVars: make(map[string]string), } } func mergeOptions[T any](target, source *T) { targetPtr := reflect.ValueOf(target).Elem() sourcePtr := reflect.ValueOf(source).Elem() targetType := targetPtr.Type() for i := 0; i < targetPtr.NumField(); i++ { fieldName := targetType.Field(i).Name targetField := targetPtr.Field(i) sourceField := sourcePtr.FieldByName(fieldName) if targetField.CanSet() && !isZero(sourceField) { // FuncMaps are being merged, while Environments must be overwritten if fieldName == "FuncMap" { if !sourceField.IsZero() { iter := sourceField.MapRange() for iter.Next() { targetField.SetMapIndex(iter.Key(), iter.Value()) } } } else { targetField.Set(sourceField) } } } } func isZero(v reflect.Value) bool { switch v.Kind() { case reflect.Func, reflect.Map, reflect.Slice: return v.IsNil() default: zero := reflect.Zero(v.Type()) return v.Interface() == zero.Interface() } } func customOptions(opts Options) Options { defOpts := defaultOptions() mergeOptions(&defOpts, &opts) return defOpts } func optionsWithSliceEnvPrefix(opts Options, index int) Options { return Options{ Environment: opts.Environment, TagName: opts.TagName, PrefixTagName: opts.PrefixTagName, DefaultValueTagName: opts.DefaultValueTagName, RequiredIfNoDef: opts.RequiredIfNoDef, OnSet: opts.OnSet, Prefix: fmt.Sprintf("%s%d_", opts.Prefix, index), UseFieldNameByDefault: opts.UseFieldNameByDefault, FuncMap: opts.FuncMap, rawEnvVars: opts.rawEnvVars, } } func optionsWithEnvPrefix(field reflect.StructField, opts Options) Options { return Options{ Environment: opts.Environment, TagName: opts.TagName, PrefixTagName: opts.PrefixTagName, DefaultValueTagName: opts.DefaultValueTagName, RequiredIfNoDef: opts.RequiredIfNoDef, OnSet: opts.OnSet, Prefix: opts.Prefix + field.Tag.Get(opts.PrefixTagName), UseFieldNameByDefault: opts.UseFieldNameByDefault, FuncMap: opts.FuncMap, rawEnvVars: opts.rawEnvVars, } } // Parse parses a struct containing `env` tags and loads its values from // environment variables. func Parse(v interface{}) error { return parseInternal(v, setField, defaultOptions()) } // ParseWithOptions parses a struct containing `env` tags and loads its values from // environment variables. func ParseWithOptions(v interface{}, opts Options) error { return parseInternal(v, setField, customOptions(opts)) } // ParseAs parses the given struct type containing `env` tags and loads its // values from environment variables. func ParseAs[T any]() (T, error) { var t T err := Parse(&t) return t, err } // ParseWithOptions parses the given struct type containing `env` tags and // loads its values from environment variables. func ParseAsWithOptions[T any](opts Options) (T, error) { var t T err := ParseWithOptions(&t, opts) return t, err } // Must panic is if err is not nil, and returns t otherwise. func Must[T any](t T, err error) T { if err != nil { panic(err) } return t } // GetFieldParams parses a struct containing `env` tags and returns information about // tags it found. func GetFieldParams(v interface{}) ([]FieldParams, error) { return GetFieldParamsWithOptions(v, defaultOptions()) } // GetFieldParamsWithOptions parses a struct containing `env` tags and returns information about // tags it found. func GetFieldParamsWithOptions(v interface{}, opts Options) ([]FieldParams, error) { var result []FieldParams err := parseInternal( v, func(_ reflect.Value, _ reflect.StructField, _ Options, fieldParams FieldParams) error { if fieldParams.OwnKey != "" { result = append(result, fieldParams) } return nil }, customOptions(opts), ) if err != nil { return nil, err } return result, nil } func parseInternal(v interface{}, processField processFieldFn, opts Options) error { ptrRef := reflect.ValueOf(v) if ptrRef.Kind() != reflect.Ptr { return newAggregateError(NotStructPtrError{}) } ref := ptrRef.Elem() if ref.Kind() != reflect.Struct { return newAggregateError(NotStructPtrError{}) } return doParse(ref, processField, opts) } func doParse(ref reflect.Value, processField processFieldFn, opts Options) error { refType := ref.Type() var agrErr AggregateError for i := 0; i < refType.NumField(); i++ { refField := ref.Field(i) refTypeField := refType.Field(i) if err := doParseField(refField, refTypeField, processField, opts); err != nil { if val, ok := err.(AggregateError); ok { agrErr.Errors = append(agrErr.Errors, val.Errors...) } else { agrErr.Errors = append(agrErr.Errors, err) } } } if len(agrErr.Errors) == 0 { return nil } return agrErr } func doParseField( refField reflect.Value, refTypeField reflect.StructField, processField processFieldFn, opts Options, ) error { if !refField.CanSet() { return nil } if refField.Kind() == reflect.Ptr && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() { return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts)) } if refField.Kind() == reflect.Struct && refField.CanAddr() && refField.Type().Name() == "" { return parseInternal(refField.Addr().Interface(), processField, optionsWithEnvPrefix(refTypeField, opts)) } params, err := parseFieldParams(refTypeField, opts) if err != nil { return err } if params.Ignored { return nil } if err := processField(refField, refTypeField, opts, params); err != nil { return err } if params.Init && isInvalidPtr(refField) { refField.Set(reflect.New(refField.Type().Elem())) refField = refField.Elem() } if refField.Kind() == reflect.Struct { return doParse(refField, processField, optionsWithEnvPrefix(refTypeField, opts)) } if isSliceOfStructs(refTypeField) { return doParseSlice(refField, processField, optionsWithEnvPrefix(refTypeField, opts)) } return nil } func isSliceOfStructs(refTypeField reflect.StructField) bool { field := refTypeField.Type // *[]struct if field.Kind() == reflect.Ptr { field = field.Elem() if field.Kind() == reflect.Slice && field.Elem().Kind() == reflect.Struct { return true } } // []struct{} if field.Kind() == reflect.Slice && field.Elem().Kind() == reflect.Struct { return true } return false } func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options) error { if opts.Prefix != "" && !strings.HasSuffix(opts.Prefix, string(underscore)) { opts.Prefix += string(underscore) } var environments []string for environment := range opts.Environment { if strings.HasPrefix(environment, opts.Prefix) { environments = append(environments, environment) } } if len(environments) > 0 { counter := 0 for finished := false; !finished; { finished = true prefix := fmt.Sprintf("%s%d%c", opts.Prefix, counter, underscore) for _, variable := range environments { if strings.HasPrefix(variable, prefix) { counter++ finished = false break } } } sliceType := ref.Type() var initialized int if reflect.Ptr == ref.Kind() { sliceType = sliceType.Elem() // Due to the rest of code the pre-initialized slice has no chance for this situation initialized = 0 } else { initialized = ref.Len() } var capacity int if capacity = initialized; counter > initialized { capacity = counter } result := reflect.MakeSlice(sliceType, capacity, capacity) for i := 0; i < capacity; i++ { item := result.Index(i) if i < initialized { item.Set(ref.Index(i)) } if err := doParse(item, processField, optionsWithSliceEnvPrefix(opts, i)); err != nil { return err } } if result.Len() > 0 { if reflect.Ptr == ref.Kind() { resultPtr := reflect.New(sliceType) resultPtr.Elem().Set(result) result = resultPtr } ref.Set(result) } } return nil } func setField(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error { value, err := get(fieldParams, opts) if err != nil { return err } if value != "" { return set(refField, refTypeField, value, opts.FuncMap) } return nil } const underscore rune = '_' func toEnvName(input string) string { var output []rune for i, c := range input { if c == underscore { continue } if len(output) > 0 && unicode.IsUpper(c) { if len(input) > i+1 { peek := rune(input[i+1]) if unicode.IsLower(peek) || unicode.IsLower(rune(input[i-1])) { output = append(output, underscore) } } } output = append(output, unicode.ToUpper(c)) } return string(output) } // FieldParams contains information about parsed field tags. 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 } func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, error) { ownKey, tags := parseKeyForOption(field.Tag.Get(opts.TagName)) if ownKey == "" && opts.UseFieldNameByDefault { ownKey = toEnvName(field.Name) } defaultValue, hasDefaultValue := field.Tag.Lookup(opts.DefaultValueTagName) result := FieldParams{ OwnKey: ownKey, Key: opts.Prefix + ownKey, Required: opts.RequiredIfNoDef, DefaultValue: defaultValue, HasDefaultValue: hasDefaultValue, Ignored: ownKey == "-", } for _, tag := range tags { switch tag { case "": continue case "file": result.LoadFile = true case "required": result.Required = true case "unset": result.Unset = true case "notEmpty": result.NotEmpty = true case "expand": result.Expand = true case "init": result.Init = true case "-": result.Ignored = true default: return FieldParams{}, newNoSupportedTagOptionError(tag) } } return result, nil } func get(fieldParams FieldParams, opts Options) (val string, err error) { var exists, isDefault bool val, exists, isDefault = getOr( fieldParams.Key, fieldParams.DefaultValue, fieldParams.HasDefaultValue, opts.Environment, ) if fieldParams.Expand { val = os.Expand(val, opts.getRawEnv) } opts.rawEnvVars[fieldParams.OwnKey] = val if fieldParams.Unset { defer os.Unsetenv(fieldParams.Key) } if fieldParams.Required && !exists && fieldParams.OwnKey != "" { return "", newVarIsNotSetError(fieldParams.Key) } if fieldParams.NotEmpty && val == "" { return "", newEmptyVarError(fieldParams.Key) } if fieldParams.LoadFile && val != "" { filename := val val, err = getFromFile(filename) if err != nil { return "", newLoadFileContentError(filename, fieldParams.Key, err) } } if opts.OnSet != nil { if fieldParams.OwnKey != "" { opts.OnSet(fieldParams.Key, val, isDefault) } } return val, err } // split the env tag's key into the expected key and desired option, if any. func parseKeyForOption(key string) (string, []string) { opts := strings.Split(key, ",") return opts[0], opts[1:] } func getFromFile(filename string) (value string, err error) { b, err := os.ReadFile(filename) return string(b), err } func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists, isDefault bool) { value, exists := envs[key] switch { case (!exists || key == "") && defExists: return defaultValue, true, true case exists && value == "" && defExists: return defaultValue, true, true case !exists: return "", false, false } return value, true, false } func set(field reflect.Value, sf reflect.StructField, value string, funcMap map[reflect.Type]ParserFunc) error { if tm := asTextUnmarshaler(field); tm != nil { if err := tm.UnmarshalText([]byte(value)); err != nil { return newParseError(sf, err) } return nil } typee := sf.Type fieldee := field if typee.Kind() == reflect.Ptr { typee = typee.Elem() fieldee = field.Elem() } parserFunc, ok := funcMap[typee] if ok { val, err := parserFunc(value) if err != nil { return newParseError(sf, err) } fieldee.Set(reflect.ValueOf(val)) return nil } parserFunc, ok = defaultBuiltInParsers[typee.Kind()] if ok { val, err := parserFunc(value) if err != nil { return newParseError(sf, err) } fieldee.Set(reflect.ValueOf(val).Convert(typee)) return nil } switch field.Kind() { case reflect.Slice: return handleSlice(field, value, sf, funcMap) case reflect.Map: return handleMap(field, value, sf, funcMap) } return newNoParserError(sf) } func handleSlice(field reflect.Value, value string, sf reflect.StructField, funcMap map[reflect.Type]ParserFunc) error { separator := sf.Tag.Get("envSeparator") if separator == "" { separator = "," } parts := strings.Split(value, separator) typee := sf.Type.Elem() if typee.Kind() == reflect.Ptr { typee = typee.Elem() } if _, ok := reflect.New(typee).Interface().(encoding.TextUnmarshaler); ok { return parseTextUnmarshalers(field, parts, sf) } parserFunc, ok := funcMap[typee] if !ok { parserFunc, ok = defaultBuiltInParsers[typee.Kind()] if !ok { return newNoParserError(sf) } } result := reflect.MakeSlice(sf.Type, 0, len(parts)) for _, part := range parts { r, err := parserFunc(part) if err != nil { return newParseError(sf, err) } v := reflect.ValueOf(r).Convert(typee) if sf.Type.Elem().Kind() == reflect.Ptr { v = reflect.New(typee) v.Elem().Set(reflect.ValueOf(r).Convert(typee)) } result = reflect.Append(result, v) } field.Set(result) return nil } func handleMap(field reflect.Value, value string, sf reflect.StructField, funcMap map[reflect.Type]ParserFunc) error { keyType := sf.Type.Key() keyParserFunc, ok := funcMap[keyType] if !ok { keyParserFunc, ok = defaultBuiltInParsers[keyType.Kind()] if !ok { return newNoParserError(sf) } } elemType := sf.Type.Elem() elemParserFunc, ok := funcMap[elemType] if !ok { elemParserFunc, ok = defaultBuiltInParsers[elemType.Kind()] if !ok { return newNoParserError(sf) } } separator := sf.Tag.Get("envSeparator") if separator == "" { separator = "," } keyValSeparator := sf.Tag.Get("envKeyValSeparator") if keyValSeparator == "" { keyValSeparator = ":" } result := reflect.MakeMap(sf.Type) for _, part := range strings.Split(value, separator) { pairs := strings.SplitN(part, keyValSeparator, 2) if len(pairs) != 2 { return newParseError(sf, fmt.Errorf(`%q should be in "key%svalue" format`, part, keyValSeparator)) } key, err := keyParserFunc(pairs[0]) if err != nil { return newParseError(sf, err) } elem, err := elemParserFunc(pairs[1]) if err != nil { return newParseError(sf, err) } result.SetMapIndex(reflect.ValueOf(key).Convert(keyType), reflect.ValueOf(elem).Convert(elemType)) } field.Set(result) return nil } func asTextUnmarshaler(field reflect.Value) encoding.TextUnmarshaler { if field.Kind() == reflect.Ptr { if field.IsNil() { field.Set(reflect.New(field.Type().Elem())) } } else if field.CanAddr() { field = field.Addr() } tm, ok := field.Interface().(encoding.TextUnmarshaler) if !ok { return nil } return tm } func parseTextUnmarshalers(field reflect.Value, data []string, sf reflect.StructField) error { s := len(data) elemType := field.Type().Elem() slice := reflect.MakeSlice(reflect.SliceOf(elemType), s, s) for i, v := range data { sv := slice.Index(i) kind := sv.Kind() if kind == reflect.Ptr { sv = reflect.New(elemType.Elem()) } else { sv = sv.Addr() } tm := sv.Interface().(encoding.TextUnmarshaler) if err := tm.UnmarshalText([]byte(v)); err != nil { return newParseError(sf, err) } if kind == reflect.Ptr { slice.Index(i).Set(sv) } } field.Set(slice) return nil } // ToMap Converts list of env vars as provided by os.Environ() to map you // can use as Options.Environment field func ToMap(env []string) map[string]string { return toMap(env) } func isInvalidPtr(v reflect.Value) bool { return reflect.Ptr == v.Kind() && v.Elem().Kind() == reflect.Invalid } env-11.3.1/env_test.go000066400000000000000000001726031473125436200145630ustar00rootroot00000000000000// editorconfig-checker-disable-file package env import ( "encoding/base64" "encoding/json" "errors" "fmt" "net/http" "net/url" "os" "path/filepath" "reflect" "runtime" "strconv" "strings" "testing" "time" ) type unmarshaler struct { time.Duration } // TextUnmarshaler implements encoding.TextUnmarshaler. func (d *unmarshaler) UnmarshalText(data []byte) (err error) { if len(data) != 0 { d.Duration, err = time.ParseDuration(string(data)) } else { d.Duration = 0 } return err } // nolint: maligned type Config struct { String string `env:"STRING"` StringPtr *string `env:"STRING"` Strings []string `env:"STRINGS"` StringPtrs []*string `env:"STRINGS"` Bool bool `env:"BOOL"` BoolPtr *bool `env:"BOOL"` Bools []bool `env:"BOOLS"` BoolPtrs []*bool `env:"BOOLS"` Int int `env:"INT"` IntPtr *int `env:"INT"` Ints []int `env:"INTS"` IntPtrs []*int `env:"INTS"` Int8 int8 `env:"INT8"` Int8Ptr *int8 `env:"INT8"` Int8s []int8 `env:"INT8S"` Int8Ptrs []*int8 `env:"INT8S"` Int16 int16 `env:"INT16"` Int16Ptr *int16 `env:"INT16"` Int16s []int16 `env:"INT16S"` Int16Ptrs []*int16 `env:"INT16S"` Int32 int32 `env:"INT32"` Int32Ptr *int32 `env:"INT32"` Int32s []int32 `env:"INT32S"` Int32Ptrs []*int32 `env:"INT32S"` Int64 int64 `env:"INT64"` Int64Ptr *int64 `env:"INT64"` Int64s []int64 `env:"INT64S"` Int64Ptrs []*int64 `env:"INT64S"` Uint uint `env:"UINT"` UintPtr *uint `env:"UINT"` Uints []uint `env:"UINTS"` UintPtrs []*uint `env:"UINTS"` Uint8 uint8 `env:"UINT8"` Uint8Ptr *uint8 `env:"UINT8"` Uint8s []uint8 `env:"UINT8S"` Uint8Ptrs []*uint8 `env:"UINT8S"` Uint16 uint16 `env:"UINT16"` Uint16Ptr *uint16 `env:"UINT16"` Uint16s []uint16 `env:"UINT16S"` Uint16Ptrs []*uint16 `env:"UINT16S"` Uint32 uint32 `env:"UINT32"` Uint32Ptr *uint32 `env:"UINT32"` Uint32s []uint32 `env:"UINT32S"` Uint32Ptrs []*uint32 `env:"UINT32S"` Uint64 uint64 `env:"UINT64"` Uint64Ptr *uint64 `env:"UINT64"` Uint64s []uint64 `env:"UINT64S"` Uint64Ptrs []*uint64 `env:"UINT64S"` Float32 float32 `env:"FLOAT32"` Float32Ptr *float32 `env:"FLOAT32"` Float32s []float32 `env:"FLOAT32S"` Float32Ptrs []*float32 `env:"FLOAT32S"` Float64 float64 `env:"FLOAT64"` Float64Ptr *float64 `env:"FLOAT64"` Float64s []float64 `env:"FLOAT64S"` Float64Ptrs []*float64 `env:"FLOAT64S"` Duration time.Duration `env:"DURATION"` Durations []time.Duration `env:"DURATIONS"` DurationPtr *time.Duration `env:"DURATION"` DurationPtrs []*time.Duration `env:"DURATIONS"` Location time.Location `env:"LOCATION"` Locations []time.Location `env:"LOCATIONS"` LocationPtr *time.Location `env:"LOCATION"` LocationPtrs []*time.Location `env:"LOCATIONS"` Unmarshaler unmarshaler `env:"UNMARSHALER"` UnmarshalerPtr *unmarshaler `env:"UNMARSHALER"` Unmarshalers []unmarshaler `env:"UNMARSHALERS"` UnmarshalerPtrs []*unmarshaler `env:"UNMARSHALERS"` URL url.URL `env:"URL"` URLPtr *url.URL `env:"URL"` URLs []url.URL `env:"URLS"` URLPtrs []*url.URL `env:"URLS"` StringWithDefault string `env:"DATABASE_URL" envDefault:"postgres://localhost:5432/db"` CustomSeparator []string `env:"SEPSTRINGS" envSeparator:":"` NonDefined struct { String string `env:"NONDEFINED_STR"` } NestedNonDefined struct { NonDefined struct { String string `env:"STR"` } `envPrefix:"NONDEFINED_"` } `envPrefix:"PRF_"` NotAnEnv string unexported string `env:"FOO"` } type ParentStruct struct { InnerStruct *InnerStruct `env:",init"` NilInnerStruct *InnerStruct unexported *InnerStruct Ignored *http.Client } type InnerStruct struct { Inner string `env:"innervar"` Number uint `env:"innernum"` } type ForNestedStruct struct { NestedStruct } type NestedStruct struct { NestedVar string `env:"nestedvar"` } func TestIssue245(t *testing.T) { t.Setenv("NAME_NOT_SET", "") type user struct { Name string `env:"NAME_NOT_SET" envDefault:"abcd"` } cfg := user{} isNoErr(t, Parse(&cfg)) isEqual(t, cfg.Name, "abcd") } func TestParsesEnv(t *testing.T) { tos := func(v interface{}) string { return fmt.Sprintf("%v", v) } toss := func(v ...interface{}) string { ss := []string{} for _, s := range v { ss = append(ss, tos(s)) } return strings.Join(ss, ",") } str1 := "str1" str2 := "str2" t.Setenv("STRING", str1) t.Setenv("STRINGS", toss(str1, str2)) bool1 := true bool2 := false t.Setenv("BOOL", tos(bool1)) t.Setenv("BOOLS", toss(bool1, bool2)) int1 := -1 int2 := 2 t.Setenv("INT", tos(int1)) t.Setenv("INTS", toss(int1, int2)) var int81 int8 = -2 var int82 int8 = 5 t.Setenv("INT8", tos(int81)) t.Setenv("INT8S", toss(int81, int82)) var int161 int16 = -24 var int162 int16 = 15 t.Setenv("INT16", tos(int161)) t.Setenv("INT16S", toss(int161, int162)) var int321 int32 = -14 var int322 int32 = 154 t.Setenv("INT32", tos(int321)) t.Setenv("INT32S", toss(int321, int322)) var int641 int64 = -12 var int642 int64 = 150 t.Setenv("INT64", tos(int641)) t.Setenv("INT64S", toss(int641, int642)) var uint1 uint = 1 var uint2 uint = 2 t.Setenv("UINT", tos(uint1)) t.Setenv("UINTS", toss(uint1, uint2)) var uint81 uint8 = 15 var uint82 uint8 = 51 t.Setenv("UINT8", tos(uint81)) t.Setenv("UINT8S", toss(uint81, uint82)) var uint161 uint16 = 532 var uint162 uint16 = 123 t.Setenv("UINT16", tos(uint161)) t.Setenv("UINT16S", toss(uint161, uint162)) var uint321 uint32 = 93 var uint322 uint32 = 14 t.Setenv("UINT32", tos(uint321)) t.Setenv("UINT32S", toss(uint321, uint322)) var uint641 uint64 = 5 var uint642 uint64 = 43 t.Setenv("UINT64", tos(uint641)) t.Setenv("UINT64S", toss(uint641, uint642)) var float321 float32 = 9.3 var float322 float32 = 1.1 t.Setenv("FLOAT32", tos(float321)) t.Setenv("FLOAT32S", toss(float321, float322)) float641 := 1.53 float642 := 0.5 t.Setenv("FLOAT64", tos(float641)) t.Setenv("FLOAT64S", toss(float641, float642)) duration1 := time.Second duration2 := time.Second * 4 t.Setenv("DURATION", tos(duration1)) t.Setenv("DURATIONS", toss(duration1, duration2)) location1 := time.UTC location2, errLoadLocation := time.LoadLocation("Europe/Berlin") isNoErr(t, errLoadLocation) t.Setenv("LOCATION", tos(location1)) t.Setenv("LOCATIONS", toss(location1, location2)) unmarshaler1 := unmarshaler{time.Minute} unmarshaler2 := unmarshaler{time.Millisecond * 1232} t.Setenv("UNMARSHALER", tos(unmarshaler1.Duration)) t.Setenv("UNMARSHALERS", toss(unmarshaler1.Duration, unmarshaler2.Duration)) url1 := "https://goreleaser.com" url2 := "https://caarlos0.dev" t.Setenv("URL", tos(url1)) t.Setenv("URLS", toss(url1, url2)) t.Setenv("SEPSTRINGS", str1+":"+str2) nonDefinedStr := "nonDefinedStr" t.Setenv("NONDEFINED_STR", nonDefinedStr) t.Setenv("PRF_NONDEFINED_STR", nonDefinedStr) t.Setenv("FOO", str1) cfg := Config{} isNoErr(t, Parse(&cfg)) isEqual(t, str1, cfg.String) isEqual(t, &str1, cfg.StringPtr) isEqual(t, str1, cfg.Strings[0]) isEqual(t, str2, cfg.Strings[1]) isEqual(t, &str1, cfg.StringPtrs[0]) isEqual(t, &str2, cfg.StringPtrs[1]) isEqual(t, bool1, cfg.Bool) isEqual(t, &bool1, cfg.BoolPtr) isEqual(t, bool1, cfg.Bools[0]) isEqual(t, bool2, cfg.Bools[1]) isEqual(t, &bool1, cfg.BoolPtrs[0]) isEqual(t, &bool2, cfg.BoolPtrs[1]) isEqual(t, int1, cfg.Int) isEqual(t, &int1, cfg.IntPtr) isEqual(t, int1, cfg.Ints[0]) isEqual(t, int2, cfg.Ints[1]) isEqual(t, &int1, cfg.IntPtrs[0]) isEqual(t, &int2, cfg.IntPtrs[1]) isEqual(t, int81, cfg.Int8) isEqual(t, &int81, cfg.Int8Ptr) isEqual(t, int81, cfg.Int8s[0]) isEqual(t, int82, cfg.Int8s[1]) isEqual(t, &int81, cfg.Int8Ptrs[0]) isEqual(t, &int82, cfg.Int8Ptrs[1]) isEqual(t, int161, cfg.Int16) isEqual(t, &int161, cfg.Int16Ptr) isEqual(t, int161, cfg.Int16s[0]) isEqual(t, int162, cfg.Int16s[1]) isEqual(t, &int161, cfg.Int16Ptrs[0]) isEqual(t, &int162, cfg.Int16Ptrs[1]) isEqual(t, int321, cfg.Int32) isEqual(t, &int321, cfg.Int32Ptr) isEqual(t, int321, cfg.Int32s[0]) isEqual(t, int322, cfg.Int32s[1]) isEqual(t, &int321, cfg.Int32Ptrs[0]) isEqual(t, &int322, cfg.Int32Ptrs[1]) isEqual(t, int641, cfg.Int64) isEqual(t, &int641, cfg.Int64Ptr) isEqual(t, int641, cfg.Int64s[0]) isEqual(t, int642, cfg.Int64s[1]) isEqual(t, &int641, cfg.Int64Ptrs[0]) isEqual(t, &int642, cfg.Int64Ptrs[1]) isEqual(t, uint1, cfg.Uint) isEqual(t, &uint1, cfg.UintPtr) isEqual(t, uint1, cfg.Uints[0]) isEqual(t, uint2, cfg.Uints[1]) isEqual(t, &uint1, cfg.UintPtrs[0]) isEqual(t, &uint2, cfg.UintPtrs[1]) isEqual(t, uint81, cfg.Uint8) isEqual(t, &uint81, cfg.Uint8Ptr) isEqual(t, uint81, cfg.Uint8s[0]) isEqual(t, uint82, cfg.Uint8s[1]) isEqual(t, &uint81, cfg.Uint8Ptrs[0]) isEqual(t, &uint82, cfg.Uint8Ptrs[1]) isEqual(t, uint161, cfg.Uint16) isEqual(t, &uint161, cfg.Uint16Ptr) isEqual(t, uint161, cfg.Uint16s[0]) isEqual(t, uint162, cfg.Uint16s[1]) isEqual(t, &uint161, cfg.Uint16Ptrs[0]) isEqual(t, &uint162, cfg.Uint16Ptrs[1]) isEqual(t, uint321, cfg.Uint32) isEqual(t, &uint321, cfg.Uint32Ptr) isEqual(t, uint321, cfg.Uint32s[0]) isEqual(t, uint322, cfg.Uint32s[1]) isEqual(t, &uint321, cfg.Uint32Ptrs[0]) isEqual(t, &uint322, cfg.Uint32Ptrs[1]) isEqual(t, uint641, cfg.Uint64) isEqual(t, &uint641, cfg.Uint64Ptr) isEqual(t, uint641, cfg.Uint64s[0]) isEqual(t, uint642, cfg.Uint64s[1]) isEqual(t, &uint641, cfg.Uint64Ptrs[0]) isEqual(t, &uint642, cfg.Uint64Ptrs[1]) isEqual(t, float321, cfg.Float32) isEqual(t, &float321, cfg.Float32Ptr) isEqual(t, float321, cfg.Float32s[0]) isEqual(t, float322, cfg.Float32s[1]) isEqual(t, &float321, cfg.Float32Ptrs[0]) isEqual(t, float641, cfg.Float64) isEqual(t, &float641, cfg.Float64Ptr) isEqual(t, float641, cfg.Float64s[0]) isEqual(t, float642, cfg.Float64s[1]) isEqual(t, &float641, cfg.Float64Ptrs[0]) isEqual(t, &float642, cfg.Float64Ptrs[1]) isEqual(t, duration1, cfg.Duration) isEqual(t, &duration1, cfg.DurationPtr) isEqual(t, duration1, cfg.Durations[0]) isEqual(t, duration2, cfg.Durations[1]) isEqual(t, &duration1, cfg.DurationPtrs[0]) isEqual(t, &duration2, cfg.DurationPtrs[1]) isEqual(t, *location1, cfg.Location) isEqual(t, location1, cfg.LocationPtr) isEqual(t, *location1, cfg.Locations[0]) isEqual(t, *location2, cfg.Locations[1]) isEqual(t, location1, cfg.LocationPtrs[0]) isEqual(t, location2, cfg.LocationPtrs[1]) isEqual(t, unmarshaler1, cfg.Unmarshaler) isEqual(t, &unmarshaler1, cfg.UnmarshalerPtr) isEqual(t, unmarshaler1, cfg.Unmarshalers[0]) isEqual(t, unmarshaler2, cfg.Unmarshalers[1]) isEqual(t, &unmarshaler1, cfg.UnmarshalerPtrs[0]) isEqual(t, &unmarshaler2, cfg.UnmarshalerPtrs[1]) isEqual(t, url1, cfg.URL.String()) isEqual(t, url1, cfg.URLPtr.String()) isEqual(t, url1, cfg.URLs[0].String()) isEqual(t, url2, cfg.URLs[1].String()) isEqual(t, url1, cfg.URLPtrs[0].String()) isEqual(t, url2, cfg.URLPtrs[1].String()) isEqual(t, "postgres://localhost:5432/db", cfg.StringWithDefault) isEqual(t, nonDefinedStr, cfg.NonDefined.String) isEqual(t, nonDefinedStr, cfg.NestedNonDefined.NonDefined.String) isEqual(t, str1, cfg.CustomSeparator[0]) isEqual(t, str2, cfg.CustomSeparator[1]) isEqual(t, cfg.NotAnEnv, "") isEqual(t, cfg.unexported, "") } func TestParsesEnv_Map(t *testing.T) { type config struct { MapStringString map[string]string `env:"MAP_STRING_STRING" envSeparator:","` MapStringInt64 map[string]int64 `env:"MAP_STRING_INT64"` MapStringBool map[string]bool `env:"MAP_STRING_BOOL" envSeparator:";"` CustomSeparatorMapStringString map[string]string `env:"CUSTOM_SEPARATOR_MAP_STRING_STRING" envSeparator:"," envKeyValSeparator:"|"` } mss := map[string]string{ "k1": "v1", "k2": "v2", } t.Setenv("MAP_STRING_STRING", "k1:v1,k2:v2") msi := map[string]int64{ "k1": 1, "k2": 2, } t.Setenv("MAP_STRING_INT64", "k1:1,k2:2") msb := map[string]bool{ "k1": true, "k2": false, } t.Setenv("MAP_STRING_BOOL", "k1:true;k2:false") withCustomSeparator := map[string]string{ "k1": "v1", "k2": "v2", } t.Setenv("CUSTOM_SEPARATOR_MAP_STRING_STRING", "k1|v1,k2|v2") var cfg config isNoErr(t, Parse(&cfg)) isEqual(t, mss, cfg.MapStringString) isEqual(t, msi, cfg.MapStringInt64) isEqual(t, msb, cfg.MapStringBool) isEqual(t, withCustomSeparator, cfg.CustomSeparatorMapStringString) } func TestParsesEnvInvalidMap(t *testing.T) { type config struct { MapStringString map[string]string `env:"MAP_STRING_STRING" envSeparator:","` } t.Setenv("MAP_STRING_STRING", "k1,k2:v2") var cfg config err := Parse(&cfg) isTrue(t, errors.Is(err, ParseError{})) } func TestParseCustomMapType(t *testing.T) { type custommap map[string]bool type config struct { SecretKey custommap `env:"SECRET_KEY"` } t.Setenv("SECRET_KEY", "somesecretkey:1") var cfg config isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(custommap{}): func(_ string) (interface{}, error) { return custommap(map[string]bool{}), nil }, }})) } func TestParseMapCustomKeyType(t *testing.T) { type CustomKey string type config struct { SecretKey map[CustomKey]bool `env:"SECRET"` } t.Setenv("SECRET", "somesecretkey:1") var cfg config isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(CustomKey("")): func(value string) (interface{}, error) { return CustomKey(value), nil }, }})) } func TestParseMapCustomKeyNoParser(t *testing.T) { type CustomKey struct{} type config struct { SecretKey map[CustomKey]bool `env:"SECRET"` } t.Setenv("SECRET", "somesecretkey:1") var cfg config err := Parse(&cfg) isTrue(t, errors.Is(err, NoParserError{})) } func TestParseMapCustomValueNoParser(t *testing.T) { type Customval struct{} type config struct { SecretKey map[string]Customval `env:"SECRET"` } t.Setenv("SECRET", "somesecretkey:1") var cfg config err := Parse(&cfg) isTrue(t, errors.Is(err, NoParserError{})) } func TestParseMapCustomKeyTypeError(t *testing.T) { type CustomKey string type config struct { SecretKey map[CustomKey]bool `env:"SECRET"` } t.Setenv("SECRET", "somesecretkey:1") var cfg config err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(CustomKey("")): func(_ string) (interface{}, error) { return nil, fmt.Errorf("custom error") }, }}) isTrue(t, errors.Is(err, ParseError{})) } func TestParseMapCustomValueTypeError(t *testing.T) { type Customval string type config struct { SecretKey map[string]Customval `env:"SECRET"` } t.Setenv("SECRET", "somesecretkey:1") var cfg config err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(Customval("")): func(_ string) (interface{}, error) { return nil, fmt.Errorf("custom error") }, }}) isTrue(t, errors.Is(err, ParseError{})) } func TestSetenvAndTagOptsChain(t *testing.T) { type config struct { Key1 string `mytag:"KEY1,required"` Key2 int `mytag:"KEY2,required"` } envs := map[string]string{ "KEY1": "VALUE1", "KEY2": "3", } cfg := config{} isNoErr(t, ParseWithOptions(&cfg, Options{TagName: "mytag", Environment: envs})) isEqual(t, "VALUE1", cfg.Key1) isEqual(t, 3, cfg.Key2) } func TestJSONTag(t *testing.T) { type config struct { Key1 string `json:"KEY1"` Key2 int `json:"KEY2"` } t.Setenv("KEY1", "VALUE7") t.Setenv("KEY2", "5") cfg := config{} isNoErr(t, ParseWithOptions(&cfg, Options{TagName: "json"})) isEqual(t, "VALUE7", cfg.Key1) isEqual(t, 5, cfg.Key2) } func TestParsesEnvInner(t *testing.T) { t.Setenv("innervar", "someinnervalue") t.Setenv("innernum", "8") cfg := ParentStruct{ InnerStruct: &InnerStruct{}, unexported: &InnerStruct{}, } isNoErr(t, Parse(&cfg)) isEqual(t, "someinnervalue", cfg.InnerStruct.Inner) isEqual(t, uint(8), cfg.InnerStruct.Number) } func TestParsesEnvInner_WhenInnerStructPointerIsNil(t *testing.T) { t.Setenv("innervar", "someinnervalue") t.Setenv("innernum", "8") cfg := ParentStruct{} isNoErr(t, Parse(&cfg)) isEqual(t, "someinnervalue", cfg.InnerStruct.Inner) isEqual(t, uint(8), cfg.InnerStruct.Number) } func TestParsesEnvInnerFails(t *testing.T) { type config struct { Foo struct { Number int `env:"NUMBER"` } } t.Setenv("NUMBER", "not-a-number") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestParsesEnvInnerFailsMultipleErrors(t *testing.T) { type config struct { Foo struct { Name string `env:"NAME,required"` Number int `env:"NUMBER"` Bar struct { Age int `env:"AGE,required"` } } } t.Setenv("NUMBER", "not-a-number") err := Parse(&config{}) isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax; required environment variable "AGE" is not set`) isTrue(t, errors.Is(err, ParseError{})) isTrue(t, errors.Is(err, VarIsNotSetError{})) isTrue(t, errors.Is(err, VarIsNotSetError{})) } func TestParsesEnvInnerNil(t *testing.T) { t.Setenv("innervar", "someinnervalue") cfg := ParentStruct{} isNoErr(t, Parse(&cfg)) } func TestParsesEnvInnerInvalid(t *testing.T) { t.Setenv("innernum", "-547") cfg := ParentStruct{ InnerStruct: &InnerStruct{}, } err := Parse(&cfg) isErrorWithMessage(t, err, `env: parse error on field "Number" of type "uint": strconv.ParseUint: parsing "-547": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestParsesEnvNested(t *testing.T) { t.Setenv("nestedvar", "somenestedvalue") var cfg ForNestedStruct isNoErr(t, Parse(&cfg)) isEqual(t, "somenestedvalue", cfg.NestedVar) } func TestEmptyVars(t *testing.T) { cfg := Config{} isNoErr(t, Parse(&cfg)) isEqual(t, "", cfg.String) isEqual(t, false, cfg.Bool) isEqual(t, 0, cfg.Int) isEqual(t, uint(0), cfg.Uint) isEqual(t, uint64(0), cfg.Uint64) isEqual(t, int64(0), cfg.Int64) isEqual(t, 0, len(cfg.Strings)) isEqual(t, 0, len(cfg.CustomSeparator)) isEqual(t, 0, len(cfg.Ints)) isEqual(t, 0, len(cfg.Bools)) } func TestPassAnInvalidPtr(t *testing.T) { var thisShouldBreak int err := Parse(&thisShouldBreak) isErrorWithMessage(t, err, "env: expected a pointer to a Struct") isTrue(t, errors.Is(err, NotStructPtrError{})) } func TestPassReference(t *testing.T) { cfg := Config{} err := Parse(cfg) isErrorWithMessage(t, err, "env: expected a pointer to a Struct") isTrue(t, errors.Is(err, NotStructPtrError{})) } func TestInvalidBool(t *testing.T) { t.Setenv("BOOL", "should-be-a-bool") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Bool" of type "bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax; parse error on field "BoolPtr" of type "*bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidInt(t *testing.T) { t.Setenv("INT", "should-be-an-int") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Int" of type "int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax; parse error on field "IntPtr" of type "*int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidUint(t *testing.T) { t.Setenv("UINT", "-44") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Uint" of type "uint": strconv.ParseUint: parsing "-44": invalid syntax; parse error on field "UintPtr" of type "*uint": strconv.ParseUint: parsing "-44": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidFloat32(t *testing.T) { t.Setenv("FLOAT32", "AAA") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Float32" of type "float32": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float32Ptr" of type "*float32": strconv.ParseFloat: parsing "AAA": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidFloat64(t *testing.T) { t.Setenv("FLOAT64", "AAA") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Float64" of type "float64": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float64Ptr" of type "*float64": strconv.ParseFloat: parsing "AAA": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidUint64(t *testing.T) { t.Setenv("UINT64", "AAA") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Uint64" of type "uint64": strconv.ParseUint: parsing "AAA": invalid syntax; parse error on field "Uint64Ptr" of type "*uint64": strconv.ParseUint: parsing "AAA": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidInt64(t *testing.T) { t.Setenv("INT64", "AAA") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Int64" of type "int64": strconv.ParseInt: parsing "AAA": invalid syntax; parse error on field "Int64Ptr" of type "*int64": strconv.ParseInt: parsing "AAA": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidInt64Slice(t *testing.T) { t.Setenv("BADINTS", "A,2,3") type config struct { BadFloats []int64 `env:"BADINTS"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]int64": strconv.ParseInt: parsing "A": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidUInt64Slice(t *testing.T) { t.Setenv("BADINTS", "A,2,3") type config struct { BadFloats []uint64 `env:"BADINTS"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]uint64": strconv.ParseUint: parsing "A": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidFloat32Slice(t *testing.T) { t.Setenv("BADFLOATS", "A,2.0,3.0") type config struct { BadFloats []float32 `env:"BADFLOATS"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float32": strconv.ParseFloat: parsing "A": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidFloat64Slice(t *testing.T) { t.Setenv("BADFLOATS", "A,2.0,3.0") type config struct { BadFloats []float64 `env:"BADFLOATS"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float64": strconv.ParseFloat: parsing "A": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidBoolsSlice(t *testing.T) { t.Setenv("BADBOOLS", "t,f,TRUE,faaaalse") type config struct { BadBools []bool `env:"BADBOOLS"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "BadBools" of type "[]bool": strconv.ParseBool: parsing "faaaalse": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidDuration(t *testing.T) { t.Setenv("DURATION", "should-be-a-valid-duration") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Duration" of type "time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"; parse error on field "DurationPtr" of type "*time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidDurations(t *testing.T) { t.Setenv("DURATIONS", "1s,contains-an-invalid-duration,3s") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Durations" of type "[]time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"; parse error on field "DurationPtrs" of type "[]*time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidLocation(t *testing.T) { t.Setenv("LOCATION", "should-be-a-valid-location") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Location" of type "time.Location": unable to parse location: unknown time zone should-be-a-valid-location; parse error on field "LocationPtr" of type "*time.Location": unable to parse location: unknown time zone should-be-a-valid-location`) isTrue(t, errors.Is(err, ParseError{})) } func TestInvalidLocations(t *testing.T) { t.Setenv("LOCATIONS", "should-be-a-valid-location,UTC,Europe/Berlin") err := Parse(&Config{}) isErrorWithMessage(t, err, `env: parse error on field "Locations" of type "[]time.Location": unable to parse location: unknown time zone should-be-a-valid-location; parse error on field "LocationPtrs" of type "[]*time.Location": unable to parse location: unknown time zone should-be-a-valid-location`) isTrue(t, errors.Is(err, ParseError{})) } func TestParseStructWithoutEnvTag(t *testing.T) { cfg := Config{} isNoErr(t, Parse(&cfg)) isEqual(t, cfg.NotAnEnv, "") } func TestParseStructWithInvalidFieldKind(t *testing.T) { type config struct { WontWorkByte byte `env:"BLAH"` } t.Setenv("BLAH", "a") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "WontWorkByte" of type "uint8": strconv.ParseUint: parsing "a": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestUnsupportedSliceType(t *testing.T) { type config struct { WontWork []map[int]int `env:"WONTWORK"` } t.Setenv("WONTWORK", "1,2,3") err := Parse(&config{}) isErrorWithMessage(t, err, `env: no parser found for field "WontWork" of type "[]map[int]int"`) isTrue(t, errors.Is(err, NoParserError{})) } func TestBadSeparator(t *testing.T) { type config struct { WontWork []int `env:"WONTWORK" envSeparator:":"` } t.Setenv("WONTWORK", "1,2,3,4") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "WontWork" of type "[]int": strconv.ParseInt: parsing "1,2,3,4": invalid syntax`) isTrue(t, errors.Is(err, ParseError{})) } func TestNoErrorRequiredSet(t *testing.T) { type config struct { IsRequired string `env:"IS_REQUIRED,required"` } cfg := &config{} t.Setenv("IS_REQUIRED", "") isNoErr(t, Parse(cfg)) isEqual(t, "", cfg.IsRequired) } func TestHook(t *testing.T) { type config struct { Something string `env:"SOMETHING" envDefault:"important"` Another string `env:"ANOTHER"` Nope string Inner struct{} `envPrefix:"FOO_"` } cfg := &config{} t.Setenv("ANOTHER", "1") type onSetArgs struct { tag string key interface{} isDefault bool } var onSetCalled []onSetArgs isNoErr(t, ParseWithOptions(cfg, Options{ OnSet: func(tag string, value interface{}, isDefault bool) { onSetCalled = append(onSetCalled, onSetArgs{tag, value, isDefault}) }, })) isEqual(t, "important", cfg.Something) isEqual(t, "1", cfg.Another) isEqual(t, 2, len(onSetCalled)) isEqual(t, onSetArgs{"SOMETHING", "important", true}, onSetCalled[0]) isEqual(t, onSetArgs{"ANOTHER", "1", false}, onSetCalled[1]) } func TestErrorRequiredWithDefault(t *testing.T) { type config struct { IsRequired string `env:"IS_REQUIRED,required" envDefault:"important"` } cfg := &config{} t.Setenv("IS_REQUIRED", "") isNoErr(t, Parse(cfg)) isEqual(t, "important", cfg.IsRequired) } func TestErrorRequiredNotSet(t *testing.T) { type config struct { IsRequired string `env:"IS_REQUIRED,required"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: required environment variable "IS_REQUIRED" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) } func TestNoErrorNotEmptySet(t *testing.T) { t.Setenv("IS_REQUIRED", "1") type config struct { IsRequired string `env:"IS_REQUIRED,notEmpty"` } isNoErr(t, Parse(&config{})) } func TestNoErrorRequiredAndNotEmptySet(t *testing.T) { t.Setenv("IS_REQUIRED", "1") type config struct { IsRequired string `env:"IS_REQUIRED,required,notEmpty"` } isNoErr(t, Parse(&config{})) } func TestErrorNotEmptySet(t *testing.T) { t.Setenv("IS_REQUIRED", "") type config struct { IsRequired string `env:"IS_REQUIRED,notEmpty"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: environment variable "IS_REQUIRED" should not be empty`) isTrue(t, errors.Is(err, EmptyVarError{})) } func TestErrorRequiredAndNotEmptySet(t *testing.T) { t.Setenv("IS_REQUIRED", "") type config struct { IsRequired string `env:"IS_REQUIRED,notEmpty,required"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: environment variable "IS_REQUIRED" should not be empty`) isTrue(t, errors.Is(err, EmptyVarError{})) } func TestErrorRequiredNotSetWithDefault(t *testing.T) { type config struct { IsRequired string `env:"IS_REQUIRED,required" envDefault:"important"` } cfg := &config{} isNoErr(t, Parse(cfg)) isEqual(t, "important", cfg.IsRequired) } func TestParseExpandOption(t *testing.T) { type config struct { Host string `env:"HOST" envDefault:"localhost"` Port int `env:"PORT,expand" envDefault:"3000"` SecretKey string `env:"SECRET_KEY,expand"` ExpandKey string `env:"EXPAND_KEY"` CompoundKey string `env:"HOST_PORT,expand" envDefault:"${HOST}:${PORT}"` Default string `env:"DEFAULT,expand" envDefault:"def1"` } t.Setenv("HOST", "localhost") t.Setenv("PORT", "3000") t.Setenv("EXPAND_KEY", "qwerty12345") t.Setenv("SECRET_KEY", "${EXPAND_KEY}") cfg := config{} err := Parse(&cfg) isNoErr(t, err) isEqual(t, "localhost", cfg.Host) isEqual(t, 3000, cfg.Port) isEqual(t, "qwerty12345", cfg.SecretKey) isEqual(t, "qwerty12345", cfg.ExpandKey) isEqual(t, "localhost:3000", cfg.CompoundKey) isEqual(t, "def1", cfg.Default) } func TestParseExpandWithDefaultOption(t *testing.T) { type config struct { Host string `env:"HOST" envDefault:"localhost"` Port int `env:"PORT,expand" envDefault:"3000"` OtherPort int `env:"OTHER_PORT" envDefault:"4000"` CompoundDefault string `env:"HOST_PORT,expand" envDefault:"${HOST}:${PORT}"` SimpleDefault string `env:"DEFAULT,expand" envDefault:"def1"` MixedDefault string `env:"MIXED_DEFAULT,expand" envDefault:"$USER@${HOST}:${OTHER_PORT}"` OverrideDefault string `env:"OVERRIDE_DEFAULT,expand"` DefaultIsExpand string `env:"DEFAULT_IS_EXPAND,expand" envDefault:"$THIS_IS_EXPAND"` NoDefault string `env:"NO_DEFAULT,expand"` } t.Setenv("OTHER_PORT", "5000") t.Setenv("USER", "jhon") t.Setenv("THIS_IS_USED", "this is used instead") t.Setenv("OVERRIDE_DEFAULT", "msg: ${THIS_IS_USED}") t.Setenv("THIS_IS_EXPAND", "msg: ${THIS_IS_USED}") t.Setenv("NO_DEFAULT", "$PORT:$OTHER_PORT") cfg := config{} err := Parse(&cfg) isNoErr(t, err) isEqual(t, "localhost", cfg.Host) isEqual(t, 3000, cfg.Port) isEqual(t, 5000, cfg.OtherPort) isEqual(t, "localhost:3000", cfg.CompoundDefault) isEqual(t, "def1", cfg.SimpleDefault) isEqual(t, "jhon@localhost:5000", cfg.MixedDefault) isEqual(t, "msg: this is used instead", cfg.OverrideDefault) isEqual(t, "3000:5000", cfg.NoDefault) } func TestParseUnsetRequireOptions(t *testing.T) { type config struct { Password string `env:"PASSWORD,unset,required"` } cfg := config{} err := Parse(&cfg) isErrorWithMessage(t, err, `env: required environment variable "PASSWORD" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) t.Setenv("PASSWORD", "superSecret") isNoErr(t, Parse(&cfg)) isEqual(t, "superSecret", cfg.Password) unset, exists := os.LookupEnv("PASSWORD") isEqual(t, "", unset) isEqual(t, false, exists) } func TestCustomParser(t *testing.T) { type foo struct { name string } type bar struct { Name string `env:"OTHER_CUSTOM"` Foo *foo `env:"BLAH_CUSTOM"` } type config struct { Var foo `env:"VAR_CUSTOM"` Foo *foo `env:"BLAH_CUSTOM"` Other *bar } t.Setenv("VAR_CUSTOM", "test") t.Setenv("OTHER_CUSTOM", "test2") t.Setenv("BLAH_CUSTOM", "test3") runtest := func(t *testing.T) { t.Helper() cfg := &config{ Other: &bar{}, } err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(foo{}): func(v string) (interface{}, error) { return foo{name: v}, nil }, }}) isNoErr(t, err) isEqual(t, cfg.Var.name, "test") isEqual(t, cfg.Foo.name, "test3") isEqual(t, cfg.Other.Name, "test2") isEqual(t, cfg.Other.Foo.name, "test3") } for i := 0; i < 10; i++ { t.Run(fmt.Sprintf("%d", i), runtest) } } func TestIssue226(t *testing.T) { type config struct { Inner struct { Abc []byte `env:"ABC" envDefault:"asdasd"` Def []byte `env:"DEF" envDefault:"a"` } Hij []byte `env:"HIJ"` Lmn []byte `env:"LMN"` } t.Setenv("HIJ", "a") t.Setenv("LMN", "b") cfg := &config{} isNoErr(t, ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf([]byte{0}): func(v string) (interface{}, error) { if v == "a" { return []byte("nope"), nil } return []byte(v), nil }, }})) isEqual(t, cfg.Inner.Abc, []byte("asdasd")) isEqual(t, cfg.Inner.Def, []byte("nope")) isEqual(t, cfg.Hij, []byte("nope")) isEqual(t, cfg.Lmn, []byte("b")) } func TestParseWithOptionsNoPtr(t *testing.T) { type foo struct{} err := ParseWithOptions(foo{}, Options{}) isErrorWithMessage(t, err, "env: expected a pointer to a Struct") isTrue(t, errors.Is(err, NotStructPtrError{})) } func TestParseWithOptionsInvalidType(t *testing.T) { var c int err := ParseWithOptions(&c, Options{}) isErrorWithMessage(t, err, "env: expected a pointer to a Struct") isTrue(t, errors.Is(err, NotStructPtrError{})) } func TestCustomParserError(t *testing.T) { type foo struct { name string } customParserFunc := func(_ string) (interface{}, error) { return nil, errors.New("something broke") } t.Run("single", func(t *testing.T) { type config struct { Var foo `env:"VAR"` } t.Setenv("VAR", "single") cfg := &config{} err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(foo{}): customParserFunc, }}) isEqual(t, cfg.Var.name, "") isErrorWithMessage(t, err, `env: parse error on field "Var" of type "env.foo": something broke`) isTrue(t, errors.Is(err, ParseError{})) }) t.Run("slice", func(t *testing.T) { type config struct { Var []foo `env:"VAR2"` } t.Setenv("VAR2", "slice,slace") cfg := &config{} err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(foo{}): customParserFunc, }}) isEqual(t, cfg.Var, nil) isErrorWithMessage(t, err, `env: parse error on field "Var" of type "[]env.foo": something broke`) isTrue(t, errors.Is(err, ParseError{})) }) } func TestCustomParserBasicType(t *testing.T) { type ConstT int32 type config struct { Const ConstT `env:"CONST_"` } exp := ConstT(123) t.Setenv("CONST_", fmt.Sprintf("%d", exp)) customParserFunc := func(v string) (interface{}, error) { i, err := strconv.Atoi(v) if err != nil { return nil, err } r := ConstT(i) return r, nil } cfg := &config{} err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(ConstT(0)): customParserFunc, }}) isNoErr(t, err) isEqual(t, exp, cfg.Const) } func TestCustomParserUint64Alias(t *testing.T) { type T uint64 var one T = 1 type config struct { Val T `env:"" envDefault:"1x"` } parserCalled := false tParser := func(value string) (interface{}, error) { parserCalled = true trimmed := strings.TrimSuffix(value, "x") i, err := strconv.Atoi(trimmed) if err != nil { return nil, err } return T(i), nil } cfg := config{} err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(one): tParser, }}) isTrue(t, parserCalled) isNoErr(t, err) isEqual(t, T(1), cfg.Val) } func TestTypeCustomParserBasicInvalid(t *testing.T) { type ConstT int32 type config struct { Const ConstT `env:"CONST_"` } t.Setenv("CONST_", "foobar") customParserFunc := func(_ string) (interface{}, error) { return nil, errors.New("random error") } cfg := &config{} err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(ConstT(0)): customParserFunc, }}) isEqual(t, cfg.Const, ConstT(0)) isErrorWithMessage(t, err, `env: parse error on field "Const" of type "env.ConstT": random error`) isTrue(t, errors.Is(err, ParseError{})) } func TestCustomParserNotCalledForNonAlias(t *testing.T) { type T uint64 type U uint64 type config struct { Val uint64 `env:"" envDefault:"33"` Other U `env:"OTHER_NAME" envDefault:"44"` } tParserCalled := false tParser := func(_ string) (interface{}, error) { tParserCalled = true return T(99), nil } cfg := config{} err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(T(0)): tParser, }}) isFalse(t, tParserCalled) isNoErr(t, err) isEqual(t, uint64(33), cfg.Val) isEqual(t, U(44), cfg.Other) } func TestCustomParserBasicUnsupported(t *testing.T) { type ConstT struct { A int } type config struct { Const ConstT `env:"CONST_"` } t.Setenv("CONST_", "42") cfg := &config{} err := Parse(cfg) isEqual(t, cfg.Const, ConstT{0}) isErrorWithMessage(t, err, `env: no parser found for field "Const" of type "env.ConstT"`) isTrue(t, errors.Is(err, NoParserError{})) } func TestUnsupportedStructType(t *testing.T) { type config struct { Foo http.Client `env:"FOO"` } t.Setenv("FOO", "foo") err := Parse(&config{}) isErrorWithMessage(t, err, `env: no parser found for field "Foo" of type "http.Client"`) isTrue(t, errors.Is(err, NoParserError{})) } func TestEmptyOption(t *testing.T) { type config struct { Var string `env:"VAR,"` } cfg := &config{} t.Setenv("VAR", "") isNoErr(t, Parse(cfg)) isEqual(t, "", cfg.Var) } func TestErrorOptionNotRecognized(t *testing.T) { type config struct { Var string `env:"VAR,not_supported!"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: tag option "not_supported!" not supported`) isTrue(t, errors.Is(err, NoSupportedTagOptionError{})) } func TestTextUnmarshalerError(t *testing.T) { type config struct { Unmarshaler unmarshaler `env:"UNMARSHALER"` } t.Setenv("UNMARSHALER", "invalid") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "Unmarshaler" of type "env.unmarshaler": time: invalid duration "invalid"`) isTrue(t, errors.Is(err, ParseError{})) } func TestTextUnmarshalersError(t *testing.T) { type config struct { Unmarshalers []unmarshaler `env:"UNMARSHALERS"` } t.Setenv("UNMARSHALERS", "1s,invalid") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "Unmarshalers" of type "[]env.unmarshaler": time: invalid duration "invalid"`) isTrue(t, errors.Is(err, ParseError{})) } func TestParseURL(t *testing.T) { type config struct { ExampleURL url.URL `env:"EXAMPLE_URL" envDefault:"https://google.com"` } var cfg config isNoErr(t, Parse(&cfg)) isEqual(t, "https://google.com", cfg.ExampleURL.String()) } func TestParseInvalidURL(t *testing.T) { type config struct { ExampleURL url.URL `env:"EXAMPLE_URL_2"` } t.Setenv("EXAMPLE_URL_2", "nope://s s/") err := Parse(&config{}) isErrorWithMessage(t, err, `env: parse error on field "ExampleURL" of type "url.URL": unable to parse URL: parse "nope://s s/": invalid character " " in host name`) isTrue(t, errors.Is(err, ParseError{})) } func TestIgnoresUnexported(t *testing.T) { type unexportedConfig struct { home string `env:"HOME"` Home2 string `env:"HOME"` } cfg := unexportedConfig{} t.Setenv("HOME", "/tmp/fakehome") isNoErr(t, Parse(&cfg)) isEqual(t, cfg.home, "") isEqual(t, "/tmp/fakehome", cfg.Home2) } type LogLevel int8 func (l *LogLevel) UnmarshalText(text []byte) error { txt := string(text) switch txt { case "debug": *l = DebugLevel case "info": *l = InfoLevel default: return fmt.Errorf("unknown level: %q", txt) } return nil } const ( DebugLevel LogLevel = iota - 1 InfoLevel ) func TestPrecedenceUnmarshalText(t *testing.T) { t.Setenv("LOG_LEVEL", "debug") t.Setenv("LOG_LEVELS", "debug,info") type config struct { LogLevel LogLevel `env:"LOG_LEVEL"` LogLevels []LogLevel `env:"LOG_LEVELS"` } var cfg config isNoErr(t, Parse(&cfg)) isEqual(t, DebugLevel, cfg.LogLevel) isEqual(t, []LogLevel{DebugLevel, InfoLevel}, cfg.LogLevels) } func TestFile(t *testing.T) { type config struct { SecretKey string `env:"SECRET_KEY,file"` } dir := t.TempDir() file := filepath.Join(dir, "sec_key") isNoErr(t, os.WriteFile(file, []byte("secret"), 0o660)) t.Setenv("SECRET_KEY", file) cfg := config{} isNoErr(t, Parse(&cfg)) isEqual(t, "secret", cfg.SecretKey) } func TestFileNoParam(t *testing.T) { type config struct { SecretKey string `env:"SECRET_KEY,file"` } cfg := config{} isNoErr(t, Parse(&cfg)) } func TestFileNoParamRequired(t *testing.T) { type config struct { SecretKey string `env:"SECRET_KEY,file,required"` } err := Parse(&config{}) isErrorWithMessage(t, err, `env: required environment variable "SECRET_KEY" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) } func TestFileBadFile(t *testing.T) { type config struct { SecretKey string `env:"SECRET_KEY,file"` } filename := "not-a-real-file" t.Setenv("SECRET_KEY", filename) oserr := "no such file or directory" if runtime.GOOS == "windows" { oserr = "The system cannot find the file specified." } err := Parse(&config{}) isErrorWithMessage(t, err, fmt.Sprintf("env: could not load content of file %q from variable SECRET_KEY: open %s: %s", filename, filename, oserr)) isTrue(t, errors.Is(err, LoadFileContentError{})) } func TestFileWithDefault(t *testing.T) { type config struct { SecretKey string `env:"SECRET_KEY,file,expand" envDefault:"${FILE}"` } dir := t.TempDir() file := filepath.Join(dir, "sec_key") isNoErr(t, os.WriteFile(file, []byte("secret"), 0o660)) t.Setenv("FILE", file) cfg := config{} isNoErr(t, Parse(&cfg)) isEqual(t, "secret", cfg.SecretKey) } func TestCustomSliceType(t *testing.T) { type customslice []byte type config struct { SecretKey customslice `env:"SECRET_KEY"` } t.Setenv("SECRET_KEY", "somesecretkey") var cfg config isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(customslice{}): func(value string) (interface{}, error) { return customslice(value), nil }, }})) } type MyTime time.Time func (t *MyTime) UnmarshalText(text []byte) error { tt, err := time.Parse("2006-01-02", string(text)) *t = MyTime(tt) return err } func TestCustomTimeParser(t *testing.T) { type config struct { SomeTime MyTime `env:"SOME_TIME"` } t.Setenv("SOME_TIME", "2021-05-06") var cfg config isNoErr(t, Parse(&cfg)) isEqual(t, 2021, time.Time(cfg.SomeTime).Year()) isEqual(t, time.Month(5), time.Time(cfg.SomeTime).Month()) isEqual(t, 6, time.Time(cfg.SomeTime).Day()) } func TestRequiredIfNoDefOption(t *testing.T) { type Tree struct { Fruit string `env:"FRUIT"` } type config struct { Name string `env:"NAME"` Genre string `env:"GENRE" envDefault:"Unknown"` Tree } var cfg config t.Run("missing", func(t *testing.T) { err := ParseWithOptions(&cfg, Options{RequiredIfNoDef: true}) isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; required environment variable "FRUIT" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) t.Setenv("NAME", "John") err = ParseWithOptions(&cfg, Options{RequiredIfNoDef: true}) isErrorWithMessage(t, err, `env: required environment variable "FRUIT" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) }) t.Run("all set", func(t *testing.T) { t.Setenv("NAME", "John") t.Setenv("FRUIT", "Apple") // should not trigger an error for the missing 'GENRE' env because it has a default value. isNoErr(t, ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})) }) } func TestRequiredIfNoDefNested(t *testing.T) { type Server struct { Host string `env:"HOST"` Port uint16 `env:"PORT"` } type API struct { Server Token string `env:"TOKEN"` } type config struct { API API `envPrefix:"SERVER_"` } t.Run("missing", func(t *testing.T) { var cfg config t.Setenv("SERVER_HOST", "https://google.com") t.Setenv("SERVER_TOKEN", "0xdeadfood") err := ParseWithOptions(&cfg, Options{RequiredIfNoDef: true}) isErrorWithMessage(t, err, `env: required environment variable "SERVER_PORT" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) }) t.Run("all set", func(t *testing.T) { var cfg config t.Setenv("SERVER_HOST", "https://google.com") t.Setenv("SERVER_PORT", "443") t.Setenv("SERVER_TOKEN", "0xdeadfood") isNoErr(t, ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})) }) } func TestPrefix(t *testing.T) { type Config struct { Home string `env:"HOME"` } type ComplexConfig struct { Foo Config `envPrefix:"FOO_"` Bar Config `envPrefix:"BAR_"` Clean Config } cfg := ComplexConfig{} isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_HOME": "/foo", "BAR_HOME": "/bar", "HOME": "/clean"}})) isEqual(t, "/foo", cfg.Foo.Home) isEqual(t, "/bar", cfg.Bar.Home) isEqual(t, "/clean", cfg.Clean.Home) } func TestPrefixPointers(t *testing.T) { type Test struct { Str string `env:"TEST"` } type ComplexConfig struct { Foo *Test `envPrefix:"FOO_"` Bar *Test `envPrefix:"BAR_"` Clean *Test } cfg := ComplexConfig{ Foo: &Test{}, Bar: &Test{}, Clean: &Test{}, } isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_TEST": "kek", "BAR_TEST": "lel", "TEST": "clean"}})) isEqual(t, "kek", cfg.Foo.Str) isEqual(t, "lel", cfg.Bar.Str) isEqual(t, "clean", cfg.Clean.Str) } func TestNestedPrefixPointer(t *testing.T) { type ComplexConfig struct { Foo struct { Str string `env:"STR"` } `envPrefix:"FOO_"` } cfg := ComplexConfig{} isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_STR": "foo_str"}})) isEqual(t, "foo_str", cfg.Foo.Str) type ComplexConfig2 struct { Foo struct { Bar struct { Str string `env:"STR"` } `envPrefix:"BAR_"` Bar2 string `env:"BAR2"` } `envPrefix:"FOO_"` } cfg2 := ComplexConfig2{} isNoErr(t, ParseWithOptions(&cfg2, Options{Environment: map[string]string{"FOO_BAR_STR": "kek", "FOO_BAR2": "lel"}})) isEqual(t, "lel", cfg2.Foo.Bar2) isEqual(t, "kek", cfg2.Foo.Bar.Str) } func TestComplePrefix(t *testing.T) { type Config struct { Home string `env:"HOME"` } type ComplexConfig struct { Foo Config `envPrefix:"FOO_"` Clean Config Bar Config `envPrefix:"BAR_"` Blah string `env:"BLAH"` } cfg := ComplexConfig{} isNoErr(t, ParseWithOptions(&cfg, Options{ Prefix: "T_", Environment: map[string]string{ "T_FOO_HOME": "/foo", "T_BAR_HOME": "/bar", "T_BLAH": "blahhh", "T_HOME": "/clean", }, })) isEqual(t, "/foo", cfg.Foo.Home) isEqual(t, "/bar", cfg.Bar.Home) isEqual(t, "/clean", cfg.Clean.Home) isEqual(t, "blahhh", cfg.Blah) } func TestNoEnvKey(t *testing.T) { type Config struct { Foo string FooBar string HTTPPort int bar string } var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{ UseFieldNameByDefault: true, Environment: map[string]string{ "FOO": "fooval", "FOO_BAR": "foobarval", "HTTP_PORT": "10", }, })) isEqual(t, "fooval", cfg.Foo) isEqual(t, "foobarval", cfg.FooBar) isEqual(t, 10, cfg.HTTPPort) isEqual(t, "", cfg.bar) } func TestToEnv(t *testing.T) { for in, out := range map[string]string{ "Foo": "FOO", "FooBar": "FOO_BAR", "FOOBar": "FOO_BAR", "Foo____Bar": "FOO_BAR", "fooBar": "FOO_BAR", "Foo_Bar": "FOO_BAR", "Foo__Bar": "FOO_BAR", "HTTPPort": "HTTP_PORT", "SSHPort": "SSH_PORT", "_SSH___Port_": "SSH_PORT", "_PortHTTP": "PORT_HTTP", } { t.Run(in, func(t *testing.T) { isEqual(t, out, toEnvName(in)) }) } } func TestErrorIs(t *testing.T) { err := newAggregateError(newParseError(reflect.StructField{}, nil)) t.Run("is", func(t *testing.T) { isTrue(t, errors.Is(err, ParseError{})) }) t.Run("is not", func(t *testing.T) { isFalse(t, errors.Is(err, NoParserError{})) }) } type FieldParamsConfig struct { Simple []string `env:"SIMPLE"` WithoutEnv string privateWithEnv string `env:"PRIVATE_WITH_ENV"` //nolint:unused WithDefault string `env:"WITH_DEFAULT" envDefault:"default"` Required string `env:"REQUIRED,required"` File string `env:"FILE,file"` Unset string `env:"UNSET,unset"` NotEmpty string `env:"NOT_EMPTY,notEmpty"` Expand string `env:"EXPAND,expand"` NestedConfig struct { Simple []string `env:"SIMPLE"` } `envPrefix:"NESTED_"` } func TestGetFieldParams(t *testing.T) { var config FieldParamsConfig params, err := GetFieldParams(&config) isNoErr(t, err) expectedParams := []FieldParams{ {OwnKey: "SIMPLE", Key: "SIMPLE"}, {OwnKey: "WITH_DEFAULT", Key: "WITH_DEFAULT", DefaultValue: "default", HasDefaultValue: true}, {OwnKey: "REQUIRED", Key: "REQUIRED", Required: true}, {OwnKey: "FILE", Key: "FILE", LoadFile: true}, {OwnKey: "UNSET", Key: "UNSET", Unset: true}, {OwnKey: "NOT_EMPTY", Key: "NOT_EMPTY", NotEmpty: true}, {OwnKey: "EXPAND", Key: "EXPAND", Expand: true}, {OwnKey: "SIMPLE", Key: "NESTED_SIMPLE"}, } isTrue(t, len(params) == len(expectedParams)) isTrue(t, areEqual(params, expectedParams)) } func TestGetFieldParamsWithPrefix(t *testing.T) { var config FieldParamsConfig params, err := GetFieldParamsWithOptions(&config, Options{Prefix: "FOO_"}) isNoErr(t, err) expectedParams := []FieldParams{ {OwnKey: "SIMPLE", Key: "FOO_SIMPLE"}, {OwnKey: "WITH_DEFAULT", Key: "FOO_WITH_DEFAULT", DefaultValue: "default", HasDefaultValue: true}, {OwnKey: "REQUIRED", Key: "FOO_REQUIRED", Required: true}, {OwnKey: "FILE", Key: "FOO_FILE", LoadFile: true}, {OwnKey: "UNSET", Key: "FOO_UNSET", Unset: true}, {OwnKey: "NOT_EMPTY", Key: "FOO_NOT_EMPTY", NotEmpty: true}, {OwnKey: "EXPAND", Key: "FOO_EXPAND", Expand: true}, {OwnKey: "SIMPLE", Key: "FOO_NESTED_SIMPLE"}, } isTrue(t, len(params) == len(expectedParams)) isTrue(t, areEqual(params, expectedParams)) } func TestGetFieldParamsError(t *testing.T) { var config FieldParamsConfig _, err := GetFieldParams(config) isErrorWithMessage(t, err, "env: expected a pointer to a Struct") isTrue(t, errors.Is(err, NotStructPtrError{})) } type Conf struct { Foo string `env:"FOO" envDefault:"bar"` } func TestParseAs(t *testing.T) { config, err := ParseAs[Conf]() isNoErr(t, err) isEqual(t, "bar", config.Foo) } func TestParseAsWithOptions(t *testing.T) { config, err := ParseAsWithOptions[Conf](Options{ Environment: map[string]string{ "FOO": "not bar", }, }) isNoErr(t, err) isEqual(t, "not bar", config.Foo) } type ConfRequired struct { Foo string `env:"FOO,required"` } func TestMust(t *testing.T) { t.Run("error", func(t *testing.T) { defer func() { err := recover() isErrorWithMessage(t, err.(error), `env: required environment variable "FOO" is not set`) }() conf := Must(ParseAs[ConfRequired]()) isEqual(t, "", conf.Foo) }) t.Run("success", func(t *testing.T) { t.Setenv("FOO", "bar") conf := Must(ParseAs[ConfRequired]()) isEqual(t, "bar", conf.Foo) }) } func isTrue(tb testing.TB, b bool) { tb.Helper() if !b { tb.Fatalf("expected true, got false") } } func isFalse(tb testing.TB, b bool) { tb.Helper() if b { tb.Fatalf("expected false, got true") } } func isErrorWithMessage(tb testing.TB, err error, msg string) { tb.Helper() if err == nil { tb.Fatalf("expected error, got nil") } if msg != err.Error() { tb.Fatalf("expected error message %q, got %q", msg, err.Error()) } } func isNoErr(tb testing.TB, err error) { tb.Helper() if err != nil { tb.Fatalf("unexpected error: %v", err) } } func isEqual(tb testing.TB, a, b interface{}) { tb.Helper() if areEqual(a, b) { return } tb.Fatalf("expected %#v (type %T) == %#v (type %T)", a, a, b, b) } // copied from https://github.com/matryer/is func areEqual(a, b interface{}) bool { if isNil(a) && isNil(b) { return true } if isNil(a) || isNil(b) { return false } if reflect.DeepEqual(a, b) { return true } aValue := reflect.ValueOf(a) bValue := reflect.ValueOf(b) return aValue == bValue } // copied from https://github.com/matryer/is func isNil(object interface{}) bool { if object == nil { return true } value := reflect.ValueOf(object) kind := value.Kind() if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() { return true } return false } func TestParseWithOptionsOverride(t *testing.T) { type config struct { Interval time.Duration `env:"INTERVAL"` } t.Setenv("INTERVAL", "1") var cfg config isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(time.Nanosecond): func(value string) (interface{}, error) { intervalI, err := strconv.Atoi(value) if err != nil { return nil, err } return time.Duration(intervalI), nil }, }})) } type Password []byte func (p *Password) UnmarshalText(text []byte) error { out, err := base64.StdEncoding.DecodeString(string(text)) if err != nil { return err } *p = out return nil } type UsernameAndPassword struct { Username string `env:"USER"` Password *Password `env:"PWD"` } func TestBase64Password(t *testing.T) { t.Setenv("USER", "admin") t.Setenv("PWD", base64.StdEncoding.EncodeToString([]byte("admin123"))) var c UsernameAndPassword isNoErr(t, Parse(&c)) isEqual(t, "admin", c.Username) isEqual(t, "admin123", string(*c.Password)) } func TestIssue304(t *testing.T) { t.Setenv("BACKEND_URL", "https://google.com") type Config struct { BackendURL string `envDefault:"localhost:8000"` } cfg, err := ParseAsWithOptions[Config](Options{ UseFieldNameByDefault: true, }) isNoErr(t, err) isEqual(t, "https://google.com", cfg.BackendURL) } func TestIssue234(t *testing.T) { type Test struct { Str string `env:"TEST"` } type ComplexConfig struct { Foo *Test `envPrefix:"FOO_" env:",init"` Bar Test `envPrefix:"BAR_"` Clean *Test } t.Setenv("FOO_TEST", "kek") t.Setenv("BAR_TEST", "lel") cfg := ComplexConfig{} isNoErr(t, Parse(&cfg)) isEqual(t, "kek", cfg.Foo.Str) isEqual(t, "lel", cfg.Bar.Str) } type Issue308 struct { Inner Issue308Map `env:"A_MAP"` } type Issue308Map map[string][]string func (rc *Issue308Map) UnmarshalText(b []byte) error { m := map[string][]string{} if err := json.Unmarshal(b, &m); err != nil { return err } *rc = Issue308Map(m) return nil } func TestIssue308(t *testing.T) { t.Setenv("A_MAP", `{"FOO":["BAR", "ZAZ"]}`) cfg := Issue308{} isNoErr(t, Parse(&cfg)) isEqual(t, Issue308Map{"FOO": []string{"BAR", "ZAZ"}}, cfg.Inner) } func TestIssue317(t *testing.T) { type TestConfig struct { U1 *url.URL `env:"U1"` U2 *url.URL `env:"U2,init"` } cases := []struct { desc string environment map[string]string expectedU1, expectedU2 *url.URL }{ { desc: "unset", environment: map[string]string{}, expectedU1: nil, expectedU2: &url.URL{}, }, { desc: "empty", environment: map[string]string{"U1": "", "U2": ""}, expectedU1: nil, expectedU2: &url.URL{}, }, { desc: "set", environment: map[string]string{"U1": "https://example.com/"}, expectedU1: &url.URL{Scheme: "https", Host: "example.com", Path: "/"}, expectedU2: &url.URL{}, }, } for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { cfg := TestConfig{} err := ParseWithOptions(&cfg, Options{Environment: tc.environment}) isNoErr(t, err) isEqual(t, tc.expectedU1, cfg.U1) isEqual(t, tc.expectedU2, cfg.U2) }) } } func TestIssue310(t *testing.T) { type TestConfig struct { URL *url.URL } cfg, err := ParseAs[TestConfig]() isNoErr(t, err) isEqual(t, nil, cfg.URL) } func TestMultipleTagOptions(t *testing.T) { type TestConfig struct { URL *url.URL `env:"URL,init,unset"` } t.Run("unset", func(t *testing.T) { cfg, err := ParseAs[TestConfig]() isNoErr(t, err) isEqual(t, &url.URL{}, cfg.URL) }) t.Run("empty", func(t *testing.T) { t.Setenv("URL", "") cfg, err := ParseAs[TestConfig]() isNoErr(t, err) isEqual(t, &url.URL{}, cfg.URL) }) t.Run("set", func(t *testing.T) { t.Setenv("URL", "https://github.com/caarlos0") cfg, err := ParseAs[TestConfig]() isNoErr(t, err) isEqual(t, &url.URL{Scheme: "https", Host: "github.com", Path: "/caarlos0"}, cfg.URL) isEqual(t, "", os.Getenv("URL")) }) } func TestIssue298(t *testing.T) { type Test struct { Str string `env:"STR"` Num int `env:"NUM"` } type ComplexConfig struct { Foo *[]Test `envPrefix:"FOO_"` Bar []Test `envPrefix:"BAR"` Baz []Test `env:",init"` } t.Setenv("FOO_0_STR", "f0t") t.Setenv("FOO_0_NUM", "101") t.Setenv("FOO_1_STR", "f1t") t.Setenv("FOO_1_NUM", "111") t.Setenv("BAR_0_STR", "b0t") // t.Setenv("BAR_0_NUM", "202") // Not overridden t.Setenv("BAR_1_STR", "b1t") t.Setenv("BAR_1_NUM", "212") t.Setenv("0_STR", "bt") t.Setenv("1_NUM", "10") sample := make([]Test, 1) sample[0].Str = "overridden text" sample[0].Num = 99999999 cfg := ComplexConfig{Bar: sample} isNoErr(t, Parse(&cfg)) isEqual(t, "f0t", (*cfg.Foo)[0].Str) isEqual(t, 101, (*cfg.Foo)[0].Num) isEqual(t, "f1t", (*cfg.Foo)[1].Str) isEqual(t, 111, (*cfg.Foo)[1].Num) isEqual(t, "b0t", cfg.Bar[0].Str) isEqual(t, 99999999, cfg.Bar[0].Num) isEqual(t, "b1t", cfg.Bar[1].Str) isEqual(t, 212, cfg.Bar[1].Num) isEqual(t, "bt", cfg.Baz[0].Str) isEqual(t, 0, cfg.Baz[0].Num) isEqual(t, "", cfg.Baz[1].Str) isEqual(t, 10, cfg.Baz[1].Num) } func TestIssue298ErrorNestedFieldRequiredNotSet(t *testing.T) { type Test struct { Str string `env:"STR,required"` Num int `env:"NUM"` } type ComplexConfig struct { Foo *[]Test `envPrefix:"FOO"` } t.Setenv("FOO_0_NUM", "101") cfg := ComplexConfig{} err := Parse(&cfg) isErrorWithMessage(t, err, `env: required environment variable "FOO_0_STR" is not set`) isTrue(t, errors.Is(err, VarIsNotSetError{})) } func TestIssue320(t *testing.T) { type Test struct { Str string `env:"STR"` Num int `env:"NUM"` } type ComplexConfig struct { Foo *[]Test `envPrefix:"FOO_"` Bar []Test `envPrefix:"BAR"` Baz []Test `env:",init"` } cfg := ComplexConfig{} isNoErr(t, Parse(&cfg)) isEqual(t, cfg.Foo, nil) isEqual(t, cfg.Bar, nil) isEqual(t, cfg.Baz, nil) } func TestParseWithOptionsRenamedDefault(t *testing.T) { type config struct { Str string `env:"STR" envDefault:"foo" myDefault:"bar"` } cfg := &config{} isNoErr(t, ParseWithOptions(cfg, Options{DefaultValueTagName: "myDefault"})) isEqual(t, "bar", cfg.Str) isNoErr(t, Parse(cfg)) isEqual(t, "foo", cfg.Str) } func TestParseWithOptionsRenamedPrefix(t *testing.T) { type Config struct { Str string `env:"STR"` } type ComplexConfig struct { Foo Config `envPrefix:"FOO_" myPrefix:"BAR_"` } t.Setenv("FOO_STR", "101") t.Setenv("BAR_STR", "202") t.Setenv("APP_BAR_STR", "303") cfg := &ComplexConfig{} isNoErr(t, ParseWithOptions(cfg, Options{PrefixTagName: "myPrefix"})) isEqual(t, "202", cfg.Foo.Str) isNoErr(t, ParseWithOptions(cfg, Options{PrefixTagName: "myPrefix", Prefix: "APP_"})) isEqual(t, "303", cfg.Foo.Str) isNoErr(t, Parse(cfg)) isEqual(t, "101", cfg.Foo.Str) } func TestFieldIgnored(t *testing.T) { type Test struct { Foo string `env:"FOO"` Bar string `env:"BAR,-"` } type ComplexConfig struct { Str string `env:"STR"` Foo Test `env:"FOO" envPrefix:"FOO_"` Bar Test `env:"-" envPrefix:"BAR_"` } t.Setenv("STR", "101") t.Setenv("FOO_FOO", "202") t.Setenv("FOO_BAR", "303") t.Setenv("BAR_FOO", "404") t.Setenv("BAR_BAR", "505") var cfg ComplexConfig isNoErr(t, Parse(&cfg)) isEqual(t, "101", cfg.Str) isEqual(t, "202", cfg.Foo.Foo) isEqual(t, "", cfg.Foo.Bar) isEqual(t, "", cfg.Bar.Foo) isEqual(t, "", cfg.Bar.Bar) } func TestNoEnvKeyIgnored(t *testing.T) { type Config struct { Foo string `env:"-"` FooBar string } t.Setenv("FOO", "101") t.Setenv("FOO_BAR", "202") var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{UseFieldNameByDefault: true})) isEqual(t, "", cfg.Foo) isEqual(t, "202", cfg.FooBar) } func TestIssue339(t *testing.T) { t.Run("Should parse with bool ptr set and env undefined", func(t *testing.T) { existingValue := true cfg := Config{ BoolPtr: &existingValue, } isNoErr(t, Parse(&cfg)) isEqual(t, &existingValue, cfg.BoolPtr) }) t.Run("Should parse with bool ptr set and env defined", func(t *testing.T) { existingValue := true cfg := Config{ BoolPtr: &existingValue, } newValue := false t.Setenv("BOOL", strconv.FormatBool(newValue)) isNoErr(t, Parse(&cfg)) isEqual(t, &newValue, cfg.BoolPtr) }) t.Run("Should parse with string ptr set and env undefined", func(t *testing.T) { existingValue := "one" cfg := Config{ StringPtr: &existingValue, } isNoErr(t, Parse(&cfg)) isEqual(t, &existingValue, cfg.StringPtr) }) t.Run("Should parse with string ptr set and env defined", func(t *testing.T) { existingValue := "one" cfg := Config{ StringPtr: &existingValue, } newValue := "two" t.Setenv("STRING", newValue) isNoErr(t, Parse(&cfg)) isEqual(t, &newValue, cfg.StringPtr) }) } func TestIssue350(t *testing.T) { t.Setenv("MAP", "url:https://foo.bar:2030") type Config struct { Map map[string]string `env:"MAP"` } var cfg Config isNoErr(t, Parse(&cfg)) isEqual(t, map[string]string{"url": "https://foo.bar:2030"}, cfg.Map) } func TestEnvBleed(t *testing.T) { type Config struct { Foo string `env:"FOO"` } t.Setenv("FOO", "101") t.Run("Default env with value", func(t *testing.T) { var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{})) isEqual(t, "101", cfg.Foo) }) t.Run("Empty env without value", func(t *testing.T) { var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{}})) isEqual(t, "", cfg.Foo) }) t.Run("Custom env with overwritten value", func(t *testing.T) { var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO": "202"}})) isEqual(t, "202", cfg.Foo) }) t.Run("Custom env without value", func(t *testing.T) { var cfg Config isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"BAR": "202"}})) isEqual(t, "", cfg.Foo) }) } env-11.3.1/env_tomap.go000066400000000000000000000003551473125436200147160ustar00rootroot00000000000000//go:build !windows package env import "strings" func toMap(env []string) map[string]string { r := map[string]string{} for _, e := range env { p := strings.SplitN(e, "=", 2) if len(p) == 2 { r[p[0]] = p[1] } } return r } env-11.3.1/env_tomap_test.go000066400000000000000000000005401473125436200157510ustar00rootroot00000000000000//go:build !windows package env import "testing" func TestUnix(t *testing.T) { envVars := []string{":=/test/unix", "PATH=:/test_val1:/test_val2", "VAR=REGULARVAR", "FOO=", "BAR"} result := ToMap(envVars) isEqual(t, map[string]string{ ":": "/test/unix", "PATH": ":/test_val1:/test_val2", "VAR": "REGULARVAR", "FOO": "", }, result) } env-11.3.1/env_tomap_windows.go000066400000000000000000000011621473125436200164650ustar00rootroot00000000000000//go:build windows package env import "strings" func toMap(env []string) map[string]string { r := map[string]string{} for _, e := range env { p := strings.SplitN(e, "=", 2) // On Windows, environment variables can start with '='. If so, Split at next character. // See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58 prefixEqualSign := false if len(e) > 0 && e[0] == '=' { e = e[1:] prefixEqualSign = true } p = strings.SplitN(e, "=", 2) if prefixEqualSign { p[0] = "=" + p[0] } if len(p) == 2 { r[p[0]] = p[1] } } return r } env-11.3.1/env_tomap_windows_test.go000066400000000000000000000010631473125436200175240ustar00rootroot00000000000000//go:build windows package env import "testing" // On Windows, environment variables can start with '='. // This test verifies this behavior without relying on a Windows environment. // See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58 func TestToMapWindows(t *testing.T) { envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR", "FOO=", "BAR"} result := ToMap(envVars) isEqual(t, map[string]string{ "=::": "::\\", "=C:": "C:\\test", "VAR": "REGULARVAR", "FOO": "", }, result) } env-11.3.1/error.go000066400000000000000000000101071473125436200140530ustar00rootroot00000000000000package env import ( "fmt" "reflect" "strings" ) // An aggregated error wrapper to combine gathered errors. // This allows either to display all errors or convert them individually // List of the available errors // ParseError // NotStructPtrError // NoParserError // NoSupportedTagOptionError // VarIsNotSetError // EmptyVarError // LoadFileContentError // ParseValueError type AggregateError struct { Errors []error } func newAggregateError(initErr error) error { return AggregateError{ []error{ initErr, }, } } func (e AggregateError) Error() string { var sb strings.Builder sb.WriteString("env:") for _, err := range e.Errors { sb.WriteString(fmt.Sprintf(" %v;", err.Error())) } return strings.TrimRight(sb.String(), ";") } // Unwrap implements std errors.Join go1.20 compatibility func (e AggregateError) Unwrap() []error { return e.Errors } // Is conforms with errors.Is. func (e AggregateError) Is(err error) bool { for _, ie := range e.Errors { if reflect.TypeOf(ie) == reflect.TypeOf(err) { return true } } return false } // The error occurs when it's impossible to convert the value for given type. type ParseError struct { Name string Type reflect.Type Err error } func newParseError(sf reflect.StructField, err error) error { return ParseError{sf.Name, sf.Type, err} } func (e ParseError) Error() string { return fmt.Sprintf("parse error on field %q of type %q: %v", e.Name, e.Type, e.Err) } // The error occurs when pass something that is not a pointer to a struct to Parse type NotStructPtrError struct{} func (e NotStructPtrError) Error() string { return "expected a pointer to a Struct" } // This error occurs when there is no parser provided for given type. type NoParserError struct { Name string Type reflect.Type } func newNoParserError(sf reflect.StructField) error { return NoParserError{sf.Name, sf.Type} } func (e NoParserError) Error() string { return fmt.Sprintf("no parser found for field %q of type %q", e.Name, e.Type) } // This error occurs when the given tag is not supported. // Built-in supported tags: "", "file", "required", "unset", "notEmpty", // "expand", "envDefault", and "envSeparator". type NoSupportedTagOptionError struct { Tag string } func newNoSupportedTagOptionError(tag string) error { return NoSupportedTagOptionError{tag} } func (e NoSupportedTagOptionError) Error() string { return fmt.Sprintf("tag option %q not supported", e.Tag) } // This error occurs when the required variable is not set. // // Deprecated: use VarIsNotSetError. type EnvVarIsNotSetError = VarIsNotSetError // This error occurs when the required variable is not set. type VarIsNotSetError struct { Key string } func newVarIsNotSetError(key string) error { return VarIsNotSetError{key} } func (e VarIsNotSetError) Error() string { return fmt.Sprintf(`required environment variable %q is not set`, e.Key) } // This error occurs when the variable which must be not empty is existing but has an empty value // // Deprecated: use EmptyVarError. type EmptyEnvVarError = EmptyVarError // This error occurs when the variable which must be not empty is existing but has an empty value type EmptyVarError struct { Key string } func newEmptyVarError(key string) error { return EmptyVarError{key} } func (e EmptyVarError) Error() string { return fmt.Sprintf("environment variable %q should not be empty", e.Key) } // This error occurs when it's impossible to load the value from the file. type LoadFileContentError struct { Filename string Key string Err error } func newLoadFileContentError(filename, key string, err error) error { return LoadFileContentError{filename, key, err} } func (e LoadFileContentError) Error() string { return fmt.Sprintf("could not load content of file %q from variable %s: %v", e.Filename, e.Key, e.Err) } // This error occurs when it's impossible to convert value using given parser. type ParseValueError struct { Msg string Err error } func newParseValueError(message string, err error) error { return ParseValueError{message, err} } func (e ParseValueError) Error() string { return fmt.Sprintf("%s: %v", e.Msg, e.Err) } env-11.3.1/example_test.go000066400000000000000000000256251473125436200154270ustar00rootroot00000000000000package env import ( "errors" "fmt" "os" "reflect" ) // Basic package usage example. func Example() { type Config struct { Foo string `env:"FOO"` } os.Setenv("FOO", "bar") // parse: var cfg1 Config _ = Parse(&cfg1) // parse with generics: cfg2, _ := ParseAs[Config]() fmt.Print(cfg1.Foo, cfg2.Foo) // Output: barbar } // Parse the environment into a struct. func ExampleParse() { type Config struct { Home string `env:"HOME"` } os.Setenv("HOME", "/tmp/fakehome") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Home:/tmp/fakehome} } // Parse the environment into a struct using generics. func ExampleParseAs() { type Config struct { Home string `env:"HOME"` } os.Setenv("HOME", "/tmp/fakehome") cfg, err := ParseAs[Config]() if err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Home:/tmp/fakehome} } func ExampleParse_required() { type Config struct { Nope string `env:"NOPE,required"` } var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: env: required environment variable "NOPE" is not set // {Nope:} } // While `required` demands the environment variable to be set, it doesn't check // its value. If you want to make sure the environment is set and not empty, you // need to use the `notEmpty` tag option instead (`env:"SOME_ENV,notEmpty"`). func ExampleParse_notEmpty() { type Config struct { Nope string `env:"NOPE,notEmpty"` } os.Setenv("NOPE", "") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: env: environment variable "NOPE" should not be empty // {Nope:} } // The `env` tag option `unset` (e.g., `env:"tagKey,unset"`) can be added // to ensure that some environment variable is unset after reading it. func ExampleParse_unset() { type Config struct { Secret string `env:"SECRET,unset"` } os.Setenv("SECRET", "1234") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v - %s", cfg, os.Getenv("SECRET")) // Output: {Secret:1234} - } // You can use `envSeparator` to define which character should be used to // separate array items in a string. // Similarly, you can use `envKeyValSeparator` to define which character should // be used to separate a key from a value in a map. // The defaults are `,` and `:`, respectively. func ExampleParse_separator() { type Config struct { Map map[string]string `env:"CUSTOM_MAP" envSeparator:"-" envKeyValSeparator:"|"` } os.Setenv("CUSTOM_MAP", "k1|v1-k2|v2") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Map:map[k1:v1 k2:v2]} } // If you set the `expand` option, environment variables (either in `${var}` or // `$var` format) in the string will be replaced according with the actual // value of the variable. For example: func ExampleParse_expand() { type Config struct { Expand1 string `env:"EXPAND_1,expand"` Expand2 string `env:"EXPAND_2,expand" envDefault:"ABC_${EXPAND_1}"` } os.Setenv("EXPANDING", "HI") os.Setenv("EXPAND_1", "HELLO_${EXPANDING}") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Expand1:HELLO_HI Expand2:ABC_HELLO_HI} } // You can automatically initialize `nil` pointers regardless of if a variable // is set for them or not. // This behavior can be enabled by using the `init` tag option. func ExampleParse_init() { type Inner struct { A string `env:"OLA" envDefault:"HI"` } type Config struct { NilInner *Inner InitInner *Inner `env:",init"` } var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Print(cfg.NilInner, cfg.InitInner) // Output: &{HI} } // You can define the default value for a field by either using the // `envDefault` tag, or when initializing the `struct`. // // Default values defined as `struct` tags will overwrite existing values // during `Parse`. func ExampleParse_setDefaults() { type Config struct { Foo string `env:"DEF_FOO"` Bar string `env:"DEF_BAR" envDefault:"bar"` } cfg := Config{ Foo: "foo", } if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Foo:foo Bar:bar} } // You might want to listen to value sets and, for example, log something or do // some other kind of logic. func ExampleParseWithOptions_onSet() { type config struct { Home string `env:"HOME,required"` Port int `env:"PORT" envDefault:"3000"` IsProduction bool `env:"PRODUCTION"` NoEnvTag bool Inner struct{} `envPrefix:"INNER_"` } os.Setenv("HOME", "/tmp/fakehome") var cfg config if err := ParseWithOptions(&cfg, Options{ OnSet: func(tag string, value interface{}, isDefault bool) { fmt.Printf("Set %s to %v (default? %v)\n", tag, value, isDefault) }, }); err != nil { fmt.Println("failed:", err) } fmt.Printf("%+v", cfg) // Output: Set HOME to /tmp/fakehome (default? false) // Set PORT to 3000 (default? true) // Set PRODUCTION to (default? false) // {Home:/tmp/fakehome Port:3000 IsProduction:false NoEnvTag:false Inner:{}} } // By default, env supports anything that implements the `TextUnmarshaler` // interface, which includes `time.Time`. // // The upside is that depending on the format you need, you don't need to change // anything. // // The downside is that if you do need time in another format, you'll need to // create your own type and implement `TextUnmarshaler`. func ExampleParse_customTimeFormat() { // type MyTime time.Time // // func (t *MyTime) UnmarshalText(text []byte) error { // tt, err := time.Parse("2006-01-02", string(text)) // *t = MyTime(tt) // return err // } type Config struct { SomeTime MyTime `env:"SOME_TIME"` } os.Setenv("SOME_TIME", "2021-05-06") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Print(cfg.SomeTime) // Output: {0 63755856000 } } // Parse using extra options. func ExampleParseWithOptions_customTypes() { type Thing struct { desc string } type Config struct { Thing Thing `env:"THING"` } os.Setenv("THING", "my thing") c := Config{} err := ParseWithOptions(&c, Options{ FuncMap: map[reflect.Type]ParserFunc{ reflect.TypeOf(Thing{}): func(v string) (interface{}, error) { return Thing{desc: v}, nil }, }, }) if err != nil { fmt.Println(err) } fmt.Print(c.Thing.desc) // Output: my thing } // Make all fields required by default. func ExampleParseWithOptions_allFieldsRequired() { type Config struct { Username string `env:"EX_USERNAME" envDefault:"admin"` Password string `env:"EX_PASSWORD"` } var cfg Config if err := ParseWithOptions(&cfg, Options{ RequiredIfNoDef: true, }); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", cfg) // Output: env: required environment variable "EX_PASSWORD" is not set // {Username:admin Password:} } // Set a custom environment. // By default, `os.Environ()` is used. func ExampleParseWithOptions_setEnv() { type Config struct { Username string `env:"EX_USERNAME" envDefault:"admin"` Password string `env:"EX_PASSWORD"` } var cfg Config if err := ParseWithOptions(&cfg, Options{ Environment: map[string]string{ "EX_USERNAME": "john", "EX_PASSWORD": "cena", }, }); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", cfg) // Output: {Username:john Password:cena} } // Handling slices of complex types. func ExampleParse_complexSlices() { type Test struct { Str string `env:"STR"` Num int `env:"NUM"` } type Config struct { Foo []Test `envPrefix:"FOO"` } os.Setenv("FOO_0_STR", "a") os.Setenv("FOO_0_NUM", "1") os.Setenv("FOO_1_STR", "b") os.Setenv("FOO_1_NUM", "2") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", cfg) // Output: {Foo:[{Str:a Num:1} {Str:b Num:2}]} } // Setting prefixes for inner types. func ExampleParse_prefix() { type Inner struct { Foo string `env:"FOO,required"` } type Config struct { A Inner `envPrefix:"A_"` B Inner `envPrefix:"B_"` } os.Setenv("A_FOO", "a") os.Setenv("B_FOO", "b") var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {A:{Foo:a} B:{Foo:b}} } // Setting prefixes for the entire config. func ExampleParseWithOptions_prefix() { type Config struct { Foo string `env:"FOO"` } os.Setenv("MY_APP_FOO", "a") var cfg Config if err := ParseWithOptions(&cfg, Options{ Prefix: "MY_APP_", }); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Foo:a} } // Use a different tag name than `env` and `envDefault`. func ExampleParseWithOptions_tagName() { type Config struct { Home string `json:"HOME"` Page string `json:"PAGE" def:"world"` } os.Setenv("HOME", "hello") var cfg Config if err := ParseWithOptions(&cfg, Options{ TagName: "json", DefaultValueTagName: "def", }); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Home:hello Page:world} } // If you don't want to set the `env` tag on every field, you can use the // `UseFieldNameByDefault` option. // // It will use the field name to define the environment variable name. // So, `Foo` becomes `FOO`, `FooBar` becomes `FOO_BAR`, and so on. func ExampleParseWithOptions_useFieldName() { type Config struct { Foo string } os.Setenv("FOO", "bar") var cfg Config if err := ParseWithOptions(&cfg, Options{ UseFieldNameByDefault: true, }); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Foo:bar} } // The `env` tag option `file` (e.g., `env:"tagKey,file"`) can be added in // order to indicate that the value of the variable shall be loaded from a // file. // // The path of that file is given by the environment variable associated with // it. func ExampleParse_fromFile() { f, _ := os.CreateTemp("", "") _, _ = f.WriteString("super secret") _ = f.Close() type Config struct { Secret string `env:"SECRET,file"` } os.Setenv("SECRET", f.Name()) var cfg Config if err := Parse(&cfg); err != nil { fmt.Println(err) } fmt.Printf("%+v", cfg) // Output: {Secret:super secret} } // TODO: envSeperator // func ExampleParse_errorHandling() { type Config struct { Username string `env:"EX_ERR_USERNAME" envDefault:"admin"` Password string `env:"EX_ERR_PASSWORD,notEmpty"` } var cfg Config if err := Parse(&cfg); err != nil { if errors.Is(err, EmptyVarError{}) { fmt.Println("oopsie") } aggErr := AggregateError{} if ok := errors.As(err, &aggErr); ok { for _, er := range aggErr.Errors { switch v := er.(type) { // Handle the error types you need: // ParseError // NotStructPtrError // NoParserError // NoSupportedTagOptionError // EnvVarIsNotSetError // EmptyEnvVarError // LoadFileContentError // ParseValueError case EmptyVarError: fmt.Println("daisy") default: fmt.Printf("Unknown error type %v", v) } } } } fmt.Printf("%+v", cfg) // Output: oopsie // daisy // {Username:admin Password:} } env-11.3.1/go.mod000066400000000000000000000007411473125436200135040ustar00rootroot00000000000000module github.com/caarlos0/env/v11 // v11.0.1 accidentally introduced a breaking change regarding the behavior of nil pointers. // You can now chose to auto-initialize them by setting the `init` tag option. retract v11.0.1 // v11.2.0 accidentally introduced a breaking change regarding the behavior of nil slices of complex types. retract v11.2.0 // v11.3.0 merges OS environment variables with environments set with Options instead of overriding them. retract v11.3.0 go 1.18 env-11.3.1/go.sum000066400000000000000000000000001473125436200135150ustar00rootroot00000000000000