refactoring pathmatch.Compile()

master
Charles Iliya Krempeaux 2019-06-21 13:16:25 -07:00
parent 477100a9f7
commit 51da238ec5
6 changed files with 39 additions and 11 deletions

View File

@ -22,7 +22,7 @@ import (
var pattern pathmatch.Pattern
err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
err := pathmatch.CompileTo(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
if nil != err {
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
@ -58,7 +58,7 @@ import (
var pattern patchmatch.Pattern
err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
err := pathmatch.CompileTo(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
if nil != err {
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return

View File

@ -33,7 +33,35 @@ var (
// fmt.Printf("ERROR Compiling: %v\n", err)
// return
// }
func Compile(target *Pattern, uncompiledPattern string) error {
func Compile(uncompiledPattern string) (*Pattern, error) {
var pattern Pattern
err := CompileTo(&pattern, uncompiledPattern)
if nil != err {
return nil, err
}
return &pattern, nil
}
// CompileTo takes an uncompiled pattern, in the form of a Go string (ex: "/users/{userId}/vehicles/{vehicleId}"),
// and compiles the pattern to the target.
//
// The compiled pattern can then be used to test if a path matches the pattern it contains.
//
// If the uncompiled pattern has a syntax error, Compile returns an error.
//
// Example Usage:
//
// var pattern patchmatch.Pattern
//
// err := pathmath.CompileTo(&pattern, "/users/{user_id}")
// if nil != err {
// fmt.Printf("ERROR Compiling: %v\n", err)
// return
// }
func CompileTo(target *Pattern, uncompiledPattern string) error {
if nil == target {
return errNilTarget
}
@ -111,7 +139,7 @@ func Compile(target *Pattern, uncompiledPattern string) error {
func MustCompile(uncompiledPattern string) *Pattern {
var pattern Pattern
if err := Compile(&pattern, uncompiledPattern); nil != err {
if err := CompileTo(&pattern, uncompiledPattern); nil != err {
panic(err)
} else {
return &pattern

View File

@ -456,7 +456,7 @@ func TestCompileAndMatchNames(t *testing.T) {
var actualPattern Pattern
err := Compile(&actualPattern, test.UncompiledPattern)
err := CompileTo(&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
@ -551,7 +551,7 @@ func TestCompileFail(t *testing.T) {
for testNumber, test := range tests {
var pattern Pattern
err := Compile(&pattern, test.UncompiledPattern)
err := CompileTo(&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

4
doc.go
View File

@ -10,7 +10,7 @@ Example Usage:
var pattern pathmatch.Pattern
err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
err := pathmatch.CompileTo(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
if nil != err {
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
@ -39,7 +39,7 @@ Alternate Example Usage:
var pattern pathmatch.Pattern
err := pathmatch.Compile(pattern *Pattern, "/users/{user_id}/vehicles/{vehicle_id}")
err := pathmatch.CompileTo(pattern *Pattern, "/users/{user_id}/vehicles/{vehicle_id}")
if nil != err {
//@TODO
}

View File

@ -6,11 +6,11 @@ import (
"fmt"
)
func ExampleCompile() {
func ExampleCompileTo() {
var pattern pathmatch.Pattern
err := pathmatch.Compile(&pattern, "/v1/users/{user_id}/contacts/{contact_type}")
err := pathmatch.CompileTo(&pattern, "/v1/users/{user_id}/contacts/{contact_type}")
if nil != err {
fmt.Printf("ERROR: %s\n", err)
return

View File

@ -194,7 +194,7 @@ func TestGlob(t *testing.T) {
var pattern Pattern
err := Compile(&pattern, test.Pattern)
err := CompileTo(&pattern, test.Pattern)
if nil != err {
t.Errorf("For test #%d, did not expected an error, but actually got one: %v", testNumber, err)
continue