Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add map and array filter code #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions internal/encoder/code.go
Expand Up @@ -271,7 +271,13 @@ func (c *SliceCode) ToOpcode(ctx *compileContext) Opcodes {
return Opcodes{header}.Add(codes...).Add(elemCode).Add(end)
}

func (c *SliceCode) Filter(_ *FieldQuery) Code {
func (c *SliceCode) Filter(fieldQuery *FieldQuery) Code {
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
return &SliceCode{
value: c.value.Filter(fieldQuery.Fields[0]),
typ: c.typ,
}
}
return c
}

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

func (c *ArrayCode) Filter(_ *FieldQuery) Code {
func (c *ArrayCode) Filter(fieldQuery *FieldQuery) Code {
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
return &ArrayCode{
value: c.value.Filter(fieldQuery.Fields[0]),
typ: c.typ,
}
}
return c
}

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

func (c *MapCode) Filter(_ *FieldQuery) Code {
func (c *MapCode) Filter(fieldQuery *FieldQuery) Code {
if len(fieldQuery.Fields) > 0 && fieldQuery.Fields[0].Name == "#" {
return &MapCode{
value: c.value.Filter(fieldQuery.Fields[0]),
typ: c.typ,
key: c.key,
}
}
return c
}

Expand Down
3 changes: 3 additions & 0 deletions internal/encoder/vm/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 60 additions & 2 deletions query_test.go
Expand Up @@ -28,6 +28,20 @@ type queryTestZ struct {
ZA string
ZB bool
ZC int
ZD []queryTestW
ZE map[string]queryTestV
}

type queryTestW struct {
WA string
WB bool
WC int
}

type queryTestV struct {
VA string
VB bool
VC int
}

func (z *queryTestZ) MarshalJSON(ctx context.Context) ([]byte, error) {
Expand All @@ -45,6 +59,10 @@ func TestFieldQuery(t *testing.T) {
json.BuildSubFieldQuery("YC").Fields(
"ZA",
"ZB",
json.BuildSubFieldQuery("ZD").Fields(json.BuildSubFieldQuery("#").Fields(
"WA", "WC")),
json.BuildSubFieldQuery("ZE").Fields(json.BuildSubFieldQuery("#").Fields(
"VA", "VC")),
),
),
)
Expand Down Expand Up @@ -77,6 +95,38 @@ func TestFieldQuery(t *testing.T) {
{
Name: "ZB",
},
{
Name: "ZD",
Fields: []*json.FieldQuery{
{
Name: "#",
Fields: []*json.FieldQuery{
{
Name: "WA",
},
{
Name: "WC",
},
},
},
},
},
{
Name: "ZE",
Fields: []*json.FieldQuery{
{
Name: "#",
Fields: []*json.FieldQuery{
{
Name: "VA",
},
{
Name: "VC",
},
},
},
},
},
},
},
},
Expand All @@ -89,7 +139,7 @@ func TestFieldQuery(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if queryStr != `["XA","XB",{"XC":["YA","YB",{"YC":["ZA","ZB"]}]}]` {
if queryStr != `["XA","XB",{"XC":["YA","YB",{"YC":["ZA","ZB",{"ZD":[{"#":["WA","WC"]}]},{"ZE":[{"#":["VA","VC"]}]}]}]}]` {
t.Fatalf("failed to create query string. %s", queryStr)
}
ctx := json.SetFieldQueryToContext(context.Background(), query)
Expand All @@ -103,6 +153,14 @@ func TestFieldQuery(t *testing.T) {
ZA: "za",
ZB: true,
ZC: 3,
ZD: []queryTestW{
{WA: "wa1", WB: true, WC: 1},
{WA: "wa2", WB: true, WC: 1},
},
ZE: map[string]queryTestV{
"key1": {VA: "va1", VB: true, VC: 1},
"key2": {VA: "va2", VB: true, VC: 1},
},
},
YD: true,
YE: 4,
Expand All @@ -113,7 +171,7 @@ func TestFieldQuery(t *testing.T) {
if err != nil {
t.Fatal(err)
}
expected := `{"XA":1,"XB":"xb","XC":{"YA":2,"YB":"yb","YC":{"ZA":"za","ZB":true}}}`
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}}}}}`
got := string(b)
if expected != got {
t.Fatalf("failed to encode with field query: expected %q but got %q", expected, got)
Expand Down