@ -16,6 +16,11 @@ type PathPart interface {
regex ( ) string
}
var _ PathPart = PathLiteral { }
var _ PathPart = PathShortPattern { }
var _ PathPart = PathLongPattern { }
var _ PathPart = PathString { }
type PathLiteral struct {
LongAny bool ` parser:" @'**'" `
ShortAny bool ` parser:"| @'*' " `
@ -25,7 +30,7 @@ type PathLiteral struct {
func ( p PathLiteral ) regex ( ) string {
switch {
case p . Slash :
return "/"
return regexp . QuoteMeta ( "/" )
case p . ShortAny :
return "([^/]+?)"
case p . LongAny :
@ -52,7 +57,7 @@ func (p PathLongPattern) regex() string {
}
type PathString struct {
Value string ` parser:"@ Ident"`
Value string ` parser:"@ ( Ident|Rune)+ "`
}
func ( p PathString ) regex ( ) string {
@ -63,18 +68,44 @@ type Pattern struct {
Parts [ ] PathPart ` parser:"@@+" `
}
func ( pp Pattern ) regex ( ) string {
sb := & strings . Builder { }
var parser = participle . MustBuild [ Pattern ] (
participle . Union [ PathPart ] (
& PathLiteral { } ,
& PathLongPattern { } ,
& PathShortPattern { } ,
& PathString { } ,
) ,
participle . Lexer (
lexer . MustSimple ( [ ] lexer . SimpleRule {
{ Name : "Slash" , Pattern : ` / ` } ,
{ Name : "LongAny" , Pattern : ` \*\* ` } ,
{ Name : "ShortAny" , Pattern : ` \* ` } ,
{ Name : "PatternLongOpen" , Pattern : ` {{ ` } ,
{ Name : "PatternLongClose" , Pattern : ` }} ` } ,
{ Name : "PatternShortOpen" , Pattern : ` { ` } ,
{ Name : "PatternShortClose" , Pattern : ` } ` } ,
{ Name : "Ident" , Pattern : ` [a-zA-Z][a-zA-Z0-9\_]* ` } ,
{ Name : "Rune" , Pattern : ` [^/ { }]+ ` } ,
} ) ,
) ,
)
for _ , p := range pp . Parts {
sb . WriteString ( p . regex ( ) )
func ParsePattern ( path string ) ( * Pattern , error ) {
return parser . ParseString ( "" , path )
}
func MustParsePattern ( path string ) * Pattern {
p , err := ParsePattern ( path )
if err != nil {
panic ( err )
}
return sb . String ( )
return p
}
func ( pp Pattern ) Match ( s string ) ( bool , map [ string ] string , error ) {
r , err := regexp . Compile ( "^" + pp . regex ( ) + "$" )
// Match takes a path "s" and matches it against this pattern and returns whether it matched and in that case a map containing all captures for this pattern. This will throw an error if it can't compile the internal regex (should never happen).
func ( p Pattern ) Match ( s string ) ( bool , map [ string ] string , error ) {
r , err := regexp . Compile ( "^" + p . regex ( ) + "$" )
if err != nil {
return false , nil , err
}
@ -96,26 +127,15 @@ func (pp Pattern) Match(s string) (bool, map[string]string, error) {
return true , ctx , nil
}
var parser = participle . MustBuild [ Pattern ] (
participle . Union [ PathPart ] (
& PathLiteral { } ,
& PathLongPattern { } ,
& PathShortPattern { } ,
& PathString { } ,
) ,
participle . Lexer (
lexer . MustSimple ( [ ] lexer . SimpleRule {
{ Name : "Slash" , Pattern : ` / ` } ,
{ Name : "LongAny" , Pattern : ` \*\* ` } ,
{ Name : "ShortAny" , Pattern : ` \* ` } ,
{ Name : "PatternLongOpen" , Pattern : ` {{ ` } ,
{ Name : "PatternLongClose" , Pattern : ` }} ` } ,
{ Name : "PatternShortOpen" , Pattern : ` { ` } ,
{ Name : "PatternShortClose" , Pattern : ` } ` } ,
{ Name : "Ident" , Pattern : ` [^/ { }]+ ` } ,
} ) ,
) ,
)
func ( p Pattern ) regex ( ) string {
sb := & strings . Builder { }
for _ , part := range p . Parts {
sb . WriteString ( part . regex ( ) )
}
return sb . String ( )
}
func RenderTemplate ( tmpl string , ctx map [ string ] string ) string {
s := tmpl
@ -124,16 +144,3 @@ func RenderTemplate(tmpl string, ctx map[string]string) string {
}
return s
}
func ParsePattern ( path string ) ( * Pattern , error ) {
return parser . ParseString ( "" , path )
}
func MustParsePattern ( path string ) * Pattern {
p , err := ParsePattern ( path )
if err != nil {
panic ( err )
}
return p
}