got rid of Pattern interface, and changed internalPattern struct into public Pattern struct
parent
9f12059c8b
commit
83fa66ddd9
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
24
pattern.go
24
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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
func TestFind(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
Pattern Pattern
|
||||
Pattern *Pattern
|
||||
Args []interface{}
|
||||
Path string
|
||||
ExpectedArgs []string
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
func TestFindAndLoad(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
Pattern Pattern
|
||||
Pattern *Pattern
|
||||
StructPtr interface{}
|
||||
Path string
|
||||
Expected map[string]string
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue