Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Types supported:
- []url.URL
- net.IP
- []net.IP
- netip.Addr
- []netip.Addr
- netip.Prefix
- []netip.Prefix
- net.HardwareAddr
- []net.HardwareAddr
- complex64
- []complex64
- complex128
Expand Down Expand Up @@ -190,11 +196,11 @@ Output:
```
[string]: golly; err: <nil>
[int]: 123; err: <nil>
[int]: 0; err: could not parse variable[GH_GETENV_TEST] value[123s4] to type[int]: invalid value
[int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: strconv.ParseInt: parsing "123s4": invalid syntax: invalid value
[time.Time]: 2022-01-20 00:00:00 +0000 UTC; err: <nil>
[[]float64]: [26.89 0.67]; err: <nil>
[time.Duration]: 2h35m0s; err: <nil>
[url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }; err: <nil>
[url.URL]: https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance&compact=false#history; err: <nil>
[net.IP]: 2001:cb8::17; err: <nil>
[[]string]: [a b c d]; err: <nil>
[complex128]: (1+2i); err: <nil>
Expand Down Expand Up @@ -332,7 +338,7 @@ Output:
[time.Time]: 2022-01-20 00:00:00 +0000 UTC
[[]float64]: [26.89 0.67]
[time.Duration]: 2h35m0s
[url.URL]: {https test:abcd123 golangbyexample.com:8000 /tutorials/intro false false type=advance&compact=false history }
[url.URL]: https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance&compact=false#history
[net.IP]: 2001:cb8::17
[[]string]: [a b c d]
[complex128]: (1+2i)
Expand Down
30 changes: 28 additions & 2 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
// - []url.URL
// - net.IP
// - []net.IP
// - netip.Addr
// - []netip.Addr
// - netip.Prefix
// - []netip.Prefix
// - net.HardwareAddr
// - []net.HardwareAddr
// - complex64
// - []complex64
// - complex128
Expand Down Expand Up @@ -75,11 +81,17 @@
val, err := w.ParseEnv(key, params)
if err != nil {
if errors.Is(err, internal.ErrNotSet) {
return t, fmt.Errorf("failed to get environment variable[%s]: %w", key, ErrNotSet)
return t, fmt.Errorf("failed to get environment variable[%s]: %w", key, publicError{
cause: err,
sentinel: ErrNotSet,
})
}

if errors.Is(err, internal.ErrInvalidValue) {
return t, fmt.Errorf("failed to parse environment variable[%s]: %w", key, ErrInvalidValue)
return t, fmt.Errorf("failed to parse environment variable[%s]: %w", key, publicError{

Check failure on line 91 in getenv.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "failed to parse environment variable[%s]: %w" 3 times.

See more on https://sonarcloud.io/project/issues?id=obalunenko_getenv&issues=AZ6aPvbLcXqDuhUkHk7u&open=AZ6aPvbLcXqDuhUkHk7u&pullRequest=197
cause: err,
sentinel: ErrInvalidValue,
})
}

return t, fmt.Errorf("failed to parse environment variable[%s]: %w", key, err)
Expand All @@ -106,6 +118,20 @@
return val
}

// publicError keeps parser details while matching exported sentinels.
type publicError struct {
cause error
sentinel error
}

func (e publicError) Error() string {
return e.cause.Error()
}

func (e publicError) Unwrap() []error {
return []error{e.cause, e.sentinel}
}

// newParseParams creates new parameters from options.
func newParseParams(opts []option.Option) internal.Parameters {
var p internal.Parameters
Expand Down
2 changes: 1 addition & 1 deletion getenv_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func ExampleEnv() {
// Output:
// [string]: golly; err: <nil>
// [int]: 123; err: <nil>
// [int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: invalid value
// [int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: strconv.ParseInt: parsing "123s4": invalid syntax: invalid value
// [time.Time]: 2022-01-20 00:00:00 +0000 UTC; err: <nil>
// [[]float64]: [26.89 0.67]; err: <nil>
// [time.Duration]: 2h35m0s; err: <nil>
Expand Down
Loading
Loading