Skip to content

Commit a292e9d

Browse files
committed
增加Select和Omit方法,简化过滤操作
1 parent 67d0b98 commit a292e9d

File tree

7 files changed

+60
-31
lines changed

7 files changed

+60
-31
lines changed

filter/filter.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func (f Filter) MarshalJSON() ([]byte, error) {
88
return f.node.Bytes()
99
}
1010

11-
//Deprecated
11+
// Deprecated
1212
func (f Filter) MastMarshalJSON() []byte {
1313
return f.node.MustBytes()
1414
}
@@ -52,6 +52,16 @@ func SelectMarshal(selectScene string, el interface{}) Filter {
5252
}
5353
}
5454

55+
// Select 直接返回过滤后的数据结构,相当于直接SelectMarshal后再调用Interface方法
56+
func Select(selectScene string, el interface{}) interface{} {
57+
return SelectMarshal(selectScene, el).Interface()
58+
}
59+
60+
// Omit 直接返回过滤后的数据结构,相当于直接OmitMarshal后再调用Interface方法
61+
func Omit(omitScene string, el interface{}) interface{} {
62+
return OmitMarshal(omitScene, el).Interface()
63+
}
64+
5565
// OmitMarshal 第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
5666
func OmitMarshal(omitScene string, el interface{}) Filter {
5767
tree := &fieldNodeTree{

filter/filter_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package filter
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"testing"
67
"time"
@@ -124,8 +125,8 @@ type Child struct {
124125
CAge int `json:"c_age,select(all|struct)"`
125126
}
126127
type UsersCase struct {
127-
Name string `json:"name,select(all|1)"`
128-
Age int `json:"age,select(all|2)"`
128+
Name string `json:"name,select(all|1),omit(1)"`
129+
Age int `json:"age,select(all|2),omit(1)"`
129130
Struct Child `json:"struct,select(all|struct)"`
130131
}
131132

@@ -390,6 +391,24 @@ func TestTestCases(t *testing.T) {
390391
fmt.Println(filter.MustJSON())
391392
//{"int":100,"int16":16,"int16_p":16,"int32":32,"int32_p":32,"int64":64,"int64_p":64,"int8":8,"int8_p":8,"int_p":100}
392393
})
394+
t.Run("intAllSelect", func(t *testing.T) {
395+
filter := Select("intAll", NewTestCases())
396+
marshal, err := json.Marshal(filter)
397+
if err != nil {
398+
t.Error("err", err)
399+
}
400+
fmt.Println(string(marshal))
401+
//{"int":100,"int16":16,"int16_p":16,"int32":32,"int32_p":32,"int64":64,"int64_p":64,"int8":8,"int8_p":8,"int_p":100}
402+
})
403+
t.Run("intAllOmit", func(t *testing.T) {
404+
filter := Omit("1", UsersCase{})
405+
marshal, err := json.Marshal(filter)
406+
if err != nil {
407+
t.Error("err", err)
408+
}
409+
fmt.Println(string(marshal))
410+
//{"int":100,"int16":16,"int16_p":16,"int32":32,"int32_p":32,"int64":64,"int64_p":64,"int8":8,"int8_p":8,"int_p":100}
411+
})
393412

394413
t.Run("sliceAll", func(t *testing.T) {
395414
filter := SelectMarshal("sliceAll", NewTestCases())

filter/node_decode.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type fieldNodeTree struct {
1111
IsAnonymous bool //是否是匿名结构体,内嵌结构体,需要把所有字段展开
1212
IsNil bool //该字段值是否为nil
1313
ParentNode *fieldNodeTree //父节点指针,根节点为nil,
14-
ChildNodes []*fieldNodeTree //如果是struct则保存所有字段名和值的指针,如果是切片就保存切片的所有值
14+
Children []*fieldNodeTree //如果是struct则保存所有字段名和值的指针,如果是切片就保存切片的所有值
1515
}
1616

1717
func (t *fieldNodeTree) GetValue() (val interface{}, ok bool) {
@@ -22,21 +22,21 @@ func (t *fieldNodeTree) GetValue() (val interface{}, ok bool) {
2222
if t.IsNil {
2323
return nil, true
2424
}
25-
if t.ChildNodes == nil {
25+
if t.Children == nil {
2626
return t.Val, true
2727
}
2828
if t.IsSlice { //为切片和数组时候key为空
29-
slices := make([]interface{}, 0, len(t.ChildNodes))
30-
for i := 0; i < len(t.ChildNodes); i++ {
31-
value, ok0 := t.ChildNodes[i].GetValue()
29+
slices := make([]interface{}, 0, len(t.Children))
30+
for i := 0; i < len(t.Children); i++ {
31+
value, ok0 := t.Children[i].GetValue()
3232
if ok0 {
3333
slices = append(slices, value)
3434
}
3535
}
3636
return slices, true
3737
}
3838
maps := make(map[string]interface{})
39-
for _, v := range t.ChildNodes {
39+
for _, v := range t.Children {
4040
value, ok1 := v.GetValue()
4141
if ok1 {
4242
maps[v.Key] = value
@@ -47,18 +47,18 @@ func (t *fieldNodeTree) GetValue() (val interface{}, ok bool) {
4747

4848
func (t *fieldNodeTree) Map() map[string]interface{} {
4949
maps := make(map[string]interface{})
50-
for _, v := range t.ChildNodes {
51-
value, ok := (*v).GetValue()
50+
for _, v := range t.Children {
51+
value, ok := v.GetValue()
5252
if ok {
53-
maps[(*v).Key] = value
53+
maps[v.Key] = value
5454
}
5555
}
5656
return maps
5757
}
5858
func (t *fieldNodeTree) Slice() interface{} {
59-
slices := make([]interface{}, 0, len(t.ChildNodes))
60-
for i := 0; i < len(t.ChildNodes); i++ {
61-
v, ok := t.ChildNodes[i].GetValue()
59+
slices := make([]interface{}, 0, len(t.Children))
60+
for i := 0; i < len(t.Children); i++ {
61+
v, ok := t.Children[i].GetValue()
6262
if ok {
6363
slices = append(slices, v)
6464
}
@@ -75,10 +75,10 @@ func (t *fieldNodeTree) Marshal() interface{} {
7575
}
7676

7777
func (t *fieldNodeTree) AddChild(tree *fieldNodeTree) *fieldNodeTree {
78-
if t.ChildNodes == nil {
79-
t.ChildNodes = make([]*fieldNodeTree, 0, 3)
78+
if t.Children == nil {
79+
t.Children = make([]*fieldNodeTree, 0, 3)
8080
}
81-
t.ChildNodes = append(t.ChildNodes, tree)
81+
t.Children = append(t.Children, tree)
8282
return t
8383
}
8484

filter/omit_encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TakePointerValue: //取指针的值
4141
var isAnonymous bool
4242
for i := 0; i < typeOf.NumField(); i++ {
4343
jsonTag, ok := typeOf.Field(i).Tag.Lookup("json")
44-
var tag Tag
44+
var tag tag
4545
if !ok {
4646
tag = newOmitNotTag(omitScene, typeOf.Field(i).Name)
4747
isAnonymous = typeOf.Field(i).Anonymous
@@ -98,7 +98,7 @@ TakePointerValue: //取指针的值
9898
t.AddChild(tree)
9999
}
100100
}
101-
if t.ChildNodes == nil && !t.IsAnonymous {
101+
if t.Children == nil && !t.IsAnonymous {
102102
//t.Val = struct{}{} //这样表示返回{}
103103

104104
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段

filter/select_encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TakePointerValue: //取指针的值
9898
t.AddChild(tree)
9999
}
100100
}
101-
if t.ChildNodes == nil && !t.IsAnonymous {
101+
if t.Children == nil && !t.IsAnonymous {
102102
//t.Val = struct{}{} //这样表示返回{}
103103

104104
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段

filter/tag.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const (
99
//empty ="$empty"
1010
)
1111

12-
type Tag struct {
12+
type tag struct {
1313
//执行的场景
1414
SelectScene string
1515
//该字段是否需要被忽略?
@@ -24,13 +24,13 @@ type Tag struct {
2424
Omitempty bool
2525
}
2626

27-
func newSelectTag(tag, selectScene, fieldName string) Tag {
27+
func newSelectTag(tagStr, selectScene, fieldName string) tag {
2828

29-
tagEl := Tag{
29+
tagEl := tag{
3030
SelectScene: selectScene,
3131
IsOmitField: true,
3232
}
33-
tags := strings.Split(tag, ",")
33+
tags := strings.Split(tagStr, ",")
3434
tagEl.UseFieldName = fieldName
3535

3636
if len(tags) < 2 {
@@ -63,13 +63,13 @@ func newSelectTag(tag, selectScene, fieldName string) Tag {
6363
return tagEl
6464
}
6565

66-
func newOmitTag(tag, omitScene, fieldName string) Tag {
67-
tagEl := Tag{
66+
func newOmitTag(tagStr, omitScene, fieldName string) tag {
67+
tagEl := tag{
6868
SelectScene: omitScene,
6969
IsOmitField: false,
7070
IsSelect: true,
7171
}
72-
tags := strings.Split(tag, ",")
72+
tags := strings.Split(tagStr, ",")
7373
tagEl.UseFieldName = fieldName
7474

7575
if len(tags) < 2 {
@@ -107,8 +107,8 @@ func newOmitTag(tag, omitScene, fieldName string) Tag {
107107
return tagEl
108108
}
109109

110-
func newOmitNotTag(omitScene, fieldName string) Tag {
111-
return Tag{
110+
func newOmitNotTag(omitScene, fieldName string) tag {
111+
return tag{
112112
IsSelect: true,
113113
UseFieldName: fieldName,
114114
SelectScene: omitScene,

filter/time.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type Time time.Time
99

10-
const timeFmt = "2006-01-02 15:04:05"
10+
var timeFmt = "2006-01-02 15:04:05"
1111

1212
func (t Time) MarshalJSON() ([]byte, error) {
1313
fmtTime := time.Time(t)

0 commit comments

Comments
 (0)