增加字段是否默认零值的判断

This commit is contained in:
白茶清欢 2023-02-02 12:18:29 +08:00
parent 1e0c756917
commit a30861a898
4 changed files with 134 additions and 0 deletions

View File

@ -89,5 +89,6 @@ type StructField struct {
type StructInfo struct {
Flag string // 结构体标识
IsStruct bool // 是否为结构体
IsStructPtr bool // 是否为结构体指针
StructFieldList []*StructField // 结构体字段列表
}

19
go.mod Normal file
View File

@ -0,0 +1,19 @@
module git.zhangdeman.cn/zhangdeman/event
go 1.17
require git.zhangdeman.cn/zhangdeman/util v0.0.0-20230113095943-b4b3e261e0c4
require (
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ini/ini v1.66.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

33
go.sum Normal file
View File

@ -0,0 +1,33 @@
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230113095943-b4b3e261e0c4 h1:1WclY9P8l8o/NZ3ZR/mupm8LtowjQ/Q4UNGXR32f0OQ=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230113095943-b4b3e261e0c4/go.mod h1:zTir/0IWdK3E7n0GiaogyWHADAQnBtTdl2I6Z2/OPqw=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k=
github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -8,6 +8,8 @@
package event
import (
"git.zhangdeman.cn/zhangdeman/event/abstract"
"git.zhangdeman.cn/zhangdeman/util"
"reflect"
"strings"
"sync"
@ -55,10 +57,13 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo {
res := &StructInfo{
Flag: dataFlag,
IsStruct: false,
IsStructPtr: false,
StructFieldList: make([]*StructField, 0),
}
isPtr := false
reflectType := reflect.TypeOf(data)
if reflectType.Kind() == reflect.Ptr {
isPtr = true
reflectType = reflectType.Elem()
}
if reflectType.Kind() != reflect.Struct {
@ -67,6 +72,7 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo {
return res
}
res.IsStruct = true
res.IsStructPtr = isPtr
for idx := 0; idx < reflectType.NumField(); idx++ {
field := &StructField{
Idx: idx,
@ -208,3 +214,78 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
}
}
}
// ReflectValue 反射值的实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/2/2
type ReflectValue struct {
}
// Do 通过反射机制对data进行数据填充此逻辑要求 data 必须是结构体或者结构体指针
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:52 2023/2/2
func (rv *ReflectValue) Do(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler) {
structInfo := ReflectTypeInstance.Do(dataFlag, data)
if !structInfo.IsStruct {
return
}
reflectValue := reflect.ValueOf(data)
if structInfo.IsStructPtr {
reflectValue = reflectValue.Elem()
}
for _, fieldInfo := range structInfo.StructFieldList {
if !rv.isZeroInputFieldValue(reflectValue, fieldInfo) {
// 不是零值, 无需处理
continue
}
}
}
// isZeroInputFieldValue 判断对应的字段是否为对应类型默认的零值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:04 2023/2/2
func (rv *ReflectValue) isZeroInputFieldValue(reflectValue reflect.Value, fieldInfo *StructField) bool {
inputVal := reflectValue.Field(fieldInfo.Idx).Interface()
switch fieldInfo.Type {
case reflect.Float64:
var f float64
if err := util.ConvertAssign(&f, inputVal); nil == err {
if f != 0 {
return false
}
return true
}
case reflect.String:
var s string
if err := util.ConvertAssign(&s, inputVal); nil == err {
if len(s) > 0 {
return false
}
return true
}
case reflect.Int64:
var i int64
if err := util.ConvertAssign(&i, inputVal); nil == err {
if i != 0 {
return false
}
return true
}
case reflect.Interface:
if inputVal == nil {
return true
}
return false
default:
// 默认不处理
return false
}
return false
}