gobwas / glob

Go glob
MIT License
960 stars 65 forks source link

Adds AnyOf Glob / EveryOf Glob #10

Closed donatj closed 8 years ago

donatj commented 8 years ago

Adds AnyOf / EveryOf for Globs fulfilling the Glob interface allowing easier grouping of globs at runtime without string concatenation.

gobwas commented 8 years ago

Hi @donatj! Thank you for your request. What is the case of this functionality? Why don't you use standard (Any/Every)Of syntax? Furthermore, there is already the code from your request in glob/match package: https://github.com/gobwas/glob/blob/master/match/any_of.go https://github.com/gobwas/glob/blob/master/match/every_of.go

donatj commented 8 years ago

My use case is that I have a set of glob strings defined dynamically at runtime by end users (human).

Instead of passing a []glob.Glob to the component that does the processing like I was doing, this allowed me to pass a single glob.Glob (in this case an EveryOf which fulfills the glob.Glob interface.). I don't want to concatenate strings, and would potentially like to be able to pull the globs apart again at a deeper level.

I built my AnyOf / EveryOf specifically to match your match/any_of / match/every_of because I thought that would help getting it accepted. 😄

I don't believe the existing AnyOf / EveryOf's would fulfill my needs, as I would have to construct the globs myself, unless I am mistaken. It lets me use a collection of globs _as_ as a single glob.

So for instance I have two globs as fl

// elsewhere in the code, defined dynamically
a := glob.MustCompile("release/*")
b := glob.MustCompile("dev/*")

// later on before passing
g := glob.NewAnyOf(a, b)

subpackage.DoMatching(g)

// different package entirely with method that takes a single glob
package subpackage

func DoMatching(g glob.Glob){
    if g.Match("blah") {
        //do awesome stuff
    }
}
gobwas commented 8 years ago

@donatj I think this is not a glob logic. I mean – there is no such methods in e.g., regexp package or smth. Why don't you write something like this:

// elsewhere in the code, defined dynamically
a := glob.MustCompile("release/*")
b := glob.MustCompile("dev/*")

// later on before passing
subpackage.DoMatching(a, b)

// different package entirely with method that takes a single glob
package subpackage

func DoMatching(globs ...glob.Glob){
    for _, g := range globx {
        if g.Match("blah") {
             //do awesome stuff
        }
    }
}

Just +1 line your snippet =)