From d5d65c366b8fe47f1e1c3e407a2a2a59ef250e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 8 Mar 2024 16:30:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstruct.go | 2 ++ common.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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 +} -- 2.36.6