2016-02-25 22:59:38 +00:00
|
|
|
# go-pathmatch
|
|
|
|
|
2023-10-01 04:49:20 +00:00
|
|
|
A library that provides *pattern-matching* for paths, for the Go programming language.
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
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).
|
|
|
|
|
|
|
|
|
|
|
|
## Documention
|
|
|
|
|
2024-08-02 00:45:30 +00:00
|
|
|
Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-pathmatch
|
2016-02-25 22:59:38 +00:00
|
|
|
|
2024-08-02 00:45:30 +00:00
|
|
|
[![GoDoc](https://godoc.org/github.com/reiver/go-pathmatch?status.svg)](https://godoc.org/github.com/reiver/go-pathmatch)
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Example Usage
|
2019-06-21 00:11:35 +00:00
|
|
|
```go
|
2016-02-25 22:59:38 +00:00
|
|
|
import (
|
2024-08-02 00:45:30 +00:00
|
|
|
"github.com/reiver/go-pathmatch"
|
2016-02-25 22:59:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
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}")
|
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 {
|
|
|
|
fmt.Println("The patch did not match.")
|
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively:
|
2019-06-21 00:04:33 +00:00
|
|
|
```go
|
2016-02-25 22:59:38 +00:00
|
|
|
import (
|
2024-08-02 00:45:30 +00:00
|
|
|
"github.com/reiver/go-pathmatch"
|
2016-02-25 22:59:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
2019-06-21 20:05:56 +00:00
|
|
|
var pattern patchmatch.Pattern
|
|
|
|
|
2019-06-21 20:16:25 +00:00
|
|
|
err := pathmatch.CompileTo(&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
|
|
|
}
|
|
|
|
|
|
|
|
data := struct{
|
|
|
|
UserId string `match:"user_id"`
|
|
|
|
VehicleId string `match:"vehicle_id"`
|
|
|
|
}{}
|
|
|
|
|
2019-06-21 20:17:44 +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 {
|
|
|
|
fmt.Println("The patch did not match.")
|
|
|
|
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
|
|
|
```
|
2024-08-02 00:40:10 +00:00
|
|
|
|
|
|
|
## Import
|
|
|
|
|
|
|
|
To import package **pathmatch** use `import` code like the follownig:
|
|
|
|
```
|
|
|
|
import "github.com/reiver/go-pathmatch"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
To install package **pathmatch** do the following:
|
|
|
|
```
|
|
|
|
GOPROXY=direct go get https://github.com/reiver/go-pathmatch
|
|
|
|
```
|
|
|
|
|
|
|
|
## Author
|
|
|
|
|
|
|
|
Package **pathmatch** was written by [Charles Iliya Krempeaux](http://reiver.link)
|