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

support glob.match without delimiters #4923 #4933

Merged
merged 8 commits into from
Jul 25, 2022
Merged
5 changes: 4 additions & 1 deletion ast/builtins.go
Expand Up @@ -2604,7 +2604,10 @@ var GlobMatch = &Builtin{
Decl: types.NewFunction(
types.Args(
types.Named("pattern", types.S),
types.Named("delimiters", types.NewArray(nil, types.S)).Description("glob pattern delimiters, e.g. `[\".\", \":\"]`, defaults to `[\".\"]` if unset."),
types.Named("delimiters", types.NewAny(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line only gives a call by type ArrayString or null. So any types else have raised error before function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for static inputs. There are still inputs whose type is only known at runtime. For example glob.match(input.m, input.d, input.x)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made a change to return OperandError with default.

types.NewArray(nil, types.S),
types.NewNull(),
)).Description("glob pattern delimiters, e.g. `[\".\", \":\"]`, defaults to `[\".\"]` if unset. If `delimiters` is `null`, glob match without delimiter."),
types.Named("match", types.S),
),
types.Named("result", types.B).Description("true if `match` can be found in `pattern` which is separated by `delimiters`"),
Expand Down
4 changes: 2 additions & 2 deletions builtin_metadata.json
Expand Up @@ -3677,9 +3677,9 @@
"type": "string"
},
{
"description": "glob pattern delimiters, e.g. `[\".\", \":\"]`, defaults to `[\".\"]` if unset.",
"description": "glob pattern delimiters, e.g. `[\".\", \":\"]`, defaults to `[\".\"]` if unset. If `delimiters` is `null`, glob match without delimiter.",
"name": "delimiters",
"type": "array[string]"
"type": "any\u003cnull, array[string]\u003e"
},
{
"name": "match",
Expand Down
16 changes: 12 additions & 4 deletions capabilities.json
Expand Up @@ -894,10 +894,18 @@
"type": "string"
},
{
"dynamic": {
"type": "string"
},
"type": "array"
"of": [
{
"type": "null"
},
{
"dynamic": {
"type": "string"
},
"type": "array"
}
],
"type": "any"
},
{
"type": "string"
Expand Down
1 change: 1 addition & 0 deletions docs/content/policy-reference.md
Expand Up @@ -337,6 +337,7 @@ The following table shows examples of how ``glob.match`` works:
| -------- | ---------- | ----------- |
| ``output := glob.match("*.github.com", [], "api.github.com")`` | ``true`` | A glob with the default ``["."]`` delimiter. |
| ``output := glob.match("*.github.com", [], "api.cdn.github.com")`` | ``false`` | A glob with the default ``["."]`` delimiter. |
| ``output := glob.match("*hub.com", null, "api.cdn.github.com")`` | ``true`` | A glob without delimiter. |
| ``output := glob.match("*:github:com", [":"], "api:github:com")`` | ``true`` | A glob with delimiters ``[":"]``. |
| ``output := glob.match("api.**.com", [], "api.github.com")`` | ``true`` | A super glob. |
| ``output := glob.match("api.**.com", [], "api.cdn.github.com")`` | ``true`` | A super glob. |
Expand Down
15 changes: 15 additions & 0 deletions test/cases/testdata/globmatch/test-globmatch-0159.yaml
@@ -0,0 +1,15 @@
cases:
- data: {}
modules:
- |
package generated

p[x] {
glob.match("*", null, "foo.bar", x)
}
note: globmatch/glob match single without default delimiter
query: data.generated.p = x
sort_bindings: true
want_result:
- x:
- true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding one case like

glob.match("foo*", null, "foo.bar", x)

just to make the "no delimiters" case more obvious?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added another test case to more clearly about this.

22 changes: 14 additions & 8 deletions topdown/glob.go
Expand Up @@ -18,16 +18,22 @@ func builtinGlobMatch(a, b, c ast.Value) (ast.Value, error) {
if err != nil {
return nil, err
}
var delimiters []rune
switch b.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a default case here, otherwise it'll accept any input, too

case ast.Null:
delimiters = []rune{}
case *ast.Array:
delimiters, err = builtins.RuneSliceOperand(b, 2)
if err != nil {
return nil, err
}

delimiters, err := builtins.RuneSliceOperand(b, 2)
if err != nil {
return nil, err
}

if len(delimiters) == 0 {
delimiters = []rune{'.'}
if len(delimiters) == 0 {
delimiters = []rune{'.'}
}
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this isn't what I meant -- if it's not null and not an array, it might be something like

  • 123
  • true
  • "foo"
    i.e. some other type of Value. We should not start to silently ignore that bad input, so let's return some sort of OperandError here, please.

delimiters = []rune{}
}

match, err := builtins.StringOperand(c, 3)
if err != nil {
return nil, err
Expand Down