From 60333738170058112560178f508f452dd932d4fa Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 25 Feb 2016 17:18:58 -0800 Subject: [PATCH] added pathmatch.Pattern.Glob() --- pattern.go | 36 ++++++-- pattern_glob_test.go | 207 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+), 6 deletions(-) create mode 100644 pattern_glob_test.go diff --git a/pattern.go b/pattern.go index 55dca51..ae70c20 100644 --- a/pattern.go +++ b/pattern.go @@ -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() +} + diff --git a/pattern_glob_test.go b/pattern_glob_test.go new file mode 100644 index 0000000..764eeed --- /dev/null +++ b/pattern_glob_test.go @@ -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 + } + } +}