From 56e9d0224a5f89ef52f614253a9258e955c14834 Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Sun, 18 Aug 2024 23:10:50 -0700 Subject: [PATCH] remove parsed args field --- generate.go | 12 +++++++----- pattern.go | 33 +++++++++++++++++---------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/generate.go b/generate.go index 0a0d960..e89bc6c 100644 --- a/generate.go +++ b/generate.go @@ -139,17 +139,18 @@ func (def Pattern) funcLit(templatesVariableIdent string, method *ast.FuncType) } call := &ast.CallExpr{Fun: callReceiverMethod(def.fun)} if method != nil { - if method.Params.NumFields() != len(def.args) { + if method.Params.NumFields() != len(def.call.Args) { return nil, nil, errWrongNumberOfArguments(def, method) } for pi, pt := range fieldListTypes(method.Params) { - if err := checkArgument(def.args[pi], pt); err != nil { + if err := checkArgument(def.call.Args[pi], pt); err != nil { return nil, nil, err } } } var imports []*ast.ImportSpec - for _, arg := range def.args { + for _, a := range def.call.Args { + arg := a.(*ast.Ident) switch arg.Name { case PatternScopeIdentifierHTTPRequest, PatternScopeIdentifierHTTPResponse: call.Args = append(call.Args, ast.NewIdent(arg.Name)) @@ -209,7 +210,8 @@ func (def Pattern) funcType() (*ast.FuncType, []*ast.ImportSpec) { Results: &ast.FieldList{List: []*ast.Field{{Type: ast.NewIdent("any")}}}, } var imports []*ast.ImportSpec - for _, arg := range def.args { + for _, a := range def.call.Args { + arg := a.(*ast.Ident) switch arg.Name { case PatternScopeIdentifierHTTPRequest: method.Params.List = append(method.Params.List, httpRequestField()) @@ -253,7 +255,7 @@ func fieldListTypes(fieldList *ast.FieldList) func(func(int, ast.Expr) bool) { } func errWrongNumberOfArguments(def Pattern, method *ast.FuncType) error { - return fmt.Errorf("handler %s expects %d arguments but call %s has %d", source.Format(&ast.FuncDecl{Name: ast.NewIdent(def.fun.Name), Type: method}), method.Params.NumFields(), def.Handler, len(def.args)) + return fmt.Errorf("handler %s expects %d arguments but call %s has %d", source.Format(&ast.FuncDecl{Name: ast.NewIdent(def.fun.Name), Type: method}), method.Params.NumFields(), def.Handler, len(def.call.Args)) } func checkArgument(exp ast.Expr, tp ast.Expr) error { diff --git a/pattern.go b/pattern.go index 2d861b0..466d457 100644 --- a/pattern.go +++ b/pattern.go @@ -43,7 +43,6 @@ type Pattern struct { fun *ast.Ident call *ast.CallExpr - args []*ast.Ident pathValueNames []string } @@ -106,6 +105,9 @@ func checkPathValueNames(in []string) error { if slices.Contains(in[:i], n) { return fmt.Errorf("forbidden repeated path parameter names: found at least 2 path parameters with name %q", n) } + if slices.Contains(patternScope(), n) { + return fmt.Errorf("the name %s is not allowed as a path paramenter it is alredy in scope", n) + } } return nil } @@ -113,7 +115,6 @@ func checkPathValueNames(in []string) error { 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 } @@ -147,30 +148,20 @@ func parseHandler(def *Pattern) error { return fmt.Errorf("unexpected ellipsis") } args := make([]*ast.Ident, len(call.Args)) + scope := append(patternScope(), def.pathValueNames...) + slices.Sort(scope) for i, a := range call.Args { arg, ok := a.(*ast.Ident) if !ok { return fmt.Errorf("expected only argument expressions as arguments, argument at index %d is: %s", i, source.Format(a)) } - switch name := arg.Name; name { - case PatternScopeIdentifierHTTPRequest, - PatternScopeIdentifierHTTPResponse, - PatternScopeIdentifierContext, - PatternScopeIdentifierTemplate, - PatternScopeIdentifierLogger: - 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(def.pathValueNames, name) { - return fmt.Errorf("unknown argument %s at index %d", name, i) - } + if _, ok := slices.BinarySearch(scope, arg.Name); !ok { + return fmt.Errorf("unknown argument %s at index %d", arg.Name, i) } args[i] = arg } def.fun = fun def.call = call - def.args = args return nil } @@ -181,3 +172,13 @@ const ( PatternScopeIdentifierTemplate = "template" PatternScopeIdentifierLogger = "logger" ) + +func patternScope() []string { + return []string{ + PatternScopeIdentifierHTTPRequest, + PatternScopeIdentifierHTTPResponse, + PatternScopeIdentifierContext, + PatternScopeIdentifierTemplate, + PatternScopeIdentifierLogger, + } +}