-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
146 lines (123 loc) · 3.05 KB
/
filter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package jsonutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
type Foo struct {
String string `json:"string"`
Skip string `json:"-"`
OmitEmpty string `json:"omit_empty,omitempty"`
OmitNotEmpty string `json:"omit_notempty,omitempty"`
Bar Bar `json:"bar"`
}
type Bar struct {
Int int `json:"int"`
Baz []Baz `json:"baz"`
}
type Baz struct {
Bool bool `json:"bool"`
Float64 float64 `json:"float64"`
}
func TestFilterMap(t *testing.T) {
source := map[string]interface{}{
"string": "abc",
"int": 123,
"map": map[string]interface{}{
"1": "a",
"2": "b",
"3": "c",
},
}
actual := FilterMap(source, []string{})
assert.Equal(t, source, actual)
expected := map[string]interface{}{
"string": "abc",
}
actual = FilterMap(source, []string{"string"})
assert.Equal(t, expected, actual)
expected = map[string]interface{}{
"string": "abc",
"map": map[string]interface{}{
"3": "c",
},
}
actual = FilterMap(source, []string{"string", "map.3"})
assert.Equal(t, expected, actual)
}
func TestFilterSlice(t *testing.T) {
foo := []Foo{{
String: "abc",
OmitNotEmpty: "def",
Bar: Bar{
Int: 123,
Baz: []Baz{
{Bool: true, Float64: 45.6},
{Bool: false, Float64: 78.9},
},
},
}}
expected := []map[string]interface{}{{
"string": foo[0].String,
"omit_notempty": foo[0].OmitNotEmpty,
"bar": map[string]interface{}{
"int": foo[0].Bar.Int,
"baz": []map[string]interface{}{
{"bool": foo[0].Bar.Baz[0].Bool, "float64": foo[0].Bar.Baz[0].Float64},
{"bool": foo[0].Bar.Baz[1].Bool, "float64": foo[0].Bar.Baz[1].Float64},
},
},
}}
actual := FilterSlice(foo, []string{})
assert.Equal(t, expected, actual)
}
func TestFilterStruct(t *testing.T) {
foo := Foo{
String: "abc",
OmitNotEmpty: "def",
Bar: Bar{
Int: 123,
Baz: []Baz{
{Bool: true, Float64: 45.6},
{Bool: false, Float64: 78.9},
},
},
}
// Test 1st level
expected := map[string]interface{}{
"string": foo.String,
"omit_notempty": foo.OmitNotEmpty,
"bar": map[string]interface{}{
"int": foo.Bar.Int,
"baz": []map[string]interface{}{
{"bool": foo.Bar.Baz[0].Bool, "float64": foo.Bar.Baz[0].Float64},
{"bool": foo.Bar.Baz[1].Bool, "float64": foo.Bar.Baz[1].Float64},
},
},
}
actual := FilterStruct(foo, []string{})
assert.Equal(t, expected, actual)
delete(expected, "omit_notempty")
actual = FilterStruct(foo, []string{"string", "bar"})
assert.Equal(t, expected, actual)
// Test 2nd level
expected = map[string]interface{}{
"string": foo.String,
"bar": map[string]interface{}{
"int": foo.Bar.Int,
},
}
actual = FilterStruct(foo, []string{"string", "bar.int"})
assert.Equal(t, expected, actual)
// Test 3rd level
expected = map[string]interface{}{
"string": foo.String,
"bar": map[string]interface{}{
"baz": []map[string]interface{}{
{"bool": foo.Bar.Baz[0].Bool},
{"bool": foo.Bar.Baz[1].Bool},
},
},
}
actual = FilterStruct(foo, []string{"string", "bar.baz.bool"})
assert.Equal(t, expected, actual)
}