2016-02-25 22:59:38 +00:00
|
|
|
/*
|
|
|
|
Package pathmatch provides pattern matching for paths.
|
|
|
|
|
|
|
|
For example, a path could be a file system path, or a path could be a path from a URL (such as an HTTP or HTTPS based URL).
|
|
|
|
|
2019-06-21 04:51:32 +00:00
|
|
|
The matches can be loaded into variables (when using pathmatch.Find());
|
2019-06-21 04:57:03 +00:00
|
|
|
or can be loaded into a struct (when using pathmatch.Pattern.FindAndLoad()).
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
Example Usage:
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
var pattern pathmatch.Pattern
|
|
|
|
|
|
|
|
err := pathmatch.Compile(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
|
2016-02-25 22:59:38 +00:00
|
|
|
if nil != err {
|
2019-06-21 20:05:56 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
|
|
|
|
return
|
2016-02-25 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var userId string
|
|
|
|
var vehicleId string
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
matched, err := pattern.Find("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &userId, &vehicleId)
|
2016-02-25 22:59:38 +00:00
|
|
|
if nil != err {
|
2019-06-21 20:05:56 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
|
|
|
|
return
|
2016-02-25 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
if !matched {
|
2016-02-25 22:59:38 +00:00
|
|
|
fmt.Println("The patch did not match.")
|
2019-06-21 20:05:56 +00:00
|
|
|
return
|
2016-02-25 22:59:38 +00:00
|
|
|
}
|
2019-06-21 20:05:56 +00:00
|
|
|
|
|
|
|
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"
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
Alternate Example Usage:
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
var pattern pathmatch.Pattern
|
|
|
|
|
|
|
|
err := pathmatch.Compile(pattern *Pattern, "/users/{user_id}/vehicles/{vehicle_id}")
|
2016-02-25 22:59:38 +00:00
|
|
|
if nil != err {
|
|
|
|
//@TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct{
|
|
|
|
UserId string `match:"user_id"`
|
|
|
|
VehicleId string `match:"vehicle_id"`
|
|
|
|
}{}
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
matched, err := pattern.FindAndLoad("/users/bMM_kJFMEV/vehicles/o_bcU.RZGK", &data)
|
2016-02-25 22:59:38 +00:00
|
|
|
if nil != err {
|
2019-06-21 20:05:56 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
|
|
|
|
return
|
2016-02-25 22:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
if !matched {
|
2016-02-25 22:59:38 +00:00
|
|
|
fmt.Println("The patch did not match.")
|
2019-06-21 20:05:56 +00:00
|
|
|
return
|
2016-02-25 22:59:38 +00:00
|
|
|
}
|
2019-06-21 20:05:56 +00:00
|
|
|
|
|
|
|
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"
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package pathmatch
|