增加数据过滤实现 #2

Merged
zhangdeman merged 1 commits from featuer/filter into master 2024-03-08 16:31:03 +08:00
2 changed files with 27 additions and 0 deletions

View File

@ -41,4 +41,6 @@ type EasyMap interface {
ToStruct(receiver interface{}) error
// ToString 转为字符串
ToString() string
// Filter 过滤数据
Filter(ignoreFieldList []string, rewriteFieldTable map[string]interface{}) map[string]interface{}
}

View File

@ -374,3 +374,28 @@ func (c *common) ToString() string {
mapData := c.GetAllForMapKeyString()
return serialize.JSON.MarshalForString(mapData)
}
// Filter 对数据进行过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2024/3/8
func (c *common) Filter(ignoreFieldList []string, rewriteFieldTable map[string]string) map[string]interface{} {
result := make(map[string]interface{})
ignoreFieldTable := make(map[string]bool)
for _, item := range ignoreFieldList {
ignoreFieldTable[item] = true
}
data := c.GetAllForMapKeyString()
for field, val := range data {
if _, exist := ignoreFieldTable[field]; exist {
// 忽略当前字段
continue
}
if rewriteField, exist := rewriteFieldTable[field]; exist && len(rewriteField) > 0 {
field = rewriteField
}
result[field] = val
}
return result
}