From 119c113bffc44915e08dcab3cae64e91ac6aedad Mon Sep 17 00:00:00 2001 From: Denis Shaposhnikov <993498+dsh2dsh@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:22:44 +0200 Subject: [PATCH] Remove LoadTo(), because it can be replaced by `Load()` with callback: ```go err := dotenv.New().Load(func() error { return env.Parse(&cfg) }) ``` --- doc_test.go | 22 +++++++------ dotenv.go | 16 ---------- dotenv_test.go | 83 -------------------------------------------------- 3 files changed, 12 insertions(+), 109 deletions(-) diff --git a/doc_test.go b/doc_test.go index 7cec036..ad71c5a 100644 --- a/doc_test.go +++ b/doc_test.go @@ -5,47 +5,49 @@ import ( "log" "os" + "github.com/caarlos0/env/v9" + dotenv "github.com/dsh2dsh/expx-dotenv" ) func Example() { - env := dotenv.New() - if err := env.Load(); err != nil { + if err := dotenv.New().Load(); err != nil { log.Fatalf("error loading .env files: %v", err) } } func Example_chainedCalls() { - env := dotenv.New() - if err := env.WithDepth(1).WithEnvSuffix("test").Load(); err != nil { + if err := dotenv.New().WithDepth(1).WithEnvSuffix("test").Load(); err != nil { log.Fatalf("error loading .env files: %v", err) } } func Example_withParse() { - env := dotenv.New() cfg := struct { SomeOpt string `env:"ENV_VAR1"` }{ SomeOpt: "some default value, because we don't have .env file(s)", } - if err := env.LoadTo(&cfg); err != nil { + + err := dotenv.New().Load(func() error { + return env.Parse(&cfg) //nolint:wrapcheck + }) + if err != nil { log.Fatalf("error loading .env files: %v", err) } + fmt.Println(cfg.SomeOpt) // Output: some default value, because we don't have .env file(s) } func ExampleLoader_WithRootDir() { - env := dotenv.New() // "ENV_ROOT" environment variable contains name of current environment. - env.WithRootDir(os.Getenv("ENV_ROOT")) + dotenv.New().WithRootDir(os.Getenv("ENV_ROOT")) } func ExampleLoader_WithRootFiles() { - env := dotenv.New() // stop at dir, which contains ".git" - env.WithRootFiles(".git") + dotenv.New().WithRootFiles(".git") } func ExampleLoader_WithRootCallback() { diff --git a/dotenv.go b/dotenv.go index fb3246d..8e7a4d2 100644 --- a/dotenv.go +++ b/dotenv.go @@ -14,7 +14,6 @@ import ( "os" "path/filepath" - "github.com/caarlos0/env/v9" "github.com/joho/godotenv" ) @@ -204,21 +203,6 @@ func (self *Loader) Load(cbs ...func() error) error { return nil } -// LoadTo loads all .env files like [Loader.Load] and fills every received -// struct with values from environment variables, using [env.Parse]. It expects -// zero or more args are pointers to structs. Without args it has no difference -// from [Loader.Load]. -func (self *Loader) LoadTo(to ...any) error { - return self.Load(func() error { - for _, v := range to { - if err := env.Parse(v); err != nil { - return fmt.Errorf("parse .env into struct: %w", err) - } - } - return nil - }) -} - // FileExistsInDir checks if file named fname exists in dir named dirName and // returns true, if it exists, or false. // diff --git a/dotenv_test.go b/dotenv_test.go index 59366da..4d9c271 100644 --- a/dotenv_test.go +++ b/dotenv_test.go @@ -420,89 +420,6 @@ func restoreEnvVars(t *testing.T) { } } -func TestLoadTo(t *testing.T) { - type testStruct struct { - TestVar string `env:"TEST_VAR1"` - } - - tests := []struct { - name string - chDir string - call func(env *Loader, t *testing.T) []string - expected []string - }{ - { - name: "optional args", - call: func(env *Loader, t *testing.T) (parsed []string) { - require.NoError(t, env.LoadTo()) - return - }, - }, - { - name: "loaded nothing", - call: func(env *Loader, t *testing.T) []string { - cfg := testStruct{"default value"} - require.NoError(t, env.LoadTo(&cfg)) - return []string{cfg.TestVar} - }, - expected: []string{"default value"}, - }, - { - name: "loaded one", - chDir: "testdata", - call: func(env *Loader, t *testing.T) []string { - cfg := testStruct{"default value"} - require.NoError(t, env.LoadTo(&cfg)) - return []string{cfg.TestVar} - }, - expected: []string{"testdata"}, - }, - { - name: "loaded multiple", - chDir: "testdata", - call: func(env *Loader, t *testing.T) []string { - cfg1 := testStruct{"default value 1"} - cfg2 := struct { - TestVar string `env:"TEST_VAR2"` - }{"default value 2"} - require.NoError(t, env.LoadTo(&cfg1, &cfg2)) - return []string{cfg1.TestVar, cfg2.TestVar} - }, - expected: []string{"testdata", "testdata2"}, - }, - { - name: "with err from Load", - chDir: "testdata", - call: func(env *Loader, t *testing.T) (parsed []string) { - require.Error(t, env.WithEnvSuffix("error").LoadTo()) - return - }, - }, - { - name: "with err from Parse", - chDir: "testdata", - call: func(env *Loader, t *testing.T) (parsed []string) { - cfg := struct { - TestVar bool `env:"TEST_VAR1"` - }{} - require.Error(t, env.LoadTo(&cfg)) - return - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.chDir != "" { - changeDir(t, tt.chDir) - } - restoreEnvVars(t) - env := New() - assert.Equal(t, tt.expected, tt.call(env, t)) - }) - } -} - func TestLoad_errorGetwd(t *testing.T) { tmpDir := valueNoError[string](t)(os.MkdirTemp("", "expx-dotenv-")) t.Cleanup(func() {