Skip to content

Commit a3a00fc

Browse files
committed
stack: refactor Func
It does cost a small bit to precalculate but it makes the code simpler. Reduces largely the API surface. Add wrap constant for go1.13 %w functionality.
1 parent 6e45ae1 commit a3a00fc

14 files changed

Lines changed: 231 additions & 237 deletions

internal/htmlstack/data.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/htmlstack/goroutines.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
{{- /* Accepts a Call */ -}}
2929
{{- define "RenderCall" -}}
3030
<span class="call"><a href="{{srcURL .}}">{{.SrcName}}:{{.Line}}</a> <span class="{{funcClass .}}">
31-
<a href="{{pkgURL .}}">{{.Func.PkgName}}.{{.Func.Name}}</a></span>({{template "RenderArgs" .Args}})</span>
31+
<a href="{{pkgURL .}}">{{.Func.DirName}}.{{.Func.Name}}</a></span>({{template "RenderArgs" .Args}})</span>
3232
{{- if isDebug -}}
3333
<br>SrcPath: {{.SrcPath}}
3434
<br>LocalSrcPath: {{.LocalSrcPath}}
35-
<br>Func: {{.Func.Raw}}
35+
<br>Func: {{.Func.Complete}}
3636
<br>IsStdlib: {{.IsStdlib}}
3737
{{- end -}}
3838
{{- end -}}
@@ -44,7 +44,7 @@
4444
<tr>
4545
<td>{{$i}}</td>
4646
<td>
47-
<a href="{{pkgURL $e}}">{{$e.Func.PkgName}}</a>
47+
<a href="{{pkgURL $e}}">{{$e.Func.DirName}}</a>
4848
</td>
4949
<td>
5050
<a href="{{srcURL $e}}">{{$e.SrcName}}:{{$e.Line}}</a>
@@ -214,7 +214,7 @@ document.addEventListener("DOMContentLoaded", ready);
214214
</h1>
215215
{{if $e.Locked}} <span class="locked">[locked]</span>
216216
{{- end -}}
217-
{{- if $e.CreatedBy.Func.Raw}} <span class="created">Created by: {{template "RenderCall" $e.CreatedBy}}</span>
217+
{{- if $e.CreatedBy.Func.Complete}} <span class="created">Created by: {{template "RenderCall" $e.CreatedBy}}</span>
218218
{{- end -}}
219219
{{template "RenderCalls" $e.Signature.Stack}}
220220
{{- end -}}

