Skip to content

Commit 8aee119

Browse files
authoredApr 2, 2024··
feat: ParseAs, ParseAsWithOptions, Must (#300)
* feat: ParseAs, ParseAsWithOptions, Must closes #299 Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * build: build with go 1.17 as well Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * feat: change min go version to 1.18 Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent fa32ef4 commit 8aee119

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed
 

‎.github/workflows/build.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: build
33
on:
44
push:
55
branches:
6-
- 'main'
6+
- "main"
77
tags:
8-
- 'v*'
8+
- "v*"
99
pull_request:
1010

1111
jobs:
@@ -18,8 +18,8 @@ jobs:
1818
build:
1919
strategy:
2020
matrix:
21-
os: [ ubuntu-latest, macos-latest, windows-latest ]
22-
go-version: [ oldstable, stable]
21+
os: [ubuntu-latest, macos-latest, windows-latest]
22+
go-version: [1.18, oldstable, stable]
2323
runs-on: ${{ matrix.os }}
2424
steps:
2525
- uses: actions/checkout@v4

‎env.go

+22
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,28 @@ func ParseWithOptions(v interface{}, opts Options) error {
191191
return parseInternal(v, setField, customOptions(opts))
192192
}
193193

194+
// ParseAs parses the given struct type containing `env` tags and loads its
195+
// values from environment variables.
196+
func ParseAs[T any]() (T, error) {
197+
var t T
198+
return t, Parse(&t)
199+
}
200+
201+
// ParseWithOptions parses the given struct type containing `env` tags and
202+
// loads its values from environment variables.
203+
func ParseAsWithOptions[T any](opts Options) (T, error) {
204+
var t T
205+
return t, ParseWithOptions(&t, opts)
206+
}
207+
208+
// Must panic is if err is not nil, and returns t otherwise.
209+
func Must[T any](t T, err error) T {
210+
if err != nil {
211+
panic(err)
212+
}
213+
return t
214+
}
215+
194216
// GetFieldParams parses a struct containing `env` tags and returns information about
195217
// tags it found.
196218
func GetFieldParams(v interface{}) ([]FieldParams, error) {

‎env_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,46 @@ func TestGetFieldParamsError(t *testing.T) {
18541854
isTrue(t, errors.Is(err, NotStructPtrError{}))
18551855
}
18561856

1857+
type Conf struct {
1858+
Foo string `env:"FOO" envDefault:"bar"`
1859+
}
1860+
1861+
func TestParseAs(t *testing.T) {
1862+
config, err := ParseAs[Conf]()
1863+
isNoErr(t, err)
1864+
isEqual(t, "bar", config.Foo)
1865+
}
1866+
1867+
func TestParseAsWithOptions(t *testing.T) {
1868+
config, err := ParseAsWithOptions[Conf](Options{
1869+
Environment: map[string]string{
1870+
"FOO": "not bar",
1871+
},
1872+
})
1873+
isNoErr(t, err)
1874+
isEqual(t, "not bar", config.Foo)
1875+
}
1876+
1877+
type ConfRequired struct {
1878+
Foo string `env:"FOO,required"`
1879+
}
1880+
1881+
func TestMust(t *testing.T) {
1882+
t.Run("error", func(t *testing.T) {
1883+
defer func() {
1884+
err := recover()
1885+
isErrorWithMessage(t, err.(error), `env: required environment variable "FOO" is not set`)
1886+
}()
1887+
conf := Must(ParseAs[ConfRequired]())
1888+
isEqual(t, "", conf.Foo)
1889+
})
1890+
t.Run("success", func(t *testing.T) {
1891+
t.Setenv("FOO", "bar")
1892+
conf := Must(ParseAs[ConfRequired]())
1893+
isEqual(t, "bar", conf.Foo)
1894+
})
1895+
}
1896+
18571897
func isTrue(tb testing.TB, b bool) {
18581898
tb.Helper()
18591899

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/caarlos0/env/v10
22

3-
go 1.17
3+
go 1.18

0 commit comments

Comments
 (0)
Please sign in to comment.