Skip to content

Commit

Permalink
feat!: unexport SourceFunc (#43)
Browse files Browse the repository at this point in the history
tmzane authored Nov 11, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent dd7ebb1 commit 260b66b
Showing 2 changed files with 7 additions and 30 deletions.
14 changes: 6 additions & 8 deletions source.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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)

0 comments on commit 260b66b

Please sign in to comment.