-
Notifications
You must be signed in to change notification settings - Fork 15
glob.* and regex.globs_match builtins #210
Copy link
Copy link
Open
Labels
built-insAdding built-in functionsAdding built-in functionsopa-compatIncreasing compatibility with the upstream OPA implementation.Increasing compatibility with the upstream OPA implementation.
Milestone
Description
Summary
Implement the glob.match, glob.quote_meta, and regex.globs_match built-in functions. All three are currently registered as placeholders in src/builtins/glob.cc and src/builtins/regex.cc respectively, returning "glob not supported" at runtime.
OPA reference
glob.match:glob.match(pattern, delimiters, match)— returnstrueifmatchmatches the globpattern, usingdelimitersto split into segments. Ifdelimitersisnull, matches without delimiters; defaults to["."]if unset.glob.quote_meta:glob.quote_meta(pattern)— escapes all glob metacharacters inpatternso it can be used as a literal.regex.globs_match:regex.globs_match(glob1, glob2)— returnstrueif the intersection of the two glob-style regular expressions matches a non-empty set of non-empty strings.
OPA implementation reference
glob.match/glob.quote_meta:topdown/glob.goregex.globs_match:topdown/regex.go(uses glob-to-regex conversion internally)- OPA delegates to
gobwas/globfor glob compilation.
Current state
src/builtins/glob.cc: Placeholder declarations forglob.matchandglob.quote_metaexist with full type metadata; both returnBuiltInDef::placeholder(...).src/builtins/regex.cc:656-674: Placeholder declaration forregex.globs_matchexists; returnsBuiltInDef::placeholder(...).- Dispatcher routing in both files is already wired up; only the implementation functions need to be written and swapped from
placeholdertocreate.
Work items
glob.matchimplementation — Implement a glob matching engine that supports*(match any sequence within a segment),?(match a single character), character classes[abc]/[a-z], alternation{foo,bar}, and configurable path delimiters. Standard C/POSIXfnmatchdoes not support custom delimiters or OPA's full semantics, so this likely requires a small custom matching function or integrating a suitable C/C++ glob library.glob.quote_metaimplementation — Escape all glob metacharacters (*,?,[,],{,},\\) in the input string. This is straightforward (~20 lines).regex.globs_matchimplementation — Convert both glob patterns to regular expressions and test whether their intersection is non-empty. OPA's approach converts globs to regexes and uses an approximation. A glob-to-regex converter can be shared withglob.match.- Swap
BuiltInDef::placeholder→BuiltInDef::createin all three factory functions. - Update
README.md— Removeglob.*andregex.globs_matchfrom the unsupported builtins list (lines 150, 156). - Tests — Add test cases covering:
- Basic wildcard matching (
*,?) - Character classes and alternation
- Delimiter-based segmentation (e.g.,
"a.b.c"with["."]) - Null delimiters (no segmentation)
quote_metaround-trippingglobs_matchpositive and negative cases- Edge cases: empty pattern, empty string, escaped metacharacters
- Validate behavior matches OPA for the relevant OPA compliance tests
- Basic wildcard matching (
Notes
- The glob metacharacter set and matching semantics should match OPA's behavior, which follows the
gobwas/globlibrary conventions. regex.globs_matchis tightly coupled to the glob implementation (needs the same glob-to-regex converter), which is why it is included in this issue.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
built-insAdding built-in functionsAdding built-in functionsopa-compatIncreasing compatibility with the upstream OPA implementation.Increasing compatibility with the upstream OPA implementation.