41 lines
762 B
Go
41 lines
762 B
Go
|
package parser
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// Build builds an URI scheme from a pattern string
|
||
|
func Build(s string) (*Scheme, error){
|
||
|
|
||
|
/* (1) Manage '/' at the start */
|
||
|
if len(s) < 1 || s[0] != '/' {
|
||
|
return nil, fmt.Errorf("URI must begin with '/'")
|
||
|
}
|
||
|
|
||
|
/* (2) Split by '/' */
|
||
|
parts := strings.Split(s, "/")
|
||
|
|
||
|
/* (3) Max exceeded */
|
||
|
if len(parts)-2 > maxMatch {
|
||
|
for i, p := range parts {
|
||
|
fmt.Printf("%d: '%s'\n", i, p);
|
||
|
}
|
||
|
return nil, fmt.Errorf("URI must not exceed %d slash-separated components, got %d", maxMatch, len(parts))
|
||
|
}
|
||
|
|
||
|
/* (4) Build for each part */
|
||
|
sch, err := buildScheme(parts)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
/* (5) Optimise */
|
||
|
opti, err := sch.optimise()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &opti, nil
|
||
|
|
||
|
}
|