basic conf for loader
This commit is contained in:
20
pkg/set/set.go
Normal file
20
pkg/set/set.go
Normal 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
30
pkg/set/set_test.go
Normal 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")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user