From e4db362f72be2ed8408ba5f3cc908904fb704e08 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 21 Jun 2019 12:49:06 -0700 Subject: [PATCH] refactoring pathmatch.Compile() --- compile.go | 37 ++++++++++++++++++++----------------- compile_test.go | 8 ++++++-- example_compile_test.go | 4 +++- pattern_glob_test.go | 4 +++- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/compile.go b/compile.go index 079d8e4..737b0a2 100644 --- a/compile.go +++ b/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 } } diff --git a/compile_test.go b/compile_test.go index 2be6d39..25aad49 100644 --- a/compile_test.go +++ b/compile_test.go @@ -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 diff --git a/example_compile_test.go b/example_compile_test.go index 2d66835..3e4fa4a 100644 --- a/example_compile_test.go +++ b/example_compile_test.go @@ -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 diff --git a/pattern_glob_test.go b/pattern_glob_test.go index 764eeed..e57abd9 100644 --- a/pattern_glob_test.go +++ b/pattern_glob_test.go @@ -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