From 09d4dffcab3060e819bae6acee62ff7b78a6ee66 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Fri, 26 Jan 2024 00:38:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BC=98=E5=8C=96=E5=B8=A6?= =?UTF-8?q?=E6=9D=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parse/convert.go | 36 +++++++++++++++++++++++----------- test/container_method_test.go | 4 +++- testBenchmark/parse_bm_test.go | 4 ++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/parse/convert.go b/parse/convert.go index 54535f9a7..062b7a95c 100644 --- a/parse/convert.go +++ b/parse/convert.go @@ -50,12 +50,14 @@ func Convert[T any](source any, defVal T) T { return toEnum[T](defValMeta.ReflectType, source) // 转字符串 case "string": - var str = NumberToString(source, sourceMeta.Kind) - return *(*T)(unsafe.Pointer(&str)) + var result any = NumberToString(source, sourceMeta.Kind) + return result.(T) + //return *(*T)(unsafe.Pointer(&str)) // 转bool case "bool": - result := EqualTo1(source, sourceMeta.Kind) - return *(*T)(unsafe.Pointer(&result)) + var result any = EqualTo1(source, sourceMeta.Kind) + //return *(*T)(unsafe.Pointer(&result)) + return result.(T) } } @@ -104,21 +106,21 @@ func Convert[T any](source any, defVal T) T { switch len(strSource) { case 19: if parse, err := time.ParseInLocation(layouts[0], strSource, time.Local); err == nil { - return *(*T)(unsafe.Pointer(&parse)) + return toTime[T](defValMeta, parse) } case 10: if parse, err := time.ParseInLocation(layouts[1], strSource, time.Local); err == nil { - return *(*T)(unsafe.Pointer(&parse)) + return toTime[T](defValMeta, parse) } case 25: if parse, err := time.ParseInLocation(layouts[2], strSource, time.Local); err == nil { - return *(*T)(unsafe.Pointer(&parse)) + return toTime[T](defValMeta, parse) } } for _, layout := range layouts { if parse, err := time.ParseInLocation(layout, strSource, time.Local); err == nil { - return *(*T)(unsafe.Pointer(&parse)) + return toTime[T](defValMeta, parse) } } // list类型 @@ -129,7 +131,8 @@ func Convert[T any](source any, defVal T) T { types.ListAdd(lstReflectValue, ConvertValue(arr[i], defValMeta.ItemMeta.ReflectType).Interface()) } val := lstReflectValue.Elem().Interface() - return *(*T)(unsafe.Pointer(&val)) + //return *(*T)(unsafe.Pointer(&val)) + return val.(T) } } @@ -182,8 +185,9 @@ func Convert[T any](source any, defVal T) T { return source.(T) // 转string case "string": - var str = source.(dateTime.DateTime).ToString("yyyy-MM-dd HH:mm:ss") - return *(*T)(unsafe.Pointer(&str)) + var result any = source.(dateTime.DateTime).ToString("yyyy-MM-dd HH:mm:ss") + //return *(*T)(unsafe.Pointer(&str)) + return result.(T) } } @@ -202,6 +206,16 @@ func Convert[T any](source any, defVal T) T { return defVal } +func toTime[T any](defValMeta fastReflect.ValueMeta, parse time.Time) T { + switch defValMeta.TypeIdentity { + case "time": + return any(parse).(T) + case "dateTime": + return any(dateTime.New(parse)).(T) + } + return *(*T)(unsafe.Pointer(&parse)) +} + // 转枚举 func toEnum[T any](tType reflect.Type, result any) T { returnTypeNew := reflect.New(tType).Elem() diff --git a/test/container_method_test.go b/test/container_method_test.go index d7c81cb2e..298e64d25 100644 --- a/test/container_method_test.go +++ b/test/container_method_test.go @@ -1,7 +1,9 @@ package test import ( + "github.com/farseer-go/fs" "github.com/farseer-go/fs/container" + "github.com/farseer-go/fs/modules" "github.com/stretchr/testify/assert" "testing" ) @@ -18,7 +20,7 @@ func (d *databaseFactory) CreateDatabase() IDatabase { } func TestContainerMethod(t *testing.T) { - //fs.Initialize[modules.FarseerKernelModule]("unit test") + fs.Initialize[modules.FarseerKernelModule]("unit test") container.Remove[IDatabase]() // 注册获取IDatabase接口的方法 diff --git a/testBenchmark/parse_bm_test.go b/testBenchmark/parse_bm_test.go index 20b026d4d..fe78e694b 100644 --- a/testBenchmark/parse_bm_test.go +++ b/testBenchmark/parse_bm_test.go @@ -7,8 +7,8 @@ import ( "time" ) -// BenchmarkConvertAll-12 446144 2876 ns/op 656 B/op 30 allocs/op -// BenchmarkConvertAll-12 735838 1669 ns/op 344 B/op 14 allocs/op +// BenchmarkConvertAll-12 446144 2876 ns/op 656 B/op 30 allocs/op +// BenchmarkConvertAll-12 767450 1527 ns/op 424 B/op 18 allocs/op func BenchmarkConvertAll(b *testing.B) { b.ReportAllocs() timeNow := time.Now()