Skip to content

Commit

Permalink
feat: resource selector array search
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Jan 16, 2025
1 parent c6746ec commit f0b3060
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
7 changes: 1 addition & 6 deletions query/grammar/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var _ = Describe("grammar", func() {

It("parses", func() {
result, err := ParsePEG("john:doe metadata.name=bob metadata.name!=harry spec.status.reason!=\"failed reson\" -jane johnny type!=pod type!=replicaset namespace!=\"a,b,c\"")
result, err := ParsePEG("metadata.name=bob metadata.name!=harry spec.status.reason!=\"failed reson\" -jane johnny type!=pod type!=replicaset namespace!=\"a,b,c\"")
Expect(err).To(BeNil())

resultJSON, err := json.Marshal(result)
Expand All @@ -21,11 +21,6 @@ var _ = Describe("grammar", func() {
{
"op": "and",
"fields": [
{
"field": "john",
"value": "doe",
"op": ":"
},
{
"field": "metadata.name",
"value": "bob",
Expand Down
18 changes: 9 additions & 9 deletions query/grammar/grammer.go

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

2 changes: 1 addition & 1 deletion query/grammar/grammer.peg
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Float
}

Identifier
= [a-zA-Z0-9_*-]+ {
= [a-zA-Z0-9_*-:\\[\]]+ {
return string(c.text), nil
}

Expand Down
5 changes: 3 additions & 2 deletions query/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (qm QueryModel) Apply(ctx context.Context, q types.QueryField, tx *gorm.DB)
var err error

if q.Field != "" {
originalField := q.Field
q.Field = strings.ToLower(q.Field)
if alias, ok := qm.Aliases[q.Field]; ok {
q.Field = alias
Expand Down Expand Up @@ -230,8 +231,8 @@ func (qm QueryModel) Apply(ctx context.Context, q types.QueryField, tx *gorm.DB)
}

for _, column := range qm.JSONColumns {
if strings.HasPrefix(q.Field, column) {
tx = JSONPathMapper(ctx, tx, column, strings.TrimPrefix(q.Field, column+"."), val)
if strings.HasPrefix(originalField, column) {
tx = JSONPathMapper(ctx, tx, column, strings.TrimPrefix(originalField, column+"."), val)
q.Field = column
}
}
Expand Down
22 changes: 20 additions & 2 deletions tests/query_resource_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ var _ = ginkgo.Describe("Resoure Selector with PEG", ginkgo.Ordered, func() {
resource: "config",
},
{
description: "config item query",
description: "config item query with :: in string",
query: `name=node-b type=Kubernetes::Node`,
expectedIDs: []uuid.UUID{dummy.KubernetesNodeB.ID},
resource: "config",
},
{
description: "config item query with quotes",
query: `name="node-b" type="Kubernetes::Node"`,
expectedIDs: []uuid.UUID{dummy.KubernetesNodeB.ID},
resource: "config",
Expand Down Expand Up @@ -403,7 +409,7 @@ var _ = ginkgo.Describe("Resoure Selector with PEG", ginkgo.Ordered, func() {
},
{
description: "config soft and limit query",
query: `name=node-* type="Kubernetes::Node" limit=1 sort=name`,
query: `name=node-* type=Kubernetes::Node limit=1 sort=name`,
expectedIDs: []uuid.UUID{dummy.KubernetesNodeA.ID},
resource: "config",
},
Expand All @@ -425,6 +431,18 @@ var _ = ginkgo.Describe("Resoure Selector with PEG", ginkgo.Ordered, func() {
expectedIDs: []uuid.UUID{dummy.EKSCluster.ID, dummy.EC2InstanceB.ID},
resource: "config",
},
{
description: "config array query",
query: `config.spec.template.spec.containers[0].name=logistics-api`,
expectedIDs: []uuid.UUID{dummy.LogisticsAPIDeployment.ID},
resource: "config",
},
{
description: "config array query with integer matching",
query: `config.spec.template.spec.containers[0].ports[0].containerPort=80`,
expectedIDs: []uuid.UUID{dummy.LogisticsAPIDeployment.ID},
resource: "config",
},
}

fmap := map[string]func(context.Context, int, ...types.ResourceSelector) ([]uuid.UUID, error){
Expand Down

0 comments on commit f0b3060

Please sign in to comment.