2019-06-21 00:29:47 +00:00
|
|
|
package pathmatch_test
|
|
|
|
|
|
|
|
import (
|
2023-10-01 04:49:20 +00:00
|
|
|
"sourcecode.social/reiver/go-pathmatch"
|
2019-06-21 00:29:47 +00:00
|
|
|
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-06-21 20:16:25 +00:00
|
|
|
func ExampleCompileTo() {
|
2019-06-21 00:29:47 +00:00
|
|
|
|
2019-06-21 19:49:06 +00:00
|
|
|
var pattern pathmatch.Pattern
|
|
|
|
|
2019-06-21 20:16:25 +00:00
|
|
|
err := pathmatch.CompileTo(&pattern, "/v1/users/{user_id}/contacts/{contact_type}")
|
2019-06-21 00:29:47 +00:00
|
|
|
if nil != err {
|
|
|
|
fmt.Printf("ERROR: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
target := struct{
|
|
|
|
UserID string `match:"user_id"`
|
|
|
|
ContactType string `match:"contact_type"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
|
|
|
|
var path = "/v1/users/123/contacts/e-mail"
|
|
|
|
|
2019-06-21 04:57:03 +00:00
|
|
|
matched, err := pattern.FindAndLoad(path, &target)
|
2019-06-21 00:29:47 +00:00
|
|
|
if nil != err {
|
|
|
|
fmt.Printf("ERROR: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matched {
|
|
|
|
fmt.Printf("The path %q did not match.", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("user_id = %q\n", target.UserID)
|
|
|
|
fmt.Printf("contact_type = %q\n", target.ContactType)
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// user_id = "123"
|
|
|
|
// contact_type = "e-mail"
|
|
|
|
}
|