Skip to content

Commit

Permalink
Fix MergeQuery Parser (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias authored Apr 26, 2024
1 parent 274903a commit c17f363
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/shared/fix-query-parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,11 @@ test('Parse Aggregates', () => {
function assert_aggregate(query: string) {
assert.strictEqual(parse_aggregate(query).toString(), query)
}

function assert_query(query: string, expected: string | undefined = undefined) {
assert.strictEqual(parse_query(query).toString(), expected || query)
}

assert_aggregate('aggregate("kind" as k, bla as foo, bar: sum(1) as count)')
assert_aggregate('aggregate(sum(1) as count)')
assert_aggregate('aggregate(a, b, c, d, e: sum(a.b.c.d.e + 23) as count)')
Expand Down Expand Up @@ -543,3 +545,13 @@ test('Parse existing queries', () => {
}
}
})

test('Parse s3 bucket query', () => {
const parsed = parse_query(
'is(aws_s3_bucket) {account_setting: <-- is(aws_account) --> is(aws_s3_account_settings)} account_setting.reported.bucket_public_access_block_configuration.{block_public_acls != true or ignore_public_acls != true or block_public_policy != true or restrict_public_buckets != true} and bucket_public_access_block_configuration.{block_public_acls != true or ignore_public_acls != true or block_public_policy != true or restrict_public_buckets != true} and (bucket_acl.grants[*].{permission in ["READ","READ_ACP","WRITE","WRITE_ACP","FULL_CONTROL"] and grantee.uri = "http://acs.amazonaws.com/groups/global/AllUsers"} or bucket_policy.Statement[*].{Effect = "Allow" and (Principal = "*" or Principal.AWS = "*" or Principal.CanonicalUser = "*") and (Action in ["s3:GetObject","s3:PutObject","s3:Get*","s3:Put*","s3:*","*"] or Action[*] in ["s3:GetObject","s3:PutObject","s3:Get*","s3:Put*","s3:*","*"])})',
)
assert.strictEqual(parsed.parts.length, 1)
const term = parsed.parts[0].term as MergeTerm
assert.strictEqual(term.preFilter instanceof IsTerm, true)
assert.strictEqual(term.postFilter instanceof CombinedTerm, true)
})
6 changes: 3 additions & 3 deletions src/shared/fix-query-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ const with_usage = apply(
WithUsageP.setPattern(kright(tok(T.WithUsage), kmid(tok(T.LParen), with_usage, tok(T.RParen))))

const part_term = apply(
seq(TermP, opt(kmid(tok(T.LCurly), times_n_sep(MergeQueryP, tok(T.Comma)), tok(T.RCurly)))),
([preFilter, merge]) => {
seq(TermP, opt(kmid(tok(T.LCurly), times_n_sep(MergeQueryP, tok(T.Comma)), tok(T.RCurly))), opt(TermP)),
([preFilter, merge, postFilter]) => {
if (merge) {
return new MergeTerm({ preFilter, merge })
return new MergeTerm({ preFilter, merge, postFilter })
} else {
return preFilter
}
Expand Down
5 changes: 1 addition & 4 deletions src/shared/fix-query-parser/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ export abstract class Term {
} else if (this instanceof ContextTerm && wdf(this)) {
return this.term.find_terms(fn, wdf)
} else if (this instanceof MergeTerm && wdf(this)) {
return this.preFilter
.find_terms(fn, wdf)
.concat(this.merge.flatMap((q) => q.query.parts.flatMap((p) => p.term.find_terms(fn, wdf))))
.concat(this.postFilter?.find_terms(fn, wdf) || [])
return this.preFilter.find_terms(fn, wdf).concat(this.postFilter?.find_terms(fn, wdf) || [])
}
return []
}
Expand Down

0 comments on commit c17f363

Please sign in to comment.