gobwas / glob

Go glob
MIT License
948 stars 63 forks source link

Expose helper functions and allow AST manipulation #45

Open erikgeiser opened 3 years ago

erikgeiser commented 3 years ago

Hi, great library you have here! We are building a file globbing library (https://github.com/goreleaser/fileglob) on top of glob which required us to write some helper functions that operate on glob/syntax/ast. Maybe some of these functions are useful to your users and could be exposed directly in the glob library.

Consider the following example (source code can be found here: https://github.com/goreleaser/fileglob/blob/main/prefix.go):

Currently, using these functions in our package results in the pattern being parsed multiple times: Once for every function and then finally for the actual compilation.

Also, it would be useful to allow for AST manipulation. This could be used to inject globbing options, perform queries on the AST (like the functions above) or to extend the functionality of this library. https://github.com/antonmedv/expr uses a great pattern for this, the Patcher: https://github.com/antonmedv/expr/blob/master/docs/Visitor-and-Patch.md. In glob this could be done by passing a variadic list of function operating on the AST to the Compile function:

var containsMatchers bool

# MustCompile(string, func(rootNode ...*ast.Node))
glob.MustCompile("/foo/*", ContainsMatchers(&containsMatchers))

What do you think? If you think these ideas are not a good fit for glob you can close this issue.