Skip to content

Commit

Permalink
修复优化带来的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jan 25, 2024
1 parent 89919ae commit 09d4dff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
36 changes: 25 additions & 11 deletions parse/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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类型
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion test/container_method_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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接口的方法
Expand Down
4 changes: 2 additions & 2 deletions testBenchmark/parse_bm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 09d4dff

Please sign in to comment.