go-pathmatch/pattern.go

52 lines
1020 B
Go
Raw Permalink Normal View History

package pathmatch
2016-02-26 01:18:58 +00:00
import (
2019-06-21 20:37:53 +00:00
"sync"
2016-02-26 01:18:58 +00:00
)
// Pattern represents a compiled pattern. It is what is returned
// from calling either the Compile to MustCompile funcs.
//
// Pattern provides the Match, FindAndLoad, and MatchNames methods.
//
// Example Usage:
//
// pattern, err := pathmath.Compile("/users/{user_id}")
// if nil != err {
// fmt.Printf("ERROR Compiling: %v\n", err)
// return
// }
//
// var userId string
//
// didMatch, err := pattern.Find("/users/123", userId)
// if nil != err {
// fmt.Printf("ERROR Matching: %v\n", err)
// return
// }
//
// if didMatch {
// fmt.Printf("user_id = %q\n", userId)
// } else {
// fmt.Println("Did not match.")
// }
type Pattern struct {
2019-06-21 20:37:53 +00:00
mutex sync.RWMutex
2019-06-24 21:05:08 +00:00
template string
bits []string
names []string
namesSet map[string]struct{}
fieldTagName string
}
2019-06-21 20:37:53 +00:00
func (pattern *Pattern) MatchNames() []string {
if nil == pattern {
return nil
2019-06-21 06:37:46 +00:00
}
2019-06-21 20:37:53 +00:00
pattern.mutex.RLock()
defer pattern.mutex.RUnlock()
2016-02-26 01:18:58 +00:00
return pattern.names
}