From dc943d45490f0c1897c503352b4233e7bfc4a247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Wed, 10 Mar 2021 18:58:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AE=80=E7=89=88=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=BF=87=E6=BB=A4,=E6=9C=AA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ safe/data.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 safe/data.go diff --git a/go.mod b/go.mod index c38de97..a06a6d4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/go-developer/gopkg go 1.15 require ( + github.com/buger/jsonparser v1.1.1 // indirect github.com/gin-gonic/gin v1.6.3 github.com/go-redis/redis/v8 v8.6.0 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible diff --git a/go.sum b/go.sum index 7822e72..1e59407 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/safe/data.go b/safe/data.go new file mode 100644 index 0000000..6d167e2 --- /dev/null +++ b/safe/data.go @@ -0,0 +1,37 @@ +// Package safe ... +// +// Description : 按需返回对外暴露的字段 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-10 6:38 下午 +package safe + +import ( + "strings" + + "github.com/buger/jsonparser" +) + +// Filter 按需输出数据 +// +// Author : go_developer@163.com<张德满> +// +// Date : 6:40 下午 2021/3/10 +func Filter(source []byte, filter []string) ([]byte, error) { + var ( + bt []byte + setErr error + ) + for _, item := range filter { + fieldList := strings.Split(item, ",") + val, _, _, err := jsonparser.Get(source, fieldList...) + if nil != err { + return nil, err + } + if bt, setErr = jsonparser.Set(bt, val, fieldList...); nil != setErr { + return nil, setErr + } + } + return bt, nil +}