2021-03-10 18:58:21 +08:00
|
|
|
// Package safe ...
|
|
|
|
//
|
|
|
|
// Description : 按需返回对外暴露的字段
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-10 18:58:21 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-10 6:38 下午
|
|
|
|
package safe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/buger/jsonparser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Filter 按需输出数据
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-10 18:58:21 +08:00
|
|
|
//
|
|
|
|
// Date : 6:40 下午 2021/3/10
|
|
|
|
func Filter(source []byte, filter []string) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
bt []byte
|
|
|
|
setErr error
|
|
|
|
)
|
|
|
|
for _, item := range filter {
|
2021-03-11 00:05:56 +08:00
|
|
|
fieldList := strings.Split(item, ".")
|
2021-03-10 18:58:21 +08:00
|
|
|
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
|
|
|
|
}
|