Skip to content

Commit

Permalink
return LoaderFunc from predefined loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Aug 23, 2023
1 parent 0d9015b commit e1a7788
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
16 changes: 6 additions & 10 deletions examples/xload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ func main() {
ctx,
cfg,
xload.FieldTagName("env"),
xload.WithLoader(
xload.SerialLoader(
yamlLoader,
xload.OSLoader(), // OSLoader values take precedence over yaml
),
xload.SerialLoader(
yamlLoader,
xload.OSLoader(), // OSLoader values take precedence over yaml
),
)
if err != nil {
Expand Down Expand Up @@ -74,11 +72,9 @@ func main() {
rcfg,
xload.Concurrency(5), // use xload.Concurrency to load values concurrently
xload.FieldTagName("env"),
xload.WithLoader(
xload.SerialLoader(
reqYamlLoader,
businessConfigLoader(), // businessConfigLoader values take precedence over yaml
),
xload.SerialLoader(
reqYamlLoader,
businessConfigLoader(), // businessConfigLoader values take precedence over yaml
),
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions xload/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoad_Async(t *testing.T) {

err := Load(context.Background(), &cfg,
Concurrency(3),
WithLoader(LoaderFunc(loader)),
LoaderFunc(loader),
)
assert.NoError(t, err)

Expand Down Expand Up @@ -87,7 +87,7 @@ func TestLoad_Async_Error(t *testing.T) {

err := Load(context.Background(), &cfg,
Concurrency(2),
WithLoader(LoaderFunc(loader)),
LoaderFunc(loader),
)
assert.Error(t, err)

Expand Down
12 changes: 6 additions & 6 deletions xload/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func ExampleLoad_customLoader() {
context.Background(),
&conf,
xload.FieldTagName("env"),
xload.WithLoader(loader),
loader,
)
if err != nil {
panic(err)
Expand Down Expand Up @@ -159,19 +159,19 @@ func ExampleLoad_transformFieldName() {
var conf AppConf

// transform converts key from MYAPP_HOST to myapp.host
transform := func(next xload.Loader) xload.Loader {
return xload.LoaderFunc(func(ctx context.Context, key string) (string, error) {
transform := func(next xload.Loader) xload.LoaderFunc {
return func(ctx context.Context, key string) (string, error) {
newKey := strings.ReplaceAll(key, "_", ".")
newKey = strings.ToLower(newKey)

return next.Load(ctx, newKey)
})
}
}

err := xload.Load(
context.Background(),
&conf,
xload.WithLoader(transform(xload.OSLoader())),
transform(xload.OSLoader()),
)
if err != nil {
panic(err)
Expand Down Expand Up @@ -270,7 +270,7 @@ func ExampleLoad_concurrentLoading() {
&conf,
xload.Concurrency(3), // load 3 keys concurrently
xload.FieldTagName("env"),
xload.WithLoader(loader),
loader,
)
if err != nil {
panic(err)
Expand Down
12 changes: 6 additions & 6 deletions xload/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func PrefixLoader(prefix string, loader Loader) LoaderFunc {
}

// OSLoader loads values from the OS environment.
func OSLoader() Loader {
return LoaderFunc(func(ctx context.Context, key string) (string, error) {
func OSLoader() LoaderFunc {
return func(ctx context.Context, key string) (string, error) {
v, ok := os.LookupEnv(key)
if !ok {
return "", nil
}

return v, nil
})
}
}

// SerialLoader loads values from multiple loaders.
// Last non-empty value wins.
func SerialLoader(loaders ...Loader) Loader {
return LoaderFunc(func(ctx context.Context, key string) (string, error) {
func SerialLoader(loaders ...Loader) LoaderFunc {
return func(ctx context.Context, key string) (string, error) {
var lastNonEmpty string

for _, loader := range loaders {
Expand All @@ -53,7 +53,7 @@ func SerialLoader(loaders ...Loader) Loader {
}

return lastNonEmpty, nil
})
}
}

func (f LoaderFunc) apply(opts *options) { opts.loader = f }
2 changes: 2 additions & 0 deletions xload/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func FlattenMap(m map[string]interface{}, sep string) map[string]string {
return flatten(m, "", sep)
}

func (m MapLoader) apply(opts *options) { opts.loader = m }

func flatten(m map[string]interface{}, prefix string, sep string) map[string]string {
flattened := make(map[string]string)

Expand Down
2 changes: 1 addition & 1 deletion xload/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestOptions(t *testing.T) {
},
{
name: "loader",
opts: []Option{WithLoader(MapLoader{"A": "1"})},
opts: []Option{MapLoader{"A": "1"}},
want: &options{
tagName: defaultKey,
loader: MapLoader{"A": "1"},
Expand Down
4 changes: 2 additions & 2 deletions xload/providers/yaml/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Example() {
panic(err)
}

err = xload.Load(ctx, &cfg, xload.WithLoader(loader))
err = xload.Load(ctx, &cfg, loader)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func Example_nestedStruct() {
panic(err)
}

err = xload.Load(ctx, &cfg, xload.WithLoader(loader))
err = xload.Load(ctx, &cfg, loader)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit e1a7788

Please sign in to comment.