Skip to content

Commit

Permalink
remove method to compute path names
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 19, 2024
1 parent 69b1935 commit cb036d5
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Pattern struct {
fun *ast.Ident
call *ast.CallExpr
args []*ast.Ident

pathValueNames []string
}

func NewPattern(in string) (Pattern, error, bool) {
Expand All @@ -66,7 +68,12 @@ func NewPattern(in string) (Pattern, error, bool) {
case "", http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
}

if _, err := p.PathParameters(); err != nil {
names, err := p.parsePathValueNames()
if err != nil {
return Pattern{}, err, true
}
p.pathValueNames = names
if err := checkPathValueNames(p.pathValueNames); err != nil {
return Pattern{}, err, true
}

Expand All @@ -78,35 +85,38 @@ var (
templateNameMux = regexp.MustCompile(`^(?P<Route>(((?P<Method>[A-Z]+)\s+)?)(?P<Host>([^/])*)(?P<Path>(/(\S)*)))(?P<Handler>.*)$`)
)

func (def Pattern) PathParameters() ([]string, error) {
func (def Pattern) parsePathValueNames() ([]string, error) {
var result []string
for _, match := range pathSegmentPattern.FindAllStringSubmatch(def.Path, strings.Count(def.Path, "/")) {
n := match[1]
if n == "$" && strings.Count(def.Path, "$") == 1 && strings.HasSuffix(def.Path, "{$}") {
continue
}
n = strings.TrimSuffix(n, "...")
if !token.IsIdentifier(n) {
return nil, fmt.Errorf("path parameter name not permitted: %q is not a Go identifier", n)
}
result = append(result, n)
}
for i, n := range result {
if slices.Contains(result[:i], n) {
return nil, fmt.Errorf("forbidden repeated path parameter names: found at least 2 path parameters with name %q", n)
}
}
return result, nil
}

func (def Pattern) CallExpr() *ast.CallExpr { return def.call }
func (def Pattern) ArgIdents() []*ast.Ident { return def.args }
func (def Pattern) FunIdent() *ast.Ident { return def.fun }

func (def Pattern) String() string {
return def.name
func checkPathValueNames(in []string) error {
for i, n := range in {
if !token.IsIdentifier(n) {
return fmt.Errorf("path parameter name not permitted: %q is not a Go identifier", n)
}
if slices.Contains(in[:i], n) {
return fmt.Errorf("forbidden repeated path parameter names: found at least 2 path parameters with name %q", n)
}
}
return nil
}

func (def Pattern) String() string { return def.name }
func (def Pattern) PathValueNames() []string { return def.pathValueNames }
func (def Pattern) CallExpr() *ast.CallExpr { return def.call }
func (def Pattern) ArgIdents() []*ast.Ident { return def.args }
func (def Pattern) FunIdent() *ast.Ident { return def.fun }
func (def Pattern) sameRoute(p Pattern) bool { return def.Route == p.Route }

func (def Pattern) byPathThenMethod(d Pattern) int {
if n := cmp.Compare(def.Path, d.Path); n != 0 {
return n
Expand All @@ -117,8 +127,6 @@ func (def Pattern) byPathThenMethod(d Pattern) int {
return cmp.Compare(def.Handler, d.Handler)
}

func (def Pattern) sameRoute(p Pattern) bool { return def.Route == p.Route }

func parseHandler(def *Pattern) error {
if def.Handler == "" {
return nil
Expand All @@ -138,10 +146,6 @@ func parseHandler(def *Pattern) error {
if call.Ellipsis != token.NoPos {
return fmt.Errorf("unexpected ellipsis")
}
pathParams, err := def.PathParameters()
if err != nil {
return err
}
args := make([]*ast.Ident, len(call.Args))
for i, a := range call.Args {
arg, ok := a.(*ast.Ident)
Expand All @@ -154,11 +158,11 @@ func parseHandler(def *Pattern) error {
PatternScopeIdentifierContext,
PatternScopeIdentifierTemplate,
PatternScopeIdentifierLogger:
if slices.Contains(pathParams, name) {
if slices.Contains(def.pathValueNames, name) {
return fmt.Errorf("the name %s is not allowed as a path paramenter it is alredy in scope", name)
}
default:
if !slices.Contains(pathParams, name) {
if !slices.Contains(def.pathValueNames, name) {
return fmt.Errorf("unknown argument %s at index %d", name, i)
}
}
Expand Down

0 comments on commit cb036d5

Please sign in to comment.