Skip to content

Commit

Permalink
第10次优化:451 ms
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Feb 4, 2024
1 parent 0e2b4e6 commit b2c8744
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions configure/parseConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func parseString(configRefVal reflect.Value, configString string) reflect.Value
s, exists := configMap[fieldName]
if exists {
value := parse.ConvertValue(s, configRefType.Field(i).Type)
configRefVal.Field(i).Set(value)
configRefVal.Field(i).Set(reflect.ValueOf(value))
}
}
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func ParseConfig[TConfig any](key string) TConfig {
func parseConfig(configType reflect.Type, node any) reflect.Value {
// 字段是基础类型
if types.IsGoBasicType(configType) {
return parse.ConvertValue(node, configType)
return reflect.ValueOf(parse.ConvertValue(node, configType))
}

configFieldValue := reflect.New(configType).Elem()
Expand Down
10 changes: 3 additions & 7 deletions fastReflect/typeMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func (receiver *TypeMeta) parseType() {
receiver.Type = Map
receiver.MapType = receiver.ReflectType
// key type
keyType := receiver.MapType.Key()
keyVal := reflect.New(keyType).Elem()
receiver.keyHashCode = PointerOfValue(keyVal).HashCode
//keyType := receiver.MapType.Key()
//keyVal := reflect.New(keyType).Elem()
//receiver.keyHashCode = PointerOfValue(keyVal).HashCode

// value type
receiver.setItemHashCode(receiver.MapType.Elem())
Expand Down Expand Up @@ -219,10 +219,6 @@ func (receiver *TypeMeta) setItemHashCode(itemType reflect.Type) {
}
}

func (receiver *TypeMeta) GetKeyMeta() *TypeMeta {
return cacheTyp[receiver.keyHashCode]
}

func (receiver *TypeMeta) GetItemMeta() *TypeMeta {
return cacheTyp[receiver.itemHashCode]
}
10 changes: 5 additions & 5 deletions parse/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
var layouts = []string{"2006-01-02 15:04:05", "2006-01-02", "2006-01-02T15:04:05Z07:00"}

// ConvertValue 通用的类型转换
func ConvertValue(source any, defValType reflect.Type) reflect.Value {
func ConvertValue(source any, defValType reflect.Type) any {
// any类型,则直接返回
if defValType == nil || defValType.Kind() == reflect.Interface {
return reflect.ValueOf(source)
return source
}

defVal := reflect.New(defValType).Elem().Interface()
val := Convert(source, defVal)
return reflect.ValueOf(val)
return val
}

// Convert 通用的类型转换
Expand Down Expand Up @@ -129,7 +129,7 @@ func Convert[T any](source any, defVal T) T {
arr := strings.Split(strSource, ",")
itemMeta := defValMeta.GetItemMeta()
for i := 0; i < len(arr); i++ {
types.ListAddValue(lstReflectValue, ConvertValue(arr[i], itemMeta.ReflectType))
types.ListAddValue(lstReflectValue, reflect.ValueOf(ConvertValue(arr[i], itemMeta.ReflectType)))
}
val := lstReflectValue.Elem().Interface()
//return *(*T)(unsafe.Pointer(&val))
Expand Down Expand Up @@ -200,7 +200,7 @@ func Convert[T any](source any, defVal T) T {
for i := 0; i < arrSource.Len(); i++ {
item := arrSource.Index(i)
destVal := ConvertValue(item.Interface(), itemMeta.ReflectType)
arr = reflect.Append(arr, destVal)
arr = reflect.Append(arr, reflect.ValueOf(destVal))
}
return arr.Interface().(T)
}
Expand Down
2 changes: 1 addition & 1 deletion test/parse_convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestConvert(t *testing.T) {
assert.Equal(t, 123, parse.ConvertValue("123", reflect.TypeOf(1)).Interface().(int))
assert.Equal(t, 123, parse.ConvertValue("123", reflect.TypeOf(1)).(int))
assert.Equal(t, 1, parse.Convert(1, 0))
assert.Equal(t, int64(1), parse.Convert(1, int64(0)))
assert.Equal(t, "1", parse.Convert(1, ""))
Expand Down
22 changes: 14 additions & 8 deletions types/listType.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ func ListAdd(lstValue reflect.Value, item any) {

// ListAddValue 动态添加元素
func ListAddValue(lstValue reflect.Value, itemValue reflect.Value) {
key := lstValue.String() + ".Add"

if _, isExists := Cache[key]; !isExists {
method, _ := lstValue.Type().MethodByName("Add")
Cache[key] = []int{method.Index}
}
method := GetAddMethod(lstValue)

if itemValue.Kind() == reflect.Ptr {
itemValue = itemValue.Elem()
}
if itemValue.Kind() == reflect.Slice {
lstValue.Method(Cache[key][0]).CallSlice([]reflect.Value{itemValue})
method.CallSlice([]reflect.Value{itemValue})
} else {
lstValue.Method(Cache[key][0]).Call([]reflect.Value{itemValue})
method.Call([]reflect.Value{itemValue})
}
}

// GetAddMethod 获取动态添加元素的Method
func GetAddMethod(lstValue reflect.Value) reflect.Value {
// 初始化反射Method
key := lstValue.String() + ".Add"
if _, isExists := Cache[key]; !isExists {
method, _ := lstValue.Type().MethodByName("Add")
Cache[key] = []int{method.Index}
}
return lstValue.Method(Cache[key][0])
}

// GetListItemArrayType 获取List的原始数组类型
Expand Down

0 comments on commit b2c8744

Please sign in to comment.