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
|
2021-08-06 20:13:02 +08:00
|
|
|
func Filter(source []byte, filter map[string]string) ([]byte, error) {
|
2021-03-10 18:58:21 +08:00
|
|
|
var (
|
|
|
|
bt []byte
|
|
|
|
setErr error
|
|
|
|
)
|
2021-08-06 20:13:02 +08:00
|
|
|
for result, 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
|
|
|
|
}
|
2021-08-06 20:13:02 +08:00
|
|
|
if bt, setErr = jsonparser.Set(bt, val, strings.Split(result, ".")...); nil != setErr {
|
2021-03-10 18:58:21 +08:00
|
|
|
return nil, setErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bt, nil
|
|
|
|
}
|