added pathmatch.Pattern.Glob()

master
Charles Iliya Krempeaux 2016-02-25 17:18:58 -08:00
parent 05476fffbb
commit 6033373817
2 changed files with 237 additions and 6 deletions

View File

@ -1,6 +1,11 @@
package pathmatch
import (
"bytes"
)
// Pattern represents a compiled pattern. It is what is returned
// from calling either the Compile to MustCompile funcs.
//
@ -28,6 +33,7 @@ package pathmatch
// fmt.Println("Did not match.")
// }
type Pattern interface {
Glob() string
Match(string, ...interface{}) (bool, error)
MatchAndLoad(string, interface{}) (bool, error)
MatchNames() []string
@ -42,12 +48,6 @@ type internalPattern struct {
}
func (pattern *internalPattern) MatchNames() []string {
return pattern.names
}
func newPattern(fieldTagName string) *internalPattern {
bits := []string{}
names := []string{}
@ -63,3 +63,27 @@ func newPattern(fieldTagName string) *internalPattern {
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()
}

View File

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