From 83fa66ddd95fb3dad3ae4511028569e926d307fa Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 20 Jun 2019 23:28:50 -0700 Subject: [PATCH] got rid of Pattern interface, and changed internalPattern struct into public Pattern struct --- compile.go | 4 ++-- compile_test.go | 4 ++-- pattern.go | 24 +++++------------------- pattern_find.go | 2 +- pattern_find_test.go | 2 +- pattern_load.go | 2 +- pattern_load_test.go | 2 +- pattern_test.go | 16 ---------------- 8 files changed, 13 insertions(+), 43 deletions(-) delete mode 100644 pattern_test.go diff --git a/compile.go b/compile.go index f002a0e..ead792d 100644 --- a/compile.go +++ b/compile.go @@ -33,7 +33,7 @@ var ( // fmt.Printf("ERROR Compiling: %v\n", err) // return // } -func Compile(uncompiledPattern string) (Pattern, error) { +func Compile(uncompiledPattern string) (*Pattern, error) { pattern := newPattern(defaultFieldTagName) @@ -105,7 +105,7 @@ func Compile(uncompiledPattern string) (Pattern, error) { // // Note that if one recover()s from the panic(), one can use a Go type-switch // to figure out what kind of error it is. -func MustCompile(uncompiledPattern string) Pattern { +func MustCompile(uncompiledPattern string) *Pattern { if pattern, err := Compile(uncompiledPattern); nil != err { panic(err) } else { diff --git a/compile_test.go b/compile_test.go index 571332b..2be6d39 100644 --- a/compile_test.go +++ b/compile_test.go @@ -460,7 +460,7 @@ func TestCompileAndMatchNames(t *testing.T) { continue } - if expected, actual := test.ExpectedBits, actualPattern.(*internalPattern).bits; len(expected) != len(actual) { + if expected, actual := test.ExpectedBits, actualPattern.bits; len(expected) != len(actual) { t.Errorf("For test #%d, expected compiled pattern to have %d bits, but actually had %d.\nEXPECTED BITS: %#v\nACTUAL BITS: %#v\nPATTERN: %q", testNumber, len(expected), len(actual), expected, actual, test.UncompiledPattern) continue } else { @@ -490,7 +490,7 @@ func TestCompileAndMatchNames(t *testing.T) { } } - if expected, actual := test.ExpectedNamesSet, actualPattern.(*internalPattern).namesSet; len(expected) != len(actual) { + if expected, actual := test.ExpectedNamesSet, actualPattern.namesSet; len(expected) != len(actual) { t.Errorf("For test #%d, when checking directly, expected compiled pattern to have %d names in set, but actually had %d.\nEXPECTED NAMES SET: %#v\nACTUAL NAMES SET: %#v\nPATTERN: %q", testNumber, len(expected), len(actual), expected, actual, test.UncompiledPattern) continue } else { diff --git a/pattern.go b/pattern.go index 7e200d6..683139d 100644 --- a/pattern.go +++ b/pattern.go @@ -1,11 +1,9 @@ package pathmatch - import ( "bytes" ) - // Pattern represents a compiled pattern. It is what is returned // from calling either the Compile to MustCompile funcs. // @@ -32,28 +30,19 @@ import ( // } else { // fmt.Println("Did not match.") // } -type Pattern interface { - Glob() string - Find(string, ...interface{}) (bool, error) - FindAndLoad(string, interface{}) (bool, error) - MatchNames() []string -} - - -type internalPattern struct { +type Pattern struct { bits []string names []string namesSet map[string]struct{} fieldTagName string } - -func newPattern(fieldTagName string) *internalPattern { +func newPattern(fieldTagName string) *Pattern { bits := []string{} names := []string{} namesSet := map[string]struct{}{} - pattern := internalPattern{ + pattern := Pattern{ bits:bits, names:names, namesSet:namesSet, @@ -64,14 +53,12 @@ func newPattern(fieldTagName string) *internalPattern { } - -func (pattern *internalPattern) MatchNames() []string { +func (pattern *Pattern) MatchNames() []string { return pattern.names } - -func (pattern *internalPattern) Glob() string { +func (pattern *Pattern) Glob() string { //@TODO: This shouldn't be executed every time! var buffer bytes.Buffer @@ -86,4 +73,3 @@ func (pattern *internalPattern) Glob() string { return buffer.String() } - diff --git a/pattern_find.go b/pattern_find.go index 1549049..d3e43aa 100644 --- a/pattern_find.go +++ b/pattern_find.go @@ -12,7 +12,7 @@ var ( errThisShouldNeverHappen = newInternalError("This should never happen.") ) -func (pattern *internalPattern) Find(path string, args ...interface{}) (bool, error) { +func (pattern *Pattern) Find(path string, args ...interface{}) (bool, error) { s := path diff --git a/pattern_find_test.go b/pattern_find_test.go index b2bdbde..62d1829 100644 --- a/pattern_find_test.go +++ b/pattern_find_test.go @@ -9,7 +9,7 @@ import ( func TestFind(t *testing.T) { tests := []struct{ - Pattern Pattern + Pattern *Pattern Args []interface{} Path string ExpectedArgs []string diff --git a/pattern_load.go b/pattern_load.go index 1978d64..dfcc8dc 100644 --- a/pattern_load.go +++ b/pattern_load.go @@ -14,7 +14,7 @@ var ( ) -func (pattern *internalPattern) FindAndLoad(path string, strct interface{}) (bool, error) { +func (pattern *Pattern) FindAndLoad(path string, strct interface{}) (bool, error) { //@TODO: Is it a good idea to be dynamically creating this? //@TODO: Also, can the struct fields be put in here directly instead? diff --git a/pattern_load_test.go b/pattern_load_test.go index 6e79692..016f39d 100644 --- a/pattern_load_test.go +++ b/pattern_load_test.go @@ -11,7 +11,7 @@ import ( func TestFindAndLoad(t *testing.T) { tests := []struct{ - Pattern Pattern + Pattern *Pattern StructPtr interface{} Path string Expected map[string]string diff --git a/pattern_test.go b/pattern_test.go deleted file mode 100644 index d74b1fb..0000000 --- a/pattern_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package pathmatch - -import ( - "testing" -) - -func TestInternalPatternAsPattern(t *testing.T) { - - var datum Pattern = new(internalPattern) // THIS IS WHAT ACTUALLY MATTERS. - - - if nil == datum { - t.Errorf("This should never happen.") - return - } -}