diff --git a/go.mod b/go.mod index 6976a7b9..75d64140 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/briandowns/spinner v1.23.0 github.com/manifoldco/promptui v0.9.0 - github.com/obalunenko/getenv v1.8.0 + github.com/obalunenko/getenv v1.9.1 github.com/obalunenko/logger v0.6.0 github.com/obalunenko/version v1.1.0 github.com/savioxavier/termlink v1.2.1 diff --git a/go.sum b/go.sum index 40c2f971..c09ee0fb 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/obalunenko/getenv v1.8.0 h1:YReGdbhEnQg9vBjh4fKxEzHLJwG0EUilszshxRKfVKM= -github.com/obalunenko/getenv v1.8.0/go.mod h1:aWb6RzOdu+NT7sBd0VcmYtu2YQ3TO34C5+ExFXIaLqw= +github.com/obalunenko/getenv v1.9.1 h1:cOtpDHd4V2fiTEoqIqp0DNr+TTKHNIXixNc+Sg6GX8U= +github.com/obalunenko/getenv v1.9.1/go.mod h1:aWb6RzOdu+NT7sBd0VcmYtu2YQ3TO34C5+ExFXIaLqw= github.com/obalunenko/logger v0.6.0 h1:Sif4MYn6aGUA5pBTcKkMtJXTZNRZ52EiP4KLiFgMEkI= github.com/obalunenko/logger v0.6.0/go.mod h1:AOThqVHT+qlG5yMVarmVYPP37yytKxL89HfY8S56a1s= github.com/obalunenko/version v1.1.0 h1:yVua7OHnK3+MJpendeMmAlfzVmq7R1h8MO3Ufz7HEec= diff --git a/vendor/github.com/obalunenko/getenv/CODE_OF_CONDUCT.md b/vendor/github.com/obalunenko/getenv/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..944ca682 --- /dev/null +++ b/vendor/github.com/obalunenko/getenv/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +oleg.balunenko@gmail.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/vendor/github.com/obalunenko/getenv/README.md b/vendor/github.com/obalunenko/getenv/README.md index 95f950f5..aeda8046 100644 --- a/vendor/github.com/obalunenko/getenv/README.md +++ b/vendor/github.com/obalunenko/getenv/README.md @@ -2,12 +2,13 @@ [![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv) [![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv) [![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv) -![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-98.93%25-brightgreen?longCache=true&style=flat) +![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-100%25-brightgreen?longCache=true&style=flat) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obalunenko_getenv&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=obalunenko_getenv) # getenv -Package getenv provides functionality for loading environment variables and parse them into go builtin types. +Package getenv provides a simple way to get environment variables. +It's type-safe and supports built-in types and slices of them. Types supported: - string @@ -30,6 +31,8 @@ Types supported: - []uint64 - uint - []uint +- uintptr +- []uintptr - uint32 - []uint32 - float32 @@ -41,18 +44,24 @@ Types supported: - time.Duration - []time.Duration - bool +- []bool - url.URL - []url.URL - net.IP - []net.IP +- complex64 +- []complex64 +- complex128 +- []complex128 ## Examples ### EnvOrDefault -EnvOrDefault retrieves the value of the environment variable named -by the key. -If variable not set or value is empty - defaultVal will be returned. +EnvOrDefault retrieves the value of the environment variable named by the key. +If the variable is present in the environment the value will be parsed and returned. +Otherwise, the default value will be returned. +The value returned will be of the same type as the default value. ```golang package main @@ -140,6 +149,30 @@ func main() { val = getenv.EnvOrDefault(key, net.IP{}) fmt.Printf("[%T]: %v\n", val, val) + // []string + if err := os.Setenv(key, "a,b,c,d"); err != nil { + panic(err) + } + + val = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(",")) + fmt.Printf("[%T]: %v\n", val, val) + + // complex128 + if err := os.Setenv(key, "1+2i"); err != nil { + panic(err) + } + + val = getenv.EnvOrDefault(key, complex128(0)) + fmt.Printf("[%T]: %v\n", val, val) + + // []complex64 + if err := os.Setenv(key, "1+2i,3+4i"); err != nil { + panic(err) + } + + val = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(",")) + fmt.Printf("[%T]: %v\n", val, val) + } ``` @@ -154,4 +187,7 @@ Output: [time.Duration]: 2h35m0s [url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history } [net.IP]: 2001:cb8::17 +[[]string]: [a b c d] +[complex128]: (1+2i) +[[]complex64]: [(1+2i) (3+4i)] ``` diff --git a/vendor/github.com/obalunenko/getenv/getenv.go b/vendor/github.com/obalunenko/getenv/getenv.go index 3ba4aeec..f4934aa3 100644 --- a/vendor/github.com/obalunenko/getenv/getenv.go +++ b/vendor/github.com/obalunenko/getenv/getenv.go @@ -1,4 +1,5 @@ -// Package getenv provides functionality for loading environment variables and parse them into go builtin types. +// Package getenv provides a simple way to get environment variables. +// It's type-safe and supports built-in types and slices of them. // // Types supported: // - string @@ -21,6 +22,8 @@ // - []uint64 // - uint // - []uint +// - uintptr +// - []uintptr // - uint32 // - []uint32 // - float32 @@ -32,10 +35,15 @@ // - time.Duration // - []time.Duration // - bool +// - []bool // - url.URL // - []url.URL // - net.IP // - []net.IP +// - complex64 +// - []complex64 +// - complex128 +// - []complex128 package getenv import ( @@ -43,9 +51,10 @@ import ( "github.com/obalunenko/getenv/option" ) -// EnvOrDefault retrieves the value of the environment variable named -// by the key. -// If variable not set or value is empty - defaultVal will be returned. +// EnvOrDefault retrieves the value of the environment variable named by the key. +// If the variable is present in the environment the value will be parsed and returned. +// Otherwise, the default value will be returned. +// The value returned will be of the same type as the default value. func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...option.Option) T { w := internal.NewEnvParser(defaultVal) @@ -56,6 +65,7 @@ func EnvOrDefault[T internal.EnvParsable](key string, defaultVal T, options ...o return val.(T) } +// newParseParams creates new parameters from options. func newParseParams(opts []option.Option) internal.Parameters { var p internal.Parameters diff --git a/vendor/github.com/obalunenko/getenv/internal/constraint.go b/vendor/github.com/obalunenko/getenv/internal/constraint.go index 8bf4293a..cdc0724d 100644 --- a/vendor/github.com/obalunenko/getenv/internal/constraint.go +++ b/vendor/github.com/obalunenko/getenv/internal/constraint.go @@ -7,12 +7,12 @@ import ( ) type ( - // EnvParsable is a constraint for supported environment variable types parsers. + // EnvParsable is a constraint for types that can be parsed from environment variable. EnvParsable interface { - String | Int | Uint | Float | Time | bool | url.URL | []url.URL | net.IP | []net.IP + String | Int | Uint | Float | Time | Bool | URL | IP | Complex } - // String is a constraint for strings and slice of strings. + // String is a constraint for string and slice of strings. String interface { string | []string } @@ -24,16 +24,36 @@ type ( // Uint is a constraint for unsigned integer and slice of unsigned integers. Uint interface { - uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 + uint | []uint | uint8 | []uint8 | uint16 | []uint16 | uint32 | []uint32 | uint64 | []uint64 | uintptr | []uintptr } - // Float is a constraint for floats and slice of floats. + // Float is a constraint for float and slice of floats. Float interface { float32 | []float32 | float64 | []float64 } - // Time is a constraint for time.Time and time.Duration. + // Time is a constraint for time.Time and slice of time.Time. Time interface { time.Time | []time.Time | time.Duration | []time.Duration } + + // Bool is a constraint for bool and slice of bool. + Bool interface { + bool | []bool + } + + // URL is a constraint for url.URL and slice of url.URL. + URL interface { + url.URL | []url.URL + } + + // IP is a constraint for net.IP and slice of net.IP. + IP interface { + net.IP | []net.IP + } + + // Complex is a constraint for complex and slice of complex. + Complex interface { + complex64 | []complex64 | complex128 | []complex128 + } ) diff --git a/vendor/github.com/obalunenko/getenv/internal/iface.go b/vendor/github.com/obalunenko/getenv/internal/iface.go index 4b74e35d..03ae958a 100644 --- a/vendor/github.com/obalunenko/getenv/internal/iface.go +++ b/vendor/github.com/obalunenko/getenv/internal/iface.go @@ -17,22 +17,20 @@ func NewEnvParser(v any) EnvParser { p = newStringParser(t) case int, []int, int8, []int8, int16, []int16, int32, []int32, int64, []int64: p = newIntParser(t) - case uint, []uint, uint8, []uint8, uint16, []uint16, uint32, []uint32, uint64, []uint64: + case uint, []uint, uint8, []uint8, uint16, []uint16, uint32, []uint32, uint64, []uint64, uintptr, []uintptr: p = newUintParser(t) - case bool: - p = boolParser(t) + case bool, []bool: + p = newBoolParser(t) case float32, []float32, float64, []float64: p = newFloatParser(t) case time.Time, []time.Time, time.Duration, []time.Duration: p = newTimeParser(t) - case url.URL: - p = urlParser(t) - case []url.URL: - p = urlSliceParser(t) - case net.IP: - p = ipParser(t) - case []net.IP: - p = ipSliceParser(t) + case url.URL, []url.URL: + p = newURLParser(t) + case net.IP, []net.IP: + p = newIPParser(t) + case complex64, []complex64, complex128, []complex128: + p = newComplexParser(t) default: p = nil } @@ -44,6 +42,46 @@ func NewEnvParser(v any) EnvParser { return p } +// newComplexParser is a constructor for complex parsers. +func newComplexParser(v any) EnvParser { + switch t := v.(type) { + case complex64: + return complex64Parser(t) + case []complex64: + return complex64SliceParser(t) + case complex128: + return complex128Parser(t) + case []complex128: + return complex128SliceParser(t) + default: + return nil + } +} + +// newURLParser is a constructor for url.URL parsers. +func newURLParser(v any) EnvParser { + switch t := v.(type) { + case url.URL: + return urlParser(t) + case []url.URL: + return urlSliceParser(t) + default: + return nil + } +} + +// newIPParser is a constructor for net.IP parsers. +func newIPParser(v any) EnvParser { + switch t := v.(type) { + case net.IP: + return ipParser(t) + case []net.IP: + return ipSliceParser(t) + default: + return nil + } +} + func newStringParser(v any) EnvParser { switch t := v.(type) { case string: @@ -55,6 +93,7 @@ func newStringParser(v any) EnvParser { } } +// newIntParser is a constructor for integer parsers. func newIntParser(v any) EnvParser { switch t := v.(type) { case int: @@ -82,6 +121,7 @@ func newIntParser(v any) EnvParser { } } +// newUintParser is a constructor for unsigned integer parsers. func newUintParser(v any) EnvParser { switch t := v.(type) { case uint8: @@ -104,11 +144,16 @@ func newUintParser(v any) EnvParser { return uint64Parser(t) case []uint64: return uint64SliceParser(t) + case uintptr: + return uintptrParser(t) + case []uintptr: + return uintptrSliceParser(t) default: return nil } } +// newFloatParser is a constructor for float parsers. func newFloatParser(v any) EnvParser { switch t := v.(type) { case float32: @@ -124,6 +169,7 @@ func newFloatParser(v any) EnvParser { } } +// newTimeParser is a constructor for time parsers. func newTimeParser(v any) EnvParser { switch t := v.(type) { case time.Time: @@ -139,11 +185,25 @@ func newTimeParser(v any) EnvParser { } } +// newBoolParser is a constructor for boolParser. +func newBoolParser(v any) EnvParser { + switch t := v.(type) { + case bool: + return boolParser(t) + case []bool: + return boolSliceParser(t) + default: + return nil + } +} + // EnvParser interface for parsing environment variables. type EnvParser interface { + // ParseEnv parses environment variable by key and returns value. ParseEnv(key string, defaltVal any, options Parameters) any } +// stringParser is a parser for string type. type stringParser string func (s stringParser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -409,6 +469,7 @@ func (i uint16SliceParser) ParseEnv(key string, defaltVal any, options Parameter return val } +// uint16Parser is a parser for uint16 type uint16Parser uint func (d uint16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -417,6 +478,7 @@ func (d uint16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } +// uint32Parser is a parser for uint32 type uint32Parser uint func (d uint32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -425,6 +487,7 @@ func (d uint32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } +// stringSliceParser is a parser for []string type urlParser url.URL func (t urlParser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -433,6 +496,7 @@ func (t urlParser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } +// urlSliceParser is a parser for []url.URL type urlSliceParser []url.URL func (t urlSliceParser) ParseEnv(key string, defaltVal any, opts Parameters) any { @@ -443,6 +507,7 @@ func (t urlSliceParser) ParseEnv(key string, defaltVal any, opts Parameters) any return val } +// ipParser is a parser for net.IP type ipParser net.IP func (t ipParser) ParseEnv(key string, defaltVal any, _ Parameters) any { @@ -451,6 +516,7 @@ func (t ipParser) ParseEnv(key string, defaltVal any, _ Parameters) any { return val } +// ipSliceParser is a parser for []net.IP type ipSliceParser []net.IP func (t ipSliceParser) ParseEnv(key string, defaltVal any, opts Parameters) any { @@ -460,3 +526,74 @@ func (t ipSliceParser) ParseEnv(key string, defaltVal any, opts Parameters) any return val } + +// boolSliceParser is a parser for []bool +type boolSliceParser []bool + +func (b boolSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { + sep := options.Separator + + val := boolSliceOrDefault(key, defaltVal.([]bool), sep) + + return val +} + +// uintptrParser is a parser for uintptr +type uintptrParser uintptr + +func (d uintptrParser) ParseEnv(key string, defaltVal any, _ Parameters) any { + val := uintptrOrDefault(key, defaltVal.(uintptr)) + + return val +} + +// uintptrSliceParser is a parser for []uintptr +type uintptrSliceParser []uintptr + +func (i uintptrSliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { + sep := options.Separator + + val := uintptrSliceOrDefault(key, defaltVal.([]uintptr), sep) + + return val +} + +// complex64Parser is a parser for complex64 +type complex64Parser complex64 + +func (d complex64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { + val := complex64OrDefault(key, defaltVal.(complex64)) + + return val +} + +// complex64SliceParser is a parser for []complex64 +type complex64SliceParser []complex64 + +func (i complex64SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { + sep := options.Separator + + val := complex64SliceOrDefault(key, defaltVal.([]complex64), sep) + + return val +} + +// complex128Parser is a parser for complex128 +type complex128Parser complex128 + +func (d complex128Parser) ParseEnv(key string, defaltVal any, _ Parameters) any { + val := complex128OrDefault(key, defaltVal.(complex128)) + + return val +} + +// complex128SliceParser is a parser for []complex128 +type complex128SliceParser []complex128 + +func (i complex128SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any { + sep := options.Separator + + val := complex128SliceOrDefault(key, defaltVal.([]complex128), sep) + + return val +} diff --git a/vendor/github.com/obalunenko/getenv/internal/options.go b/vendor/github.com/obalunenko/getenv/internal/options.go index c9bde14b..65c26b26 100644 --- a/vendor/github.com/obalunenko/getenv/internal/options.go +++ b/vendor/github.com/obalunenko/getenv/internal/options.go @@ -1,6 +1,9 @@ package internal -// Parameters represents environment parser parameters. +// Parameters is a struct for holding parameters for the parser. +// It is used to pass parameters to the parser. +// Separator is a separator for the environment variable that holds slice. +// Layout is a layout for the time.Time. type Parameters struct { Separator string Layout string diff --git a/vendor/github.com/obalunenko/getenv/internal/parsers.go b/vendor/github.com/obalunenko/getenv/internal/parsers.go index ad9a9feb..f3641bd5 100644 --- a/vendor/github.com/obalunenko/getenv/internal/parsers.go +++ b/vendor/github.com/obalunenko/getenv/internal/parsers.go @@ -55,6 +55,29 @@ func boolOrDefault(key string, defaultVal bool) bool { return val } +// boolSliceOrDefault retrieves the bool slice value of the environment variable named +// by the key and separated by sep. +// If variable not set or value is empty - defaultVal will be returned. +func boolSliceOrDefault(key string, defaultVal []bool, sep string) []bool { + valraw := stringSliceOrDefault(key, nil, sep) + if valraw == nil { + return defaultVal + } + + val := make([]bool, 0, len(valraw)) + + for _, s := range valraw { + b, err := strconv.ParseBool(s) + if err != nil { + return defaultVal + } + + val = append(val, b) + } + + return val +} + // stringSliceOrDefault retrieves the string slice value of the environment variable named // by the key and separated by sep. // If variable not set or value is empty - defaultVal will be returned. @@ -801,3 +824,149 @@ func ipSliceOrDefault(key string, defaultVal []net.IP, sep string) []net.IP { return val } + +// uintptrOrDefault retrieves the uintptr value of the environment variable named +// by the key. +// If variable not set or value is empty - defaultVal will be returned. +func uintptrOrDefault(key string, defaultVal uintptr) uintptr { + env := stringOrDefault(key, "") + if env == "" { + return defaultVal + } + + const ( + base = 10 + bitsize = 0 + ) + + val, err := strconv.ParseUint(env, base, bitsize) + if err != nil { + return defaultVal + } + + return uintptr(val) +} + +// uintptrSliceOrDefault retrieves the uintptr slice value of the environment variable named +// by the key and separated by sep. +// If variable not set or value is empty - defaultVal will be returned. +func uintptrSliceOrDefault(key string, defaultVal []uintptr, sep string) []uintptr { + valraw := stringSliceOrDefault(key, nil, sep) + if valraw == nil { + return defaultVal + } + + val := make([]uintptr, 0, len(valraw)) + + const ( + base = 10 + bitsize = 0 + ) + + for _, s := range valraw { + v, err := strconv.ParseUint(s, base, bitsize) + if err != nil { + return defaultVal + } + + val = append(val, uintptr(v)) + } + + return val +} + +// complex64OrDefault retrieves the complex64 value of the environment variable named +// by the key. +// If variable not set or value is empty - defaultVal will be returned. +func complex64OrDefault(key string, defaultVal complex64) complex64 { + env := stringOrDefault(key, "") + if env == "" { + return defaultVal + } + + const ( + bitsize = 64 + ) + + val, err := strconv.ParseComplex(env, bitsize) + if err != nil { + return defaultVal + } + + return complex64(val) +} + +// complex64SliceOrDefault retrieves the complex64 slice value of the environment variable named +// by the key and separated by sep. +// If variable not set or value is empty - defaultVal will be returned. +func complex64SliceOrDefault(key string, defaultVal []complex64, sep string) []complex64 { + valraw := stringSliceOrDefault(key, nil, sep) + if valraw == nil { + return defaultVal + } + + const ( + bitsize = 64 + ) + + val := make([]complex64, 0, len(valraw)) + + for _, s := range valraw { + v, err := strconv.ParseComplex(s, bitsize) + if err != nil { + return defaultVal + } + + val = append(val, complex64(v)) + } + + return val +} + +// complex128OrDefault retrieves the complex128 value of the environment variable named +// by the key. +// If variable not set or value is empty - defaultVal will be returned. +func complex128OrDefault(key string, defaultVal complex128) complex128 { + env := stringOrDefault(key, "") + if env == "" { + return defaultVal + } + + const ( + bitsize = 128 + ) + + val, err := strconv.ParseComplex(env, bitsize) + if err != nil { + return defaultVal + } + + return val +} + +// complex128SliceOrDefault retrieves the complex128 slice value of the environment variable named +// by the key and separated by sep. +// If variable not set or value is empty - defaultVal will be returned. +func complex128SliceOrDefault(key string, defaultVal []complex128, sep string) []complex128 { + valraw := stringSliceOrDefault(key, nil, sep) + if valraw == nil { + return defaultVal + } + + const ( + bitsize = 128 + ) + + val := make([]complex128, 0, len(valraw)) + + for _, s := range valraw { + v, err := strconv.ParseComplex(s, bitsize) + if err != nil { + return defaultVal + } + + val = append(val, v) + } + + return val +} diff --git a/vendor/github.com/obalunenko/getenv/option/option.go b/vendor/github.com/obalunenko/getenv/option/option.go index adca202a..beebb013 100644 --- a/vendor/github.com/obalunenko/getenv/option/option.go +++ b/vendor/github.com/obalunenko/getenv/option/option.go @@ -5,8 +5,9 @@ import ( "github.com/obalunenko/getenv/internal" ) -// Option implements options for EnvOrDefault. +// Option is a contract for EnvParser parameters options. type Option interface { + // Apply applies the option to the parameters. Apply(params *internal.Parameters) } @@ -16,7 +17,7 @@ func (w withSeparator) Apply(p *internal.Parameters) { p.Separator = string(w) } -// WithSeparator ads slice separator option. +// WithSeparator adds slice separator option. func WithSeparator(separator string) Option { return withSeparator(separator) } @@ -27,7 +28,7 @@ func (w withTimeLayout) Apply(p *internal.Parameters) { p.Layout = string(w) } -// WithTimeLayout ads time layout option. +// WithTimeLayout adds time.Time layout option. func WithTimeLayout(layout string) Option { return withTimeLayout(layout) } diff --git a/vendor/modules.txt b/vendor/modules.txt index a58611d3..91965015 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -37,7 +37,7 @@ github.com/mattn/go-colorable # github.com/mattn/go-isatty v0.0.14 ## explicit; go 1.12 github.com/mattn/go-isatty -# github.com/obalunenko/getenv v1.8.0 +# github.com/obalunenko/getenv v1.9.1 ## explicit; go 1.20 github.com/obalunenko/getenv github.com/obalunenko/getenv/internal