Skip to content

Commit 532fe62

Browse files
committed
add fileset to name expressions
1 parent b2c88c3 commit 532fe62

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

name.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ type TemplateName struct {
4444
fun *ast.Ident
4545
call *ast.CallExpr
4646

47+
fileSet *token.FileSet
48+
4749
pathValueNames []string
4850
}
4951

50-
func NewTemplateName(in string) (TemplateName, error, bool) {
52+
func NewTemplateName(in string) (TemplateName, error, bool) { return newTemplate(in, "") }
53+
54+
func newTemplate(in, fileName string) (TemplateName, error, bool) {
5155
if !templateNameMux.MatchString(in) {
5256
return TemplateName{}, nil, false
5357
}
@@ -59,6 +63,7 @@ func NewTemplateName(in string) (TemplateName, error, bool) {
5963
path: matches[templateNameMux.SubexpIndex("path")],
6064
handler: strings.TrimSpace(matches[templateNameMux.SubexpIndex("handler")]),
6165
endpoint: matches[templateNameMux.SubexpIndex("endpoint")],
66+
fileSet: token.NewFileSet(),
6267
}
6368

6469
switch p.method {
@@ -76,7 +81,7 @@ func NewTemplateName(in string) (TemplateName, error, bool) {
7681
return TemplateName{}, err, true
7782
}
7883

79-
return p, parseHandler(&p), true
84+
return p, parseHandler(p.fileSet, fileName, &p), true
8085
}
8186

8287
var (
@@ -127,11 +132,11 @@ func (def TemplateName) byPathThenMethod(d TemplateName) int {
127132
return cmp.Compare(def.handler, d.handler)
128133
}
129134

130-
func parseHandler(def *TemplateName) error {
135+
func parseHandler(fileSet *token.FileSet, fileName string, def *TemplateName) error {
131136
if def.handler == "" {
132137
return nil
133138
}
134-
e, err := parser.ParseExpr(def.handler)
139+
e, err := parser.ParseExprFrom(fileSet, fileName, []byte(def.handler), 0)
135140
if err != nil {
136141
return fmt.Errorf("failed to parse handler expression: %v", err)
137142
}

name_internal_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package muxt
22

33
import (
4+
"fmt"
45
"net/http"
56
"slices"
67
"testing"
@@ -88,11 +89,19 @@ func TestTemplateName_ByPathThenMethod(t *testing.T) {
8889
} {
8990
t.Run(tt.Name, func(t *testing.T) {
9091
slices.SortFunc(tt.In, TemplateName.byPathThenMethod)
91-
assert.Equal(t, tt.Exp, tt.In)
92+
assert.Equal(t, stringList(tt.Exp), stringList(tt.In))
9293
})
9394
}
9495
}
9596

97+
func stringList[T fmt.Stringer](in []T) []string {
98+
out := make([]string, 0, len(in))
99+
for _, e := range in {
100+
out = append(out, e.String())
101+
}
102+
return out
103+
}
104+
96105
func mustNewTemplateName(in ...string) []TemplateName {
97106
var result []TemplateName
98107
for _, n := range in {

0 commit comments

Comments
 (0)