From 477100a9f7a1d43b170c56034b8a9d393502d57b Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 21 Jun 2019 13:05:56 -0700 Subject: [PATCH] refactoring pathmatch.Compile() --- README.md | 55 ++++++++++++++++++++++++++++++++----------------------- doc.go | 49 ++++++++++++++++++++++++++++--------------------- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 5ab3bd5..0e40435 100644 --- a/README.md +++ b/README.md @@ -20,28 +20,32 @@ import ( // ... -pattern, err := pathmatch.Compile("/users/{user_id}/vehicles/{vehicle_id}}") +var pattern pathmatch.Pattern + +err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}") if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } var userId string var vehicleId string -didMatch, err := pattern.Find("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &userId, &vehicleId) - +matched, err := pattern.Find("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &userId, &vehicleId) if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } -if didMatch { - fmt.Println("The path matched!") - - fmt.Printf("user_id = %q \n", userId) // user_id = "bMM_kJFMEV" - fmt.Printf("vehicle_id = %q \n", vehicleId) // vehicle_id = "o_bcU.RZGK" -} else { - fmt.Println("The patch did not match.") +if !matched { + fmt.Println("The patch did not match.") + return } + +fmt.Println("The path matched!") + +fmt.Printf("user_id = %q \n", userId) // user_id = "bMM_kJFMEV" +fmt.Printf("vehicle_id = %q \n", vehicleId) // vehicle_id = "o_bcU.RZGK" ``` Alternatively: @@ -52,9 +56,12 @@ import ( // ... -pattern, err := pathmatch.Compile("/users/{user_id}/vehicles/{vehicle_id}}") +var pattern patchmatch.Pattern + +err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}") if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } data := struct{ @@ -63,17 +70,19 @@ data := struct{ }{} didMatch, err := pattern.FindAndLoad("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &data) - if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } -if didMatch { - fmt.Println("The path matched!") - - fmt.Printf("user_id = %q \n", data.UserId) // user_id = "bMM_kJFMEV" - fmt.Printf("vehicle_id = %q \n", data.VehicleId) // vehicle_id = "o_bcU.RZGK" -} else { - fmt.Println("The patch did not match.") +if !matched { + fmt.Println("The patch did not match.") + return } + + +fmt.Println("The path matched!") + +fmt.Printf("user_id = %q \n", data.UserId) // user_id = "bMM_kJFMEV" +fmt.Printf("vehicle_id = %q \n", data.VehicleId) // vehicle_id = "o_bcU.RZGK" ``` diff --git a/doc.go b/doc.go index 31dd7f4..e0e6a97 100644 --- a/doc.go +++ b/doc.go @@ -8,32 +8,38 @@ or can be loaded into a struct (when using pathmatch.Pattern.FindAndLoad()). Example Usage: - pattern, err := pathmatch.Compile("/users/{user_id}/vehicles/{vehicle_id}}") + var pattern pathmatch.Pattern + + err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}") if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } var userId string var vehicleId string - didMatch, err := pattern.Find("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &userId, &vehicleId) - + matched, err := pattern.Find("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &userId, &vehicleId) if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } - if didMatch { - fmt.Println("The path matched!") - - fmt.Printf("user_id = %q \n", userId) // user_id = "bMM_kJFMEV" - fmt.Printf("vehicle_id = %q \n", vehicleId) // vehicle_id = "o_bcU.RZGK" - } else { + if !matched { fmt.Println("The patch did not match.") + return } + + fmt.Println("The path matched!") + + fmt.Printf("user_id = %q \n", userId) // user_id = "bMM_kJFMEV" + fmt.Printf("vehicle_id = %q \n", vehicleId) // vehicle_id = "o_bcU.RZGK" Alternate Example Usage: - pattern, err := pathmatch.Compile("/users/{user_id}/vehicles/{vehicle_id}}") + var pattern pathmatch.Pattern + + err := pathmatch.Compile(pattern *Pattern, "/users/{user_id}/vehicles/{vehicle_id}") if nil != err { //@TODO } @@ -43,20 +49,21 @@ Alternate Example Usage: VehicleId string `match:"vehicle_id"` }{} - didMatch, err := pattern.FindAndLoad("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &data) - + matched, err := pattern.FindAndLoad("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &data) if nil != err { - //@TODO + fmt.Fprintf(os.Stdout, "ERROR: %s\n", err) + return } - if didMatch { - fmt.Println("The path matched!") - - fmt.Printf("user_id = %q \n", data.UserId) // user_id = "bMM_kJFMEV" - fmt.Printf("vehicle_id = %q \n", data.VehicleId) // vehicle_id = "o_bcU.RZGK" - } else { + if !matched { fmt.Println("The patch did not match.") + return } + + fmt.Println("The path matched!") + + fmt.Printf("user_id = %q \n", data.UserId) // user_id = "bMM_kJFMEV" + fmt.Printf("vehicle_id = %q \n", data.VehicleId) // vehicle_id = "o_bcU.RZGK" */ package pathmatch