added pathmatch.Pattern.Glob()
parent
05476fffbb
commit
6033373817
36
pattern.go
36
pattern.go
|
@ -1,6 +1,11 @@
|
||||||
package pathmatch
|
package pathmatch
|
||||||
|
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
// Pattern represents a compiled pattern. It is what is returned
|
// Pattern represents a compiled pattern. It is what is returned
|
||||||
// from calling either the Compile to MustCompile funcs.
|
// from calling either the Compile to MustCompile funcs.
|
||||||
//
|
//
|
||||||
|
@ -28,6 +33,7 @@ package pathmatch
|
||||||
// fmt.Println("Did not match.")
|
// fmt.Println("Did not match.")
|
||||||
// }
|
// }
|
||||||
type Pattern interface {
|
type Pattern interface {
|
||||||
|
Glob() string
|
||||||
Match(string, ...interface{}) (bool, error)
|
Match(string, ...interface{}) (bool, error)
|
||||||
MatchAndLoad(string, interface{}) (bool, error)
|
MatchAndLoad(string, interface{}) (bool, error)
|
||||||
MatchNames() []string
|
MatchNames() []string
|
||||||
|
@ -42,12 +48,6 @@ type internalPattern struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (pattern *internalPattern) MatchNames() []string {
|
|
||||||
|
|
||||||
return pattern.names
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func newPattern(fieldTagName string) *internalPattern {
|
func newPattern(fieldTagName string) *internalPattern {
|
||||||
bits := []string{}
|
bits := []string{}
|
||||||
names := []string{}
|
names := []string{}
|
||||||
|
@ -63,3 +63,27 @@ func newPattern(fieldTagName string) *internalPattern {
|
||||||
return &pattern
|
return &pattern
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (pattern *internalPattern) MatchNames() []string {
|
||||||
|
|
||||||
|
return pattern.names
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (pattern *internalPattern) Glob() string {
|
||||||
|
//@TODO: This shouldn't be executed every time!
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
|
for _, bit := range pattern.bits {
|
||||||
|
if wildcardBit == bit {
|
||||||
|
buffer.WriteRune('*')
|
||||||
|
} else {
|
||||||
|
buffer.WriteString(bit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,207 @@
|
||||||
|
package pathmatch
|
||||||
|
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func TestGlob(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct{
|
||||||
|
Pattern string
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Pattern: "/",
|
||||||
|
Expected: "/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple",
|
||||||
|
Expected: "/apple",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple/",
|
||||||
|
Expected: "/apple/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple/banana",
|
||||||
|
Expected: "/apple/banana",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple/banana/",
|
||||||
|
Expected: "/apple/banana/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple/banana/cherry",
|
||||||
|
Expected: "/apple/banana/cherry",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/apple/banana/cherry/",
|
||||||
|
Expected: "/apple/banana/cherry/",
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Pattern: "",
|
||||||
|
Expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple",
|
||||||
|
Expected: "apple",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple/",
|
||||||
|
Expected: "apple/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple/banana",
|
||||||
|
Expected: "apple/banana",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple/banana/",
|
||||||
|
Expected: "apple/banana/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple/banana/cherry",
|
||||||
|
Expected: "apple/banana/cherry",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "apple/banana/cherry/",
|
||||||
|
Expected: "apple/banana/cherry/",
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Pattern: "/users/{user_id}",
|
||||||
|
Expected: "/users/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/users/{user_id}/",
|
||||||
|
Expected: "/users/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/users/{user_id}/cards",
|
||||||
|
Expected: "/users/*/cards",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/users/{user_id}/cards/{card_id}",
|
||||||
|
Expected: "/users/*/cards/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/users/{user_id}/cards/{card_id}/",
|
||||||
|
Expected: "/users/*/cards/*/",
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Pattern: "users/{user_id}",
|
||||||
|
Expected: "users/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "users/{user_id}/",
|
||||||
|
Expected: "users/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "users/{user_id}/cards",
|
||||||
|
Expected: "users/*/cards",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "users/{user_id}/cards/{card_id}",
|
||||||
|
Expected: "users/*/cards/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "users/{user_id}/cards/{card_id}/",
|
||||||
|
Expected: "users/*/cards/*/",
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Pattern: "/{this}",
|
||||||
|
Expected: "/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/",
|
||||||
|
Expected: "/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}",
|
||||||
|
Expected: "/*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}/",
|
||||||
|
Expected: "/*/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}/{these}",
|
||||||
|
Expected: "/*/*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}/{these}/",
|
||||||
|
Expected: "/*/*/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}/{these}/{those}",
|
||||||
|
Expected: "/*/*/*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/{this}/{that}/{these}/{those}/",
|
||||||
|
Expected: "/*/*/*/*/",
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
Pattern: "{this}",
|
||||||
|
Expected: "*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/",
|
||||||
|
Expected: "*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}",
|
||||||
|
Expected: "*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}/",
|
||||||
|
Expected: "*/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}/{these}",
|
||||||
|
Expected: "*/*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}/{these}/",
|
||||||
|
Expected: "*/*/*/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}/{these}/{those}",
|
||||||
|
Expected: "*/*/*/*",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Pattern: "{this}/{that}/{these}/{those}/",
|
||||||
|
Expected: "*/*/*/*/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for testNumber, test := range tests {
|
||||||
|
|
||||||
|
pattern, err := Compile(test.Pattern)
|
||||||
|
if nil != err {
|
||||||
|
t.Errorf("For test #%d, did not expected an error, but actually got one: %v", testNumber, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
glob := pattern.Glob()
|
||||||
|
if expected, actual := test.Expected, glob; expected != actual {
|
||||||
|
t.Errorf("For test #%d, expected glob to be %q, but was actually %q.", testNumber, expected, actual)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue