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

support glob.match without delimiters #4923 #4933

merged 8 commits into from Jul 25, 2022

Conversation

vinhph0906
Copy link
Contributor

As discussed at #4923, I have changed the code to support glob.match without delimiter, by using delimiter as null.

Signed-off-by: vinhph0906 <vinhph0906@gmail.com>
Signed-off-by: vinhph0906 <vinhph0906@gmail.com>
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Two small comments online. 👍

@@ -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("*.github.com", null, "api.cdn.github.com")`` | ``true`` | A glob without delimiter. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the example a little more obvious, please? Maybe match with pattern *hub.com?

@@ -18,16 +18,20 @@ 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

Signed-off-by: vinhph0906 <vinhph0906@gmail.com>
@vinhph0906
Copy link
Contributor Author

I have changed as your comment above.

@@ -31,6 +31,8 @@ func builtinGlobMatch(a, b, c ast.Value) (ast.Value, error) {
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.

@@ -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.

Signed-off-by: vinhph0906 <vinhph0906@gmail.com>
srenatus
srenatus previously approved these changes Jul 25, 2022
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thank you for contributing! Let's add one more test case and then this is good to go.

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.

Signed-off-by: vinhph0906 <vinhph0906@gmail.com>
srenatus
srenatus previously approved these changes Jul 25, 2022
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks a lot this is a nice contribution. And such a quick turnaround, too 👏 🎉

I'll squash and merge when green.

@srenatus
Copy link
Contributor

urm there's something I hadn't thought about -- wasm! glob.match is natively implemented, i.e., there's C(++) code backing that built-in function in our wasm modules... that will need to learn about null there, too -- is that something you'd like to look into, or would you rather shy away from C et al.? LMK, and I'll pick up the rest of what's to do here.

@srenatus
Copy link
Contributor

So I jumped ahead a bit -- I think this is what we need: srenatus@32ff3ee -- but I'm still waiting for the CI run over here.

@srenatus
Copy link
Contributor

Another thing to note is that glob.match calls without a delimiter end up not getting tracked in the rule indexer. That's OK for now, it's just something to keep in mind, and perhaps revisit later.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus
Copy link
Contributor

Would you mind cherry-picking this commit? srenatus@21beae4 it should resolve the wasm parts of this, and let us merge the enhancement. :)

@vinhph0906
Copy link
Contributor Author

somehow it fails the tests :(. What I can do to fix this?

@srenatus
Copy link
Contributor

Annoying, but unrelated flakey test. I'll merge this later

@srenatus srenatus merged commit da4a100 into open-policy-agent:main Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants