Rename packages
This commit is contained in:
parent
49b8760527
commit
ef1243ea39
47 changed files with 524 additions and 369 deletions
60
internal/config/read.go
Normal file
60
internal/config/read.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/envreader"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/stringparser"
|
||||
)
|
||||
|
||||
// Unmarshal updates this configuration from the provided [io.Reader].
|
||||
//
|
||||
// Data is read using the [envreader.ReadAll] method, see the appropriate documentation for the file format.
|
||||
//
|
||||
// The `env` and `parser` reflect tags of the [Config] struct determine the keys to read from, and the types to expect.
|
||||
// When a key is missing, it is set to the default value.
|
||||
//
|
||||
// See also [stringparser.Parse].
|
||||
func (config *Config) Unmarshal(src io.Reader) error {
|
||||
// read all the values!
|
||||
values, err := envreader.ReadAll(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vConfig := reflect.ValueOf(config).Elem()
|
||||
tConfig := vConfig.Type()
|
||||
|
||||
// iterate over the types
|
||||
numValues := tConfig.NumField()
|
||||
for i := 0; i < numValues; i++ {
|
||||
tField := tConfig.Field(i)
|
||||
vField := vConfig.FieldByName(tField.Name)
|
||||
|
||||
env := tField.Tag.Get("env")
|
||||
dflt := tField.Tag.Get("default")
|
||||
parser := tField.Tag.Get("parser")
|
||||
|
||||
// skip it if it isn't loaded!
|
||||
if env == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// read the value with a default
|
||||
value, ok := values[env]
|
||||
if !ok || value == "" {
|
||||
if dflt == "" {
|
||||
continue
|
||||
}
|
||||
value = dflt
|
||||
}
|
||||
|
||||
// parse the value!
|
||||
if err := stringparser.Parse(parser, value, vField); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue