Skip to content

Commit

Permalink
Rework ConfigLoader to collect values accross successive LoadConfig a…
Browse files Browse the repository at this point in the history
…nd LoadValues calls
  • Loading branch information
Rieb, Elias committed Dec 13, 2023
1 parent 53bb04a commit 470f5f1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 117 deletions.
62 changes: 32 additions & 30 deletions pkg/configloader/loader.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
package configloader

import (
"errors"
"fmt"

auconfigapi "github.com/StephanHCB/go-autumn-config-api"
)

type ConfigurationLoader struct {
type ConfigLoader struct {
providers []Provider
values map[string]string
}

type Config interface {
ConfigItems() []auconfigapi.ConfigItem

ObtainValues(getter func(string) string) error
}

type Provider func([]auconfigapi.ConfigItem) (map[string]string, error)

func New() *ConfigLoader {
return &ConfigLoader{
values: make(map[string]string),
}
}

func New(providers ...Provider) *ConfigurationLoader {
return &ConfigurationLoader{
providers: providers,
func (l *ConfigLoader) LoadConfig(config Config, providers ...Provider) error {
if err := l.LoadValues(config.ConfigItems(), providers...); err != nil {
return err
}
return config.ObtainValues(l.Get)
}

func (l *ConfigLoader) Get(key string) string {
return l.values[key]
}

func (l *ConfigurationLoader) ObtainValues(
func (l *ConfigLoader) LoadValues(
configItems []auconfigapi.ConfigItem,
) (map[string]string, error) {
values, err := loadValues(configItems, l.providers...)
providers ...Provider,
) error {
values, err := loadValues(configItems, providers...)
if err != nil {
return nil, err
return err
}
if err = validateValues(configItems, values); err != nil {
return nil, err
for key, value := range values {
l.values[key] = value
}
return values, nil
return nil
}

func loadValues(
Expand All @@ -49,19 +67,3 @@ func loadValues(
}
return rawValues, nil
}

func validateValues(
configItems []auconfigapi.ConfigItem,
values map[string]string,
) error {
var errs = make([]error, 0)
for _, it := range configItems {
if it.Validate != nil {
err := it.Validate(values[it.Key])
if err != nil {
errs = append(errs, fmt.Errorf("failed to validate configuration value of %s: %s", it.Key, err.Error()))
}
}
}
return errors.Join(errs...)
}
3 changes: 3 additions & 0 deletions pkg/configloader/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package configloader_test

// ToDo
2 changes: 0 additions & 2 deletions pkg/configloader/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"gopkg.in/yaml.v3"
)

type Provider func([]auconfigapi.ConfigItem) (map[string]string, error)

func CreateDefaultValuesProvider() Provider {
return func(configItems []auconfigapi.ConfigItem) (map[string]string, error) {
rawValues := make(map[string]string)
Expand Down
3 changes: 3 additions & 0 deletions pkg/configloader/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package configloader_test

// ToDo
85 changes: 0 additions & 85 deletions pkg/configloader/validator.go

This file was deleted.

0 comments on commit 470f5f1

Please sign in to comment.