diff --git a/abstruct.go b/abstruct.go index e970f34..353600d 100644 --- a/abstruct.go +++ b/abstruct.go @@ -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{} } diff --git a/common.go b/common.go index d8ca79f..c0167e3 100644 --- a/common.go +++ b/common.go @@ -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 +}