basic conf for loader

This commit is contained in:
Andreas Schröpfer
2021-02-27 20:09:29 +01:00
parent ac9b441283
commit cc6e58acd9
7 changed files with 375 additions and 0 deletions

20
pkg/set/set.go Normal file
View File

@@ -0,0 +1,20 @@
package set
import (
"errors"
"reflect"
)
func Field(dst interface{}, fieldName string, value interface{}) error {
valDst := reflect.ValueOf(dst).Elem()
dstField := valDst.FieldByName(fieldName)
val := reflect.ValueOf(value)
if dstField.Kind() != val.Kind() {
return errors.New("value-Type does not match to field")
}
dstField.Set(val)
return nil
}

30
pkg/set/set_test.go Normal file
View File

@@ -0,0 +1,30 @@
package set
import "testing"
func TestField(t *testing.T) {
input := struct {
MyString string
MyInt int
MyBool bool
}{}
value := "abc"
Field(&input, "MyString", value)
if input.MyString != value {
t.Errorf("got: %s; want: %s", input.MyString, value)
}
vInt := 2
Field(&input, "MyInt", vInt)
if input.MyInt != vInt {
t.Errorf("MyInt: %d, want: %d", input.MyInt, vInt)
}
err := Field(&input, "MyInt", "vInt")
if err == nil {
t.Error("expect error, when wrong value-Type")
}
err = Field(&input, "NoField", "jjj")
if err == nil {
t.Error("expect error, when field not exists")
}
}