From b22753fbbb84843e9ed91da717cbcb8d3ef699f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 2 Feb 2023 12:24:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=8F=8D=E5=B0=84=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=98=AF=E5=90=A6=E6=8C=87=E9=92=88=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 1 + reflect.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/define.go b/define.go index e7fef2b..47d4216 100644 --- a/define.go +++ b/define.go @@ -76,6 +76,7 @@ type StructField struct { Idx int // 字段在结构体的索引 Name string // 字段名 Type reflect.Kind // 字段类型 + IsPtr bool // 是否为指针 JsonTag string // json标签 EventTag string // 事件标签 MappingRuleList []MappingRuleItem // 规则列表 diff --git a/reflect.go b/reflect.go index eb4d523..d67c24a 100644 --- a/reflect.go +++ b/reflect.go @@ -81,7 +81,7 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo { EventTag: reflectType.Field(idx).Tag.Get(OutEventTag), MappingRuleList: make([]MappingRuleItem, 0), } - rt.fillFieldType(field, reflectType.Field(idx).Type.Kind()) + rt.fillFieldType(field, reflectType.Field(idx).Type) rt.fillMappingRule(field, reflectType.Field(idx).Tag.Get(MappingTag)) res.StructFieldList = append(res.StructFieldList, field) } @@ -94,14 +94,20 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo { // Author : go_developer@163.com<白茶清欢> // // Date : 20:46 2023/2/1 -func (rt *ReflectType) fillFieldType(field *StructField, dataType reflect.Kind) { - switch dataType { +func (rt *ReflectType) fillFieldType(field *StructField, dataType reflect.Type) { + switch dataType.Kind() { case reflect.Float32: fallthrough case reflect.Float64: field.Type = reflect.Float64 case reflect.String: field.Type = reflect.String + case reflect.Struct: + field.Type = reflect.Struct + case reflect.Ptr: + field.IsPtr = true + // 指针再次判断基础类型 + field.Type = dataType.Elem().Kind() default: if strings.Contains(dataType.String(), "int") { field.Type = reflect.Int64 @@ -243,6 +249,7 @@ func (rv *ReflectValue) Do(dataFlag string, data interface{}, preSendHandler abs // 不是零值, 无需处理 continue } + // 是零值, 填充默认值 } }