rename to mocksnow
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -66,3 +67,40 @@ func SleepWithContext(ctx context.Context, d time.Duration) {
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
|
||||
// Generic converter using reflection
|
||||
func ConvertStruct(src interface{}, dst interface{}) {
|
||||
srcVal := reflect.ValueOf(src)
|
||||
srcType := reflect.TypeOf(src)
|
||||
dstVal := reflect.ValueOf(dst).Elem()
|
||||
|
||||
for i := 0; i < srcVal.NumField(); i++ {
|
||||
srcField := srcVal.Field(i)
|
||||
dstField := dstVal.FieldByName(srcType.Field(i).Name)
|
||||
|
||||
if !dstField.IsValid() || !dstField.CanSet() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch srcField.Type().Name() {
|
||||
case "NullString":
|
||||
if srcField.FieldByName("Valid").Bool() {
|
||||
dstField.SetString(srcField.FieldByName("String").String())
|
||||
} else {
|
||||
dstField.SetString("")
|
||||
}
|
||||
case "NullTime":
|
||||
if srcField.FieldByName("Valid").Bool() {
|
||||
t := srcField.FieldByName("Time").Interface().(time.Time)
|
||||
dstField.SetString(t.Format("2006-01-02 15:04:05"))
|
||||
} else {
|
||||
dstField.SetString("")
|
||||
}
|
||||
default:
|
||||
// Handle int64 -> int conversion
|
||||
if srcField.Kind() == reflect.Int64 && dstField.Kind() == reflect.Int {
|
||||
dstField.SetInt(srcField.Int())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user