company facts simple read out of xls works

This commit is contained in:
Andreas Schröpfer
2021-02-28 08:33:00 +01:00
parent e0faa1fc7c
commit 90af0db862
11 changed files with 197 additions and 170 deletions

View File

@@ -3,16 +3,37 @@ package set
import (
"errors"
"reflect"
"strconv"
)
func Field(dst interface{}, fieldName string, value interface{}) error {
var (
ErrTypeConvert = errors.New("cannot convert input")
)
func Field(dst interface{}, fieldName string, value string) 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")
// try to convert
switch dstField.Kind() {
case reflect.Int:
d, err := strconv.Atoi(value)
if err != nil {
return ErrTypeConvert
}
val = reflect.ValueOf(d)
case reflect.Float64:
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return ErrTypeConvert
}
val = reflect.ValueOf(f)
default:
return ErrTypeConvert
}
}
dstField.Set(val)