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)
|
// fmt.Printf("ERROR Compiling: %v\n", err)
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
func Compile(uncompiledPattern string) (Pattern, error) {
|
func Compile(uncompiledPattern string) (*Pattern, error) {
|
||||||
|
|
||||||
pattern := newPattern(defaultFieldTagName)
|
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
|
// 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.
|
// 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 {
|
if pattern, err := Compile(uncompiledPattern); nil != err {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -460,7 +460,7 @@ func TestCompileAndMatchNames(t *testing.T) {
|
||||||
continue
|
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)
|
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
|
continue
|
||||||
} else {
|
} 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)
|
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
|
continue
|
||||||
} else {
|
} else {
|
||||||
|
|
24
pattern.go
24
pattern.go
|
@ -1,11 +1,9 @@
|
||||||
package pathmatch
|
package pathmatch
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"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.
|
||||||
//
|
//
|
||||||
|
@ -32,28 +30,19 @@ import (
|
||||||
// } else {
|
// } else {
|
||||||
// fmt.Println("Did not match.")
|
// fmt.Println("Did not match.")
|
||||||
// }
|
// }
|
||||||
type Pattern interface {
|
type Pattern struct {
|
||||||
Glob() string
|
|
||||||
Find(string, ...interface{}) (bool, error)
|
|
||||||
FindAndLoad(string, interface{}) (bool, error)
|
|
||||||
MatchNames() []string
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type internalPattern struct {
|
|
||||||
bits []string
|
bits []string
|
||||||
names []string
|
names []string
|
||||||
namesSet map[string]struct{}
|
namesSet map[string]struct{}
|
||||||
fieldTagName string
|
fieldTagName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newPattern(fieldTagName string) *Pattern {
|
||||||
func newPattern(fieldTagName string) *internalPattern {
|
|
||||||
bits := []string{}
|
bits := []string{}
|
||||||
names := []string{}
|
names := []string{}
|
||||||
namesSet := map[string]struct{}{}
|
namesSet := map[string]struct{}{}
|
||||||
|
|
||||||
pattern := internalPattern{
|
pattern := Pattern{
|
||||||
bits:bits,
|
bits:bits,
|
||||||
names:names,
|
names:names,
|
||||||
namesSet:namesSet,
|
namesSet:namesSet,
|
||||||
|
@ -64,14 +53,12 @@ func newPattern(fieldTagName string) *internalPattern {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pattern *Pattern) MatchNames() []string {
|
||||||
func (pattern *internalPattern) MatchNames() []string {
|
|
||||||
|
|
||||||
return pattern.names
|
return pattern.names
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pattern *Pattern) Glob() string {
|
||||||
func (pattern *internalPattern) Glob() string {
|
|
||||||
//@TODO: This shouldn't be executed every time!
|
//@TODO: This shouldn't be executed every time!
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
@ -86,4 +73,3 @@ func (pattern *internalPattern) Glob() string {
|
||||||
|
|
||||||
return buffer.String()
|
return buffer.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ var (
|
||||||
errThisShouldNeverHappen = newInternalError("This should never happen.")
|
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
|
s := path
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct{
|
tests := []struct{
|
||||||
Pattern Pattern
|
Pattern *Pattern
|
||||||
Args []interface{}
|
Args []interface{}
|
||||||
Path string
|
Path string
|
||||||
ExpectedArgs []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: Is it a good idea to be dynamically creating this?
|
||||||
//@TODO: Also, can the struct fields be put in here directly instead?
|
//@TODO: Also, can the struct fields be put in here directly instead?
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func TestFindAndLoad(t *testing.T) {
|
func TestFindAndLoad(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct{
|
tests := []struct{
|
||||||
Pattern Pattern
|
Pattern *Pattern
|
||||||
StructPtr interface{}
|
StructPtr interface{}
|
||||||
Path string
|
Path string
|
||||||
Expected map[string]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