Skip to content

Commit

Permalink
第6次优化
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jan 30, 2024
1 parent 2f70791 commit 9f52fab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
12 changes: 8 additions & 4 deletions fastReflect/typeMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// TypeMeta 类型元数据
type TypeMeta struct {
Name string // 字段名称
//Name string // 字段名称
ReflectType reflect.Type // 字段类型
ReflectTypeString string // 类型
Type FieldType // 集合类型
Expand Down Expand Up @@ -67,7 +67,7 @@ func (receiver *TypeMeta) parseType() {
//}

receiver.ZeroValue = reflect.New(receiver.ReflectType).Elem().Interface()
receiver.Name = receiver.ReflectType.Name()
//receiver.Name = receiver.ReflectType.Name()
receiver.ReflectTypeString = receiver.ReflectType.String()

switch receiver.Kind {
Expand Down Expand Up @@ -130,8 +130,12 @@ func (receiver *TypeMeta) parseType() {
if _, isTrue := types.IsListByType(receiver.ReflectType); isTrue {
receiver.Type = List
itemType := types.GetListItemType(receiver.ReflectType)
itemVal := reflect.New(itemType).Elem().Interface()
receiver.ItemMeta = PointerOf(itemVal).TypeMeta
if itemType.Kind() != reflect.Interface {
itemVal := reflect.New(itemType).Elem().Interface()
receiver.ItemMeta = PointerOf(itemVal).TypeMeta
} else {
receiver.ItemMeta = typeOf(itemType, nil)
}
receiver.SliceType = reflect.SliceOf(receiver.ItemMeta.ReflectType)
break
}
Expand Down
2 changes: 1 addition & 1 deletion fastReflect/valueMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type PointerMeta struct {

// 字段为any类型,当为nil时无法获取,需手动设置类型
var anyNil = &TypeMeta{
Name: "interface {}",
//Name: "interface {}",
ReflectType: nil,
ReflectTypeString: "interface {}",
Type: Interface,
Expand Down
7 changes: 5 additions & 2 deletions types/isType.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

const (
ListTypeString = "collections.List["
ListAnyTypeString = "collections.ListAny"
DictionaryTypeString = "collections.Dictionary["
PageListTypeString = "collections.PageList["
CollectionsTypeString = "github.com/farseer-go/collections"
Expand Down Expand Up @@ -34,12 +35,14 @@ func IsMap(val reflect.Value) (reflect.Type, bool) {
// IsList 判断类型是否为List
func IsList(val reflect.Value) (reflect.Type, bool) {
realType := GetRealType(val)
return realType, strings.HasPrefix(realType.String(), ListTypeString)
realTypeString := realType.String()
return realType, strings.HasPrefix(realTypeString, ListTypeString) || realTypeString == ListAnyTypeString
}

// IsListByType 判断类型是否为List
func IsListByType(realType reflect.Type) (reflect.Type, bool) {
return realType, strings.HasPrefix(realType.String(), ListTypeString)
realTypeString := realType.String()
return realType, strings.HasPrefix(realTypeString, ListTypeString) || realTypeString == ListAnyTypeString
}

// IsDictionary 判断类型是否为Dictionary
Expand Down

0 comments on commit 9f52fab

Please sign in to comment.