增加简版数据过滤,未测试

This commit is contained in:
白茶清欢 2021-03-10 18:58:21 +08:00
parent 498f3ec7c4
commit dc943d4549
3 changed files with 40 additions and 0 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

37
safe/data.go Normal file
View File

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