Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: unexport SourceFunc #43

Merged
merged 1 commit into from
Nov 11, 2023
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
14 changes: 6 additions & 8 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ type Source interface {
LookupEnv(key string) (value string, ok bool)
}

// SourceFunc is an adapter that allows using a function as a [Source].
type SourceFunc func(key string) (value string, ok bool)

// LookupEnv implements the [Source] interface.
func (fn SourceFunc) LookupEnv(key string) (string, bool) { return fn(key) }

// OS is the main [Source] that uses [os.LookupEnv].
var OS Source = SourceFunc(os.LookupEnv)
var OS Source = sourceFunc(os.LookupEnv)

// Map is an in-memory [Source] implementation useful in tests.
// Map is a [Source] implementation useful in tests.
type Map map[string]string

// LookupEnv implements the [Source] interface.
func (m Map) LookupEnv(key string) (string, bool) {
value, ok := m[key]
return value, ok
}

type sourceFunc func(key string) (string, bool)

func (fn sourceFunc) LookupEnv(key string) (string, bool) { return fn(key) }
23 changes: 1 addition & 22 deletions source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,19 @@ import (
. "go-simpler.org/env/internal/assert/EF"
)

func TestSourceFunc_LookupEnv(t *testing.T) {
fn := env.SourceFunc(func(key string) (string, bool) {
switch key {
case "FOO":
return "1", true
case "BAR":
return "2", true
case "BAZ":
return "3", true
default:
return "", false
}
})

testSource(t, fn)
}

func TestMap_LookupEnv(t *testing.T) {
m := env.Map{
"FOO": "1",
"BAR": "2",
"BAZ": "3",
}

testSource(t, m)
}

func testSource(t *testing.T, src env.Source) {
var cfg struct {
Foo int `env:"FOO"`
Bar int `env:"BAR"`
Baz int `env:"BAZ"`
}
err := env.Load(&cfg, &env.Options{Source: src})
err := env.Load(&cfg, &env.Options{Source: m})
assert.NoErr[F](t, err)
assert.Equal[E](t, cfg.Foo, 1)
assert.Equal[E](t, cfg.Bar, 2)
Expand Down