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 +}