Skip to content

Commit 40080c2

Browse files
ddzrcxixi
authored and
xixi
committed
feat:add map and array filter code
1 parent 8e8cdea commit 40080c2

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

internal/encoder/code.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,13 @@ func (c *SliceCode) ToOpcode(ctx *compileContext) Opcodes {
271271
return Opcodes{header}.Add(codes...).Add(elemCode).Add(end)
272272
}
273273

274-
func (c *SliceCode) Filter(_ *FieldQuery) Code {
274+
func (c *SliceCode) Filter(fieldQuery *FieldQuery) Code {
275+
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
276+
return &SliceCode{
277+
value: c.value.Filter(fieldQuery.Fields[0]),
278+
typ: c.typ,
279+
}
280+
}
275281
return c
276282
}
277283

@@ -316,7 +322,13 @@ func (c *ArrayCode) ToOpcode(ctx *compileContext) Opcodes {
316322
return Opcodes{header}.Add(codes...).Add(elemCode).Add(end)
317323
}
318324

319-
func (c *ArrayCode) Filter(_ *FieldQuery) Code {
325+
func (c *ArrayCode) Filter(fieldQuery *FieldQuery) Code {
326+
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
327+
return &ArrayCode{
328+
value: c.value.Filter(fieldQuery.Fields[0]),
329+
typ: c.typ,
330+
}
331+
}
320332
return c
321333
}
322334

@@ -366,7 +378,14 @@ func (c *MapCode) ToOpcode(ctx *compileContext) Opcodes {
366378
return Opcodes{header}.Add(keyCodes...).Add(value).Add(valueCodes...).Add(key).Add(end)
367379
}
368380

369-
func (c *MapCode) Filter(_ *FieldQuery) Code {
381+
func (c *MapCode) Filter(fieldQuery *FieldQuery) Code {
382+
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
383+
return &MapCode{
384+
value: c.value.Filter(fieldQuery.Fields[0]),
385+
typ: c.typ,
386+
key: c.key,
387+
}
388+
}
370389
return c
371390
}
372391

internal/encoder/vm/vm.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

query_test.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ type queryTestZ struct {
2828
ZA string
2929
ZB bool
3030
ZC int
31+
ZD []queryTestW
32+
ZE map[string]queryTestV
33+
}
34+
35+
type queryTestW struct {
36+
WA string
37+
WB bool
38+
WC int
39+
}
40+
41+
type queryTestV struct {
42+
VA string
43+
VB bool
44+
VC int
3145
}
3246

3347
func (z *queryTestZ) MarshalJSON(ctx context.Context) ([]byte, error) {
@@ -45,6 +59,10 @@ func TestFieldQuery(t *testing.T) {
4559
json.BuildSubFieldQuery("YC").Fields(
4660
"ZA",
4761
"ZB",
62+
json.BuildSubFieldQuery("ZD").Fields(json.BuildSubFieldQuery("#").Fields(
63+
"WA", "WC")),
64+
json.BuildSubFieldQuery("ZE").Fields(json.BuildSubFieldQuery("#").Fields(
65+
"VA", "VC")),
4866
),
4967
),
5068
)
@@ -77,6 +95,38 @@ func TestFieldQuery(t *testing.T) {
7795
{
7896
Name: "ZB",
7997
},
98+
{
99+
Name: "ZD",
100+
Fields: []*json.FieldQuery{
101+
{
102+
Name: "#",
103+
Fields: []*json.FieldQuery{
104+
{
105+
Name: "WA",
106+
},
107+
{
108+
Name: "WC",
109+
},
110+
},
111+
},
112+
},
113+
},
114+
{
115+
Name: "ZE",
116+
Fields: []*json.FieldQuery{
117+
{
118+
Name: "#",
119+
Fields: []*json.FieldQuery{
120+
{
121+
Name: "VA",
122+
},
123+
{
124+
Name: "VC",
125+
},
126+
},
127+
},
128+
},
129+
},
80130
},
81131
},
82132
},
@@ -89,7 +139,7 @@ func TestFieldQuery(t *testing.T) {
89139
if err != nil {
90140
t.Fatal(err)
91141
}
92-
if queryStr != `["XA","XB",{"XC":["YA","YB",{"YC":["ZA","ZB"]}]}]` {
142+
if queryStr != `["XA","XB",{"XC":["YA","YB",{"YC":["ZA","ZB",{"ZD":[{"#":["WA","WC"]}]},{"ZE":[{"#":["VA","VC"]}]}]}]}]` {
93143
t.Fatalf("failed to create query string. %s", queryStr)
94144
}
95145
ctx := json.SetFieldQueryToContext(context.Background(), query)
@@ -103,6 +153,14 @@ func TestFieldQuery(t *testing.T) {
103153
ZA: "za",
104154
ZB: true,
105155
ZC: 3,
156+
ZD: []queryTestW{
157+
{WA: "wa1", WB: true, WC: 1},
158+
{WA: "wa2", WB: true, WC: 1},
159+
},
160+
ZE: map[string]queryTestV{
161+
"key1": {VA: "va1", VB: true, VC: 1},
162+
"key2": {VA: "va2", VB: true, VC: 1},
163+
},
106164
},
107165
YD: true,
108166
YE: 4,
@@ -113,7 +171,7 @@ func TestFieldQuery(t *testing.T) {
113171
if err != nil {
114172
t.Fatal(err)
115173
}
116-
expected := `{"XA":1,"XB":"xb","XC":{"YA":2,"YB":"yb","YC":{"ZA":"za","ZB":true}}}`
174+
expected := `{"XA":1,"XB":"xb","XC":{"YA":2,"YB":"yb","YC":{"ZA":"za","ZB":true,"ZD":[{"WA":"wa1","WC":1},{"WA":"wa2","WC":1}],"ZE":{"key1":{"VA":"va1","VC":1},"key2":{"VA":"va2","VC":1}}}}}`
117175
got := string(b)
118176
if expected != got {
119177
t.Fatalf("failed to encode with field query: expected %q but got %q", expected, got)

0 commit comments

Comments
 (0)