internal/htmlstack/htmlstack.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ func isDebug() bool {
6767

6868
func funcClass(line *stack.Call) template.HTML {
6969
if line.IsStdlib {
70-
if line.Func.IsExported() {
70+
if line.Func.IsExported {
7171
return "FuncStdLibExported"
7272
}
7373
return "FuncStdLib"
74-
} else if line.IsPkgMain() {
74+
} else if line.Func.IsPkgMain {
7575
return "FuncMain"
76-
} else if line.Func.IsExported() {
76+
} else if line.Func.IsExported {
7777
return "FuncOtherExported"
7878
}
7979
return "FuncOther"
@@ -109,7 +109,7 @@ func pkgURL(c *stack.Call) template.URL {
109109
url = "https://pkg.go.dev/"
110110
}
111111
}
112-
if c.Func.IsExported() {
112+
if c.Func.IsExported {
113113
return template.URL(url + ip + "#" + symbol(&c.Func))
114114
}
115115
return template.URL(url + ip)
@@ -224,15 +224,7 @@ func splitTag(s string) (string, string, template.URL) {
224224
// All of godoc/gddo, pkg.go.dev and golang.org/godoc use the same symbol
225225
// reference format.
226226
func symbol(f *stack.Func) template.URL {
227-
i := strings.LastIndexByte(f.Raw, '/')
228-
if i == -1 {
229-
return ""
230-
}
231-
j := strings.IndexByte(f.Raw[i:], '.')
232-
if j == -1 {
233-
return ""
234-
}
235-
s := f.Raw[i+j+1:]
227+
s := f.Name
236228
if reMethodSymbol.MatchString(s) {
237229
// Transform the method form.
238230
s = reMethodSymbol.ReplaceAllString(s, "$1$2")

internal/htmlstack/htmlstack_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestSymbol(t *testing.T) {
228228
},
229229
{
230230
newFunc("main.baz"),
231-
"",
231+
"baz",
232232
},
233233
}
234234
for i, line := range data {
@@ -262,7 +262,11 @@ func BenchmarkWrite(b *testing.B) {
262262
//
263263

264264
func newFunc(s string) stack.Func {
265-
return stack.Func{Raw: s}
265+
f := stack.Func{}
266+
if err := f.Init(s); err != nil {
267+
panic(err)
268+
}
269+
return f
266270
}
267271

268272
func newCall(f string, a stack.Args, s string, l int) stack.Call {

internal/ui.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ func (pf pathFormat) formatCall(c *stack.Call) string {
6060
}
6161

6262
func (pf pathFormat) createdByString(s *stack.Signature) string {
63-
created := s.CreatedBy.Func.PkgDotName()
64-
if created == "" {
63+
if s.CreatedBy.Func.DirName == "" {
6564
return ""
6665
}
67-
return created + " @ " + pf.formatCall(&s.CreatedBy)
66+
return s.CreatedBy.Func.DirName + "." + s.CreatedBy.Func.Name + " @ " + pf.formatCall(&s.CreatedBy)
6867
}
6968

7069
// calcLengths returns the maximum length of the source lines and package names.
@@ -76,7 +75,7 @@ func calcLengths(buckets []*stack.Bucket, pf pathFormat) (int, int) {
7675
if l := len(pf.formatCall(&line)); l > srcLen {
7776
srcLen = l
7877
}
79-
if l := len(line.Func.PkgName()); l > pkgLen {
78+
if l := len(line.Func.DirName); l > pkgLen {
8079
pkgLen = l
8180
}
8281
}
@@ -88,13 +87,13 @@ func calcLengths(buckets []*stack.Bucket, pf pathFormat) (int, int) {
8887
// the type of package the function is in.
8988
func (p *Palette) functionColor(line *stack.Call) string {
9089
if line.IsStdlib {
91-
if line.Func.IsExported() {
90+
if line.Func.IsExported {
9291
return p.FuncStdLibExported
9392
}
9493
return p.FuncStdLib
95-
} else if line.IsPkgMain() {
94+
} else if line.Func.IsPkgMain {
9695
return p.FuncMain
97-
} else if line.Func.IsExported() {
96+
} else if line.Func.IsExported {
9897
return p.FuncOtherExported
9998
}
10099
return p.FuncOther
@@ -131,9 +130,9 @@ func (p *Palette) BucketHeader(bucket *stack.Bucket, pf pathFormat, multipleBuck
131130
func (p *Palette) callLine(line *stack.Call, srcLen, pkgLen int, pf pathFormat) string {
132131
return fmt.Sprintf(
133132
" %s%-*s %s%-*s %s%s%s(%s)%s",
134-
p.Package, pkgLen, line.Func.PkgName(),
133+
p.Package, pkgLen, line.Func.DirName,
135134
p.SrcFile, srcLen, pf.formatCall(line),
136-
p.functionColor(line), line.Func.Name(),
135+
p.functionColor(line), line.Func.Name,
137136
p.Arguments, &line.Args,
138137
p.EOLReset)
139138
}

internal/ui_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ func TestCalcLengths(t *testing.T) {
4545
// When printing, it prints the remote path, not the transposed local path.
4646
compareString(t, "/home/user/go/src/foo/baz.go:123", fullPath.formatCall(&b[0].Signature.Stack.Calls[0]))
4747
compareInt(t, len("/home/user/go/src/foo/baz.go:123"), srcLen)
48-
compareString(t, "main", b[0].Signature.Stack.Calls[0].Func.PkgName())
48+
compareString(t, "main", b[0].Signature.Stack.Calls[0].Func.ImportPath)
4949
compareInt(t, len("main"), pkgLen)
5050

5151
srcLen, pkgLen = calcLengths(b, basePath)
5252
compareString(t, "baz.go:123", basePath.formatCall(&b[0].Signature.Stack.Calls[0]))
5353
compareInt(t, len("baz.go:123"), srcLen)
54-
compareString(t, "main", b[0].Signature.Stack.Calls[0].Func.PkgName())
54+
compareString(t, "main", b[0].Signature.Stack.Calls[0].Func.ImportPath)
5555
compareInt(t, len("main"), pkgLen)
5656
}
5757

@@ -160,7 +160,11 @@ func TestStackLines(t *testing.T) {
160160
//
161161

162162
func newFunc(s string) stack.Func {
163-
return stack.Func{Raw: s}
163+
f := stack.Func{}
164+
if err := f.Init(s); err != nil {
165+
panic(err)
166+
}
167+
return f
164168
}
165169

166170
func newCallLocal(f string, a stack.Args, s string, l int) stack.Call {

stack/context.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ func (s *scanningState) scan(line string) (string, error) {
489489

490490
case gotFileFunc:
491491
if match := reCreated.FindStringSubmatch(trimmed); match != nil {
492-
cur.CreatedBy.Func.Raw = match[1]
492+
if err := cur.CreatedBy.Func.Init(match[1]); err != nil {
493+
return "", err
494+
}
493495
s.state = gotCreated
494496
return "", nil
495497
}
@@ -532,7 +534,9 @@ func (s *scanningState) scan(line string) (string, error) {
532534
return "", nil
533535
}
534536
if match := reCreated.FindStringSubmatch(trimmed); match != nil {
535-
cur.CreatedBy.Func.Raw = match[1]
537+
if err := cur.CreatedBy.Func.Init(match[1]); err != nil {
538+
return "", err
539+
}
536540
s.state = gotCreated
537541
return "", nil
538542
}
@@ -700,7 +704,9 @@ func (s *scanningState) scan(line string) (string, error) {
700704
// Uses reFunc.
701705
func parseFunc(c *Call, line string) (bool, error) {
702706
if match := reFunc.FindStringSubmatch(line); match != nil {
703-
c.Func.Raw = match[1]
707+
if err := c.Func.Init(match[1]); err != nil {
708+
return true, err
709+
}
704710
for _, a := range strings.Split(match[2], ", ") {
705711
if a == "..." {
706712
c.Args.Elided = true

0 commit comments

Comments
 (0)