diff --git a/name.go b/name.go index dad52d5..b6189ad 100644 --- a/name.go +++ b/name.go @@ -44,10 +44,14 @@ type TemplateName struct { fun *ast.Ident call *ast.CallExpr + fileSet *token.FileSet + pathValueNames []string } -func NewTemplateName(in string) (TemplateName, error, bool) { +func NewTemplateName(in string) (TemplateName, error, bool) { return newTemplate(in, "") } + +func newTemplate(in, fileName string) (TemplateName, error, bool) { if !templateNameMux.MatchString(in) { return TemplateName{}, nil, false } @@ -59,6 +63,7 @@ func NewTemplateName(in string) (TemplateName, error, bool) { path: matches[templateNameMux.SubexpIndex("path")], handler: strings.TrimSpace(matches[templateNameMux.SubexpIndex("handler")]), endpoint: matches[templateNameMux.SubexpIndex("endpoint")], + fileSet: token.NewFileSet(), } switch p.method { @@ -76,7 +81,7 @@ func NewTemplateName(in string) (TemplateName, error, bool) { return TemplateName{}, err, true } - return p, parseHandler(&p), true + return p, parseHandler(p.fileSet, fileName, &p), true } var ( @@ -127,11 +132,11 @@ func (def TemplateName) byPathThenMethod(d TemplateName) int { return cmp.Compare(def.handler, d.handler) } -func parseHandler(def *TemplateName) error { +func parseHandler(fileSet *token.FileSet, fileName string, def *TemplateName) error { if def.handler == "" { return nil } - e, err := parser.ParseExpr(def.handler) + e, err := parser.ParseExprFrom(fileSet, fileName, []byte(def.handler), 0) if err != nil { return fmt.Errorf("failed to parse handler expression: %v", err) } diff --git a/name_internal_test.go b/name_internal_test.go index 08b7ea6..621d22b 100644 --- a/name_internal_test.go +++ b/name_internal_test.go @@ -1,6 +1,7 @@ package muxt import ( + "fmt" "net/http" "slices" "testing" @@ -88,11 +89,19 @@ func TestTemplateName_ByPathThenMethod(t *testing.T) { } { t.Run(tt.Name, func(t *testing.T) { slices.SortFunc(tt.In, TemplateName.byPathThenMethod) - assert.Equal(t, tt.Exp, tt.In) + assert.Equal(t, stringList(tt.Exp), stringList(tt.In)) }) } } +func stringList[T fmt.Stringer](in []T) []string { + out := make([]string, 0, len(in)) + for _, e := range in { + out = append(out, e.String()) + } + return out +} + func mustNewTemplateName(in ...string) []TemplateName { var result []TemplateName for _, n := range in {