From 51da238ec5495f6df5b07aca71a1815413193c1e Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 21 Jun 2019 13:16:25 -0700 Subject: [PATCH] refactoring pathmatch.Compile() --- README.md | 4 ++-- compile.go | 32 ++++++++++++++++++++++++++++++-- compile_test.go | 4 ++-- doc.go | 4 ++-- example_compile_test.go | 4 ++-- pattern_glob_test.go | 2 +- 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0e40435..10f728d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/compile.go b/compile.go index 737b0a2..cb8117c 100644 --- a/compile.go +++ b/compile.go @@ -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 diff --git a/compile_test.go b/compile_test.go index 25aad49..8e5e2a1 100644 --- a/compile_test.go +++ b/compile_test.go @@ -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 diff --git a/doc.go b/doc.go index e0e6a97..f91545c 100644 --- a/doc.go +++ b/doc.go @@ -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 } diff --git a/example_compile_test.go b/example_compile_test.go index 3e4fa4a..aa4859e 100644 --- a/example_compile_test.go +++ b/example_compile_test.go @@ -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 diff --git a/pattern_glob_test.go b/pattern_glob_test.go index e57abd9..5d17e7f 100644 --- a/pattern_glob_test.go +++ b/pattern_glob_test.go @@ -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