Skip to content

Commit

Permalink
remove parsed args field
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 19, 2024
1 parent cb036d5 commit 56e9d02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
12 changes: 7 additions & 5 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 17 additions & 16 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type Pattern struct {

fun *ast.Ident
call *ast.CallExpr
args []*ast.Ident

pathValueNames []string
}
Expand Down Expand Up @@ -106,14 +105,16 @@ 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
}

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 }

Expand Down Expand Up @@ -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
}

Expand All @@ -181,3 +172,13 @@ const (
PatternScopeIdentifierTemplate = "template"
PatternScopeIdentifierLogger = "logger"
)

func patternScope() []string {
return []string{
PatternScopeIdentifierHTTPRequest,
PatternScopeIdentifierHTTPResponse,
PatternScopeIdentifierContext,
PatternScopeIdentifierTemplate,
PatternScopeIdentifierLogger,
}
}

0 comments on commit 56e9d02

Please sign in to comment.