go-pathmatch/doc.go

100 lines
2.5 KiB
Go
Raw Permalink Normal View History

/*
2019-06-24 21:33:23 +00:00
Package pathmatch provides pattern matching for path templates.
2019-06-24 21:33:23 +00:00
A path template might look something like the following:
2019-06-24 21:33:23 +00:00
/v1/users/{user_id}
2019-06-24 21:33:23 +00:00
Or:
/account={account_name}/user={user_name}/message={message_hash}
Or:
/backup/{folder_name}/
Or:
/v2/select/{fields}/from/{table_name}/where/{filters}
This path template could be a file system path, or a path could be a path from a URL (such as an HTTP or HTTPS based URL).
To compile one of these pattern templates, you would do something such as:
var template string = "/v1/users/{user_id}/messages/{message_id}"
var pattern pathmatch.Pattern
err := pathmatch.CompileTo(&pattern, template)
if nil != err {
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
}
(In addition to the pathmatch.CompileTo() func, there is also the pathmatch.Compile(), and
pathmatch.MustCompile(). But pathmatch.CompileTo() is recommended over the other 2 options
for most cases.)
One you have the compiled pattern, you would either use pathmatch.Match(), pathmatch.Find(),
or pathmatch.FindAndLoad() depending on what you were trying to accomplish.
Example Usage
2019-06-21 20:05:56 +00:00
var pattern pathmatch.Pattern
2019-06-21 20:16:25 +00:00
err := pathmatch.CompileTo(&pattern, "/users/{user_id}/vehicles/{vehicle_id}")
if nil != err {
2019-06-21 20:05:56 +00:00
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
}
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)
if nil != err {
2019-06-21 20:05:56 +00:00
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
}
2019-06-21 20:05:56 +00:00
if !matched {
fmt.Println("The patch did not match.")
2019-06-21 20:05:56 +00:00
return
}
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"
2019-06-24 21:33:23 +00:00
Alternate Example Usage
2019-06-21 20:05:56 +00:00
var pattern pathmatch.Pattern
2019-06-21 20:16:25 +00:00
err := pathmatch.CompileTo(pattern *Pattern, "/users/{user_id}/vehicles/{vehicle_id}")
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)
if nil != err {
2019-06-21 20:05:56 +00:00
fmt.Fprintf(os.Stdout, "ERROR: %s\n", err)
return
}
2019-06-21 20:05:56 +00:00
if !matched {
fmt.Println("The patch did not match.")
2019-06-21 20:05:56 +00:00
return
}
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"
*/
package pathmatch