refactoring pathmatch.Compile()
parent
eb4a553ef2
commit
e4db362f72
37
compile.go
37
compile.go
|
@ -33,22 +33,23 @@ var (
|
|||
// fmt.Printf("ERROR Compiling: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
func Compile(uncompiledPattern string) (*Pattern, error) {
|
||||
func Compile(target *Pattern, uncompiledPattern string) error {
|
||||
if nil == target {
|
||||
return errNilTarget
|
||||
}
|
||||
|
||||
var pattern Pattern
|
||||
|
||||
newPattern(&pattern, defaultFieldTagName)
|
||||
newPattern(target, defaultFieldTagName)
|
||||
|
||||
s := uncompiledPattern
|
||||
for {
|
||||
index := strings.IndexRune(s, '{')
|
||||
if -1 == index {
|
||||
pattern.bits = append(pattern.bits, s)
|
||||
target.bits = append(target.bits, s)
|
||||
break
|
||||
}
|
||||
bit := s[:index]
|
||||
if "" != bit { // This is to deal with the case where a {???} is right at the beginning of the uncompiledPattern.
|
||||
pattern.bits = append(pattern.bits, bit)
|
||||
target.bits = append(target.bits, bit)
|
||||
}
|
||||
s = s[1+index:]
|
||||
if "" == s {
|
||||
|
@ -57,21 +58,21 @@ func Compile(uncompiledPattern string) (*Pattern, error) {
|
|||
|
||||
index = strings.IndexRune(s, '}')
|
||||
if -1 == index {
|
||||
return nil, errMissingEndingRightBraceToMatchBeginningLeftBrace
|
||||
return errMissingEndingRightBraceToMatchBeginningLeftBrace
|
||||
}
|
||||
|
||||
// There should not be a slash ("/") before the ending brace ("}").
|
||||
// If there is, it is a syntax error.
|
||||
slashIndex := strings.IndexRune(s, '/')
|
||||
if -1 != slashIndex && slashIndex <= index {
|
||||
return nil, errSlashInsideOfBraces
|
||||
return errSlashInsideOfBraces
|
||||
}
|
||||
|
||||
// There should not be another beginning brace ("{") before the ending brace ("}").
|
||||
// If there is, it is a syntax error.
|
||||
anotherLeftBraceIndex := strings.IndexRune(s, '{')
|
||||
if -1 != anotherLeftBraceIndex && anotherLeftBraceIndex <= index {
|
||||
return nil, errLeftBraceInsideOfBraces
|
||||
return errLeftBraceInsideOfBraces
|
||||
}
|
||||
|
||||
|
||||
|
@ -79,14 +80,14 @@ func Compile(uncompiledPattern string) (*Pattern, error) {
|
|||
|
||||
|
||||
// Match names should be unique, within a pattern.
|
||||
if _, ok := pattern.namesSet[bit]; ok {
|
||||
return nil, newPatternSyntaxError("Duplicate match name: %q.", bit)
|
||||
if _, ok := target.namesSet[bit]; ok {
|
||||
return newPatternSyntaxError("Duplicate match name: %q.", bit)
|
||||
}
|
||||
|
||||
|
||||
pattern.names = append(pattern.names, bit)
|
||||
pattern.namesSet[bit] = struct{}{}
|
||||
pattern.bits = append(pattern.bits, wildcardBit)
|
||||
target.names = append(target.names, bit)
|
||||
target.namesSet[bit] = struct{}{}
|
||||
target.bits = append(target.bits, wildcardBit)
|
||||
s = s[1+index:]
|
||||
if "" == s {
|
||||
break
|
||||
|
@ -94,7 +95,7 @@ func Compile(uncompiledPattern string) (*Pattern, error) {
|
|||
}
|
||||
|
||||
|
||||
return &pattern, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
@ -108,9 +109,11 @@ 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 {
|
||||
if pattern, err := Compile(uncompiledPattern); nil != err {
|
||||
var pattern Pattern
|
||||
|
||||
if err := Compile(&pattern, uncompiledPattern); nil != err {
|
||||
panic(err)
|
||||
} else {
|
||||
return pattern
|
||||
return &pattern
|
||||
}
|
||||
}
|
||||
|
|
|
@ -454,7 +454,9 @@ func TestCompileAndMatchNames(t *testing.T) {
|
|||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actualPattern, err := Compile(test.UncompiledPattern)
|
||||
var actualPattern Pattern
|
||||
|
||||
err := Compile(&actualPattern, test.UncompiledPattern)
|
||||
if nil != err {
|
||||
t.Errorf("For test #%d, did not expect to receive an error, but actually got one: %v\nPATTERN: %q", testNumber, err, test.UncompiledPattern)
|
||||
continue
|
||||
|
@ -547,7 +549,9 @@ func TestCompileFail(t *testing.T) {
|
|||
|
||||
|
||||
for testNumber, test := range tests {
|
||||
_, err := Compile(test.UncompiledPattern)
|
||||
var pattern Pattern
|
||||
|
||||
err := Compile(&pattern, test.UncompiledPattern)
|
||||
if nil == err {
|
||||
t.Errorf("For test #%d, expected to receive an error, but actually did not get one: %v\nPATTERN: %q", testNumber, err, test.UncompiledPattern)
|
||||
continue
|
||||
|
|
|
@ -8,7 +8,9 @@ import (
|
|||
|
||||
func ExampleCompile() {
|
||||
|
||||
pattern, err := pathmatch.Compile("/v1/users/{user_id}/contacts/{contact_type}")
|
||||
var pattern pathmatch.Pattern
|
||||
|
||||
err := pathmatch.Compile(&pattern, "/v1/users/{user_id}/contacts/{contact_type}")
|
||||
if nil != err {
|
||||
fmt.Printf("ERROR: %s\n", err)
|
||||
return
|
||||
|
|
|
@ -192,7 +192,9 @@ func TestGlob(t *testing.T) {
|
|||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
pattern, err := Compile(test.Pattern)
|
||||
var pattern Pattern
|
||||
|
||||
err := Compile(&pattern, test.Pattern)
|
||||
if nil != err {
|
||||
t.Errorf("For test #%d, did not expected an error, but actually got one: %v", testNumber, err)
|
||||
continue
|
||||
|
|
Loading…
Reference in New Issue