refactoring pathmatch.Compile()

master
Charles Iliya Krempeaux 2019-06-21 13:05:56 -07:00
parent e4db362f72
commit 477100a9f7
2 changed files with 60 additions and 44 deletions

View File

@ -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 {
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 {
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"
```

41
doc.go
View File

@ -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 !matched {
fmt.Println("The patch did not match.")
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.")
}
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 !matched {
fmt.Println("The patch did not match.")
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.")
}
*/
package pathmatch