Skip to content

Commit 77b909a

Browse files
committed
增加[]uint8转string
1 parent 6ac7948 commit 77b909a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

fastReflect/typeMeta.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package fastReflect
22

33
import (
4-
"github.com/farseer-go/fs/types"
54
"reflect"
65
"strings"
6+
7+
"github.com/farseer-go/fs/types"
78
)
89

910
// TypeMeta 类型元数据
@@ -34,6 +35,7 @@ type TypeMeta struct {
3435
IsSliceOrArray bool // 是否切片或数组类型
3536
IsStruct bool // 是否结构体
3637
IsMap bool // 是否字典
38+
IsUint8 bool // 是否[]uint8
3739
HashCode uint32 // 每个类型的HashCode都是唯一的
3840
Size uintptr // 内存占用大小
3941
}
@@ -89,7 +91,7 @@ func (receiver *TypeMeta) parseType() {
8991
receiver.IsSliceOrArray = true
9092
receiver.Type = Slice
9193
receiver.SliceType = receiver.ReflectType
92-
94+
receiver.IsUint8 = receiver.ReflectTypeString == "[]uint8"
9395
receiver.setItemHashCode(receiver.ReflectType.Elem())
9496
case reflect.Array:
9597
receiver.IsSliceOrArray = true

parse/convert.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,16 @@ func Convert[T any](source any, defVal T) T {
216216
return result.(T)
217217
}
218218
}
219-
219+
// []uint8转string
220+
if sourceMeta.IsUint8 {
221+
switch defValMeta.TypeIdentity {
222+
case "string":
223+
if s, ok := source.([]uint8); ok {
224+
var result any = string(s)
225+
return result.(T)
226+
}
227+
}
228+
}
220229
// 切片转切片
221230
if sourceMeta.Type == fastReflect.Slice && defValMeta.Type == fastReflect.Slice {
222231
arr := reflect.MakeSlice(defValMeta.ReflectType, 0, 0)

0 commit comments

Comments
 (0)