Skip to content

Commit e426616

Browse files
committed
godoc,present,refactor: modernize
Apply modernizations to godoc, present, and refactor. Almost all of these are changing interface{} to any. Change-Id: Ib0a524a0c73efa7a467026cb16808cc4a1b64e57 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645376 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 8171d94 commit e426616

File tree

17 files changed

+55
-54
lines changed

17 files changed

+55
-54
lines changed

godoc/analysis/analysis.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ type Link interface {
6262
// FileInfo holds analysis information for the source file view.
6363
// Clients must not mutate it.
6464
type FileInfo struct {
65-
Data []interface{} // JSON serializable values
66-
Links []Link // HTML link markup
65+
Data []any // JSON serializable values
66+
Links []Link // HTML link markup
6767
}
6868

6969
// A fileInfo is the server's store of hyperlinks and JSON data for a
7070
// particular file.
7171
type fileInfo struct {
7272
mu sync.Mutex
73-
data []interface{} // JSON objects
73+
data []any // JSON objects
7474
links []Link
7575
sorted bool
7676
hasErrors bool // TODO(adonovan): surface this in the UI

godoc/godoc.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string {
190190
return `<span class="alert">no snippet text available</span>`
191191
}
192192

193-
func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string {
193+
func (p *Presentation) nodeFunc(info *PageInfo, node any) string {
194194
var buf bytes.Buffer
195195
p.writeNode(&buf, info, info.FSet, node)
196196
return buf.String()
197197
}
198198

199-
func (p *Presentation) node_htmlFunc(info *PageInfo, node interface{}, linkify bool) string {
199+
func (p *Presentation) node_htmlFunc(info *PageInfo, node any, linkify bool) string {
200200
var buf1 bytes.Buffer
201201
p.writeNode(&buf1, info, info.FSet, node)
202202

@@ -477,9 +477,9 @@ func srcBreadcrumbFunc(relpath string) string {
477477
return buf.String()
478478
}
479479

480-
func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n interface{}) string {
480+
func newPosLink_urlFunc(srcPosLinkFunc func(s string, line, low, high int) string) func(info *PageInfo, n any) string {
481481
// n must be an ast.Node or a *doc.Note
482-
return func(info *PageInfo, n interface{}) string {
482+
return func(info *PageInfo, n any) string {
483483
var pos, end token.Pos
484484

485485
switch n := n.(type) {
@@ -839,7 +839,7 @@ func replaceLeadingIndentation(body, oldIndent, newIndent string) string {
839839
// The provided fset must be non-nil. The pageInfo is optional. If
840840
// present, the pageInfo is used to add comments to struct fields to
841841
// say which version of Go introduced them.
842-
func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x interface{}) {
842+
func (p *Presentation) writeNode(w io.Writer, pageInfo *PageInfo, fset *token.FileSet, x any) {
843843
// convert trailing tabs into spaces using a tconv filter
844844
// to ensure a good outcome in most browsers (there may still
845845
// be tabs in comments and strings, but converting those into
@@ -918,7 +918,7 @@ var slashSlash = []byte("//")
918918

919919
// WriteNode writes x to w.
920920
// TODO(bgarcia) Is this method needed? It's just a wrapper for p.writeNode.
921-
func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x interface{}) {
921+
func (p *Presentation) WriteNode(w io.Writer, fset *token.FileSet, x any) {
922922
p.writeNode(w, nil, fset, x)
923923
}
924924

godoc/index.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ import (
7171
// InterfaceSlice is a helper type for sorting interface
7272
// slices according to some slice-specific sort criteria.
7373

74-
type comparer func(x, y interface{}) bool
74+
type comparer func(x, y any) bool
7575

7676
type interfaceSlice struct {
77-
slice []interface{}
77+
slice []any
7878
less comparer
7979
}
8080

@@ -87,7 +87,7 @@ type interfaceSlice struct {
8787
// runs. For instance, a RunList containing pairs (x, y) may be compressed
8888
// into a RunList containing pair runs (x, {y}) where each run consists of
8989
// a list of y's with the same x.
90-
type RunList []interface{}
90+
type RunList []any
9191

9292
func (h RunList) sort(less comparer) {
9393
sort.Sort(&interfaceSlice{h, less})
@@ -99,7 +99,7 @@ func (p *interfaceSlice) Swap(i, j int) { p.slice[i], p.slice[j] = p.slice[
9999

100100
// Compress entries which are the same according to a sort criteria
101101
// (specified by less) into "runs".
102-
func (h RunList) reduce(less comparer, newRun func(h RunList) interface{}) RunList {
102+
func (h RunList) reduce(less comparer, newRun func(h RunList) any) RunList {
103103
if len(h) == 0 {
104104
return nil
105105
}
@@ -143,10 +143,10 @@ func (k KindRun) Less(i, j int) bool { return k[i].Lori() < k[j].Lori() }
143143
func (k KindRun) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
144144

145145
// FileRun contents are sorted by Kind for the reduction into KindRuns.
146-
func lessKind(x, y interface{}) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() }
146+
func lessKind(x, y any) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() }
147147

148148
// newKindRun allocates a new KindRun from the SpotInfo run h.
149-
func newKindRun(h RunList) interface{} {
149+
func newKindRun(h RunList) any {
150150
run := make(KindRun, len(h))
151151
for i, x := range h {
152152
run[i] = x.(SpotInfo)
@@ -214,7 +214,7 @@ type FileRun struct {
214214
}
215215

216216
// Spots are sorted by file path for the reduction into FileRuns.
217-
func lessSpot(x, y interface{}) bool {
217+
func lessSpot(x, y any) bool {
218218
fx := x.(Spot).File
219219
fy := y.(Spot).File
220220
// same as "return fx.Path() < fy.Path()" but w/o computing the file path first
@@ -224,7 +224,7 @@ func lessSpot(x, y interface{}) bool {
224224
}
225225

226226
// newFileRun allocates a new FileRun from the Spot run h.
227-
func newFileRun(h RunList) interface{} {
227+
func newFileRun(h RunList) any {
228228
file := h[0].(Spot).File
229229

230230
// reduce the list of Spots into a list of KindRuns
@@ -257,12 +257,12 @@ func (p *PakRun) Less(i, j int) bool { return p.Files[i].File.Name < p.Files[j].
257257
func (p *PakRun) Swap(i, j int) { p.Files[i], p.Files[j] = p.Files[j], p.Files[i] }
258258

259259
// FileRuns are sorted by package for the reduction into PakRuns.
260-
func lessFileRun(x, y interface{}) bool {
260+
func lessFileRun(x, y any) bool {
261261
return x.(*FileRun).File.Pak.less(y.(*FileRun).File.Pak)
262262
}
263263

264264
// newPakRun allocates a new PakRun from the *FileRun run h.
265-
func newPakRun(h RunList) interface{} {
265+
func newPakRun(h RunList) any {
266266
pak := h[0].(*FileRun).File.Pak
267267
files := make([]*FileRun, len(h))
268268
for i, x := range h {
@@ -280,7 +280,7 @@ func newPakRun(h RunList) interface{} {
280280
type HitList []*PakRun
281281

282282
// PakRuns are sorted by package.
283-
func lessPakRun(x, y interface{}) bool { return x.(*PakRun).Pak.less(y.(*PakRun).Pak) }
283+
func lessPakRun(x, y any) bool { return x.(*PakRun).Pak.less(y.(*PakRun).Pak) }
284284

285285
func reduce(h0 RunList) HitList {
286286
// reduce a list of Spots into a list of FileRuns
@@ -325,10 +325,10 @@ type AltWords struct {
325325
}
326326

327327
// wordPairs are sorted by their canonical spelling.
328-
func lessWordPair(x, y interface{}) bool { return x.(*wordPair).canon < y.(*wordPair).canon }
328+
func lessWordPair(x, y any) bool { return x.(*wordPair).canon < y.(*wordPair).canon }
329329

330330
// newAltWords allocates a new AltWords from the *wordPair run h.
331-
func newAltWords(h RunList) interface{} {
331+
func newAltWords(h RunList) any {
332332
canon := h[0].(*wordPair).canon
333333
alts := make([]string, len(h))
334334
for i, x := range h {
@@ -1159,7 +1159,7 @@ func (x *Index) WriteTo(w io.Writer) (n int64, err error) {
11591159
return 0, err
11601160
}
11611161
if fulltext {
1162-
encode := func(x interface{}) error {
1162+
encode := func(x any) error {
11631163
return gob.NewEncoder(w).Encode(x)
11641164
}
11651165
if err := x.fset.Write(encode); err != nil {
@@ -1199,7 +1199,7 @@ func (x *Index) ReadFrom(r io.Reader) (n int64, err error) {
11991199
x.opts = fx.Opts
12001200
if fx.Fulltext {
12011201
x.fset = token.NewFileSet()
1202-
decode := func(x interface{}) error {
1202+
decode := func(x any) error {
12031203
return gob.NewDecoder(r).Decode(x)
12041204
}
12051205
if err := x.fset.Read(decode); err != nil {

godoc/search.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c *Corpus) Lookup(query string) SearchResult {
3636
// identifier search
3737
if r, err := index.Lookup(query); err == nil {
3838
result = r
39-
} else if err != nil && !c.IndexFullText {
39+
} else if !c.IndexFullText {
4040
// ignore the error if full text search is enabled
4141
// since the query may be a valid regular expression
4242
result.Alert = "Error in query string: " + err.Error()
@@ -127,7 +127,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) {
127127

128128
func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) {
129129
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
130-
data := map[string]interface{}{
130+
data := map[string]any{
131131
"BaseURL": fmt.Sprintf("http://%s", r.Host),
132132
}
133133
applyTemplateToResponseWriter(w, p.SearchDescXML, &data)

godoc/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func packageExports(fset *token.FileSet, pkg *ast.Package) {
502502
}
503503
}
504504

505-
func applyTemplate(t *template.Template, name string, data interface{}) []byte {
505+
func applyTemplate(t *template.Template, name string, data any) []byte {
506506
var buf bytes.Buffer
507507
if err := t.Execute(&buf, data); err != nil {
508508
log.Printf("%s.Execute: %s", name, err)
@@ -529,7 +529,7 @@ func (w *writerCapturesErr) Write(p []byte) (int, error) {
529529
// they come from the template processing and not the Writer; this avoid
530530
// polluting log files with error messages due to networking issues, such as
531531
// client disconnects and http HEAD protocol violations.
532-
func applyTemplateToResponseWriter(rw http.ResponseWriter, t *template.Template, data interface{}) {
532+
func applyTemplateToResponseWriter(rw http.ResponseWriter, t *template.Template, data any) {
533533
w := &writerCapturesErr{w: rw}
534534
err := t.Execute(w, data)
535535
// There are some cases where template.Execute does not return an error when
@@ -839,7 +839,7 @@ func (p *Presentation) ServeText(w http.ResponseWriter, text []byte) {
839839
w.Write(text)
840840
}
841841

842-
func marshalJSON(x interface{}) []byte {
842+
func marshalJSON(x any) []byte {
843843
var data []byte
844844
var err error
845845
const indentJSON = false // for easier debugging

godoc/spec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (p *ebnfParser) next() {
3838
p.lit = p.scanner.TokenText()
3939
}
4040

41-
func (p *ebnfParser) printf(format string, args ...interface{}) {
41+
func (p *ebnfParser) printf(format string, args ...any) {
4242
p.flush()
4343
fmt.Fprintf(p.out, format, args...)
4444
}

godoc/template.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Corpus) contents(name string) string {
5555
}
5656

5757
// stringFor returns a textual representation of the arg, formatted according to its nature.
58-
func stringFor(arg interface{}) string {
58+
func stringFor(arg any) string {
5959
switch arg := arg.(type) {
6060
case int:
6161
return fmt.Sprintf("%d", arg)
@@ -70,7 +70,7 @@ func stringFor(arg interface{}) string {
7070
return ""
7171
}
7272

73-
func (p *Presentation) code(file string, arg ...interface{}) (s string, err error) {
73+
func (p *Presentation) code(file string, arg ...any) (s string, err error) {
7474
defer func() {
7575
if r := recover(); r != nil {
7676
err = fmt.Errorf("%v", r)
@@ -85,7 +85,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro
8585
command = fmt.Sprintf("code %q", file)
8686
case 1:
8787
command = fmt.Sprintf("code %q %s", file, stringFor(arg[0]))
88-
text = p.Corpus.oneLine(file, text, arg[0])
88+
text = p.Corpus.oneLine(file, arg[0])
8989
case 2:
9090
command = fmt.Sprintf("code %q %s %s", file, stringFor(arg[0]), stringFor(arg[1]))
9191
text = p.Corpus.multipleLines(file, text, arg[0], arg[1])
@@ -105,7 +105,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro
105105
}
106106

107107
// parseArg returns the integer or string value of the argument and tells which it is.
108-
func parseArg(arg interface{}, file string, max int) (ival int, sval string, isInt bool) {
108+
func parseArg(arg any, file string, max int) (ival int, sval string, isInt bool) {
109109
switch n := arg.(type) {
110110
case int:
111111
if n <= 0 || n > max {
@@ -120,7 +120,7 @@ func parseArg(arg interface{}, file string, max int) (ival int, sval string, isI
120120
}
121121

122122
// oneLine returns the single line generated by a two-argument code invocation.
123-
func (c *Corpus) oneLine(file, text string, arg interface{}) string {
123+
func (c *Corpus) oneLine(file string, arg any) string {
124124
lines := strings.SplitAfter(c.contents(file), "\n")
125125
line, pattern, isInt := parseArg(arg, file, len(lines))
126126
if isInt {
@@ -130,7 +130,7 @@ func (c *Corpus) oneLine(file, text string, arg interface{}) string {
130130
}
131131

132132
// multipleLines returns the text generated by a three-argument code invocation.
133-
func (c *Corpus) multipleLines(file, text string, arg1, arg2 interface{}) string {
133+
func (c *Corpus) multipleLines(file, text string, arg1, arg2 any) string {
134134
lines := strings.SplitAfter(c.contents(file), "\n")
135135
line1, pattern1, isInt1 := parseArg(arg1, file, len(lines))
136136
line2, pattern2, isInt2 := parseArg(arg2, file, len(lines))

godoc/util/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ import (
1818
// access to it and records the time the value was last set.
1919
type RWValue struct {
2020
mutex sync.RWMutex
21-
value interface{}
21+
value any
2222
timestamp time.Time // time of last set()
2323
}
2424

25-
func (v *RWValue) Set(value interface{}) {
25+
func (v *RWValue) Set(value any) {
2626
v.mutex.Lock()
2727
v.value = value
2828
v.timestamp = time.Now()
2929
v.mutex.Unlock()
3030
}
3131

32-
func (v *RWValue) Get() (interface{}, time.Time) {
32+
func (v *RWValue) Get() (any, time.Time) {
3333
v.mutex.RLock()
3434
defer v.mutex.RUnlock()
3535
return v.value, v.timestamp

godoc/vfs/emptyvfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ func (e *emptyVFS) IsDir() bool {
8484
return true
8585
}
8686

87-
func (e *emptyVFS) Sys() interface{} {
87+
func (e *emptyVFS) Sys() any {
8888
return nil
8989
}

godoc/vfs/mapfs/mapfs.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ func (fi mapFI) Mode() os.FileMode {
158158
}
159159
return 0444
160160
}
161-
func (fi mapFI) Name() string { return pathpkg.Base(fi.name) }
162-
func (fi mapFI) Size() int64 { return int64(fi.size) }
163-
func (fi mapFI) Sys() interface{} { return nil }
161+
func (fi mapFI) Name() string { return pathpkg.Base(fi.name) }
162+
func (fi mapFI) Size() int64 { return int64(fi.size) }
163+
func (fi mapFI) Sys() any { return nil }
164164

165165
type nopCloser struct {
166166
io.ReadSeeker

godoc/vfs/namespace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (d dirInfo) Size() int64 { return 0 }
275275
func (d dirInfo) Mode() os.FileMode { return os.ModeDir | 0555 }
276276
func (d dirInfo) ModTime() time.Time { return startTime }
277277
func (d dirInfo) IsDir() bool { return true }
278-
func (d dirInfo) Sys() interface{} { return nil }
278+
func (d dirInfo) Sys() any { return nil }
279279

280280
var startTime = time.Now()
281281

godoc/vfs/zipfs/zipfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (fi zipFI) IsDir() bool {
6868
return fi.file == nil
6969
}
7070

71-
func (fi zipFI) Sys() interface{} {
71+
func (fi zipFI) Sys() any {
7272
return nil
7373
}
7474

present/args.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err error
9696
j = i
9797
}
9898
pattern := addr[1:i]
99-
lo, hi, err = addrRegexp(data, lo, hi, dir, pattern)
99+
lo, hi, err = addrRegexp(data, hi, dir, pattern)
100100
prevc = c
101101
addr = addr[j:]
102102
continue
@@ -202,7 +202,7 @@ func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int,
202202
// addrRegexp searches for pattern in the given direction starting at lo, hi.
203203
// The direction dir is '+' (search forward from hi) or '-' (search backward from lo).
204204
// Backward searches are unimplemented.
205-
func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, error) {
205+
func addrRegexp(data []byte, hi int, dir byte, pattern string) (int, int, error) {
206206
// We want ^ and $ to work as in sam/acme, so use ?m.
207207
re, err := regexp.Compile("(?m:" + pattern + ")")
208208
if err != nil {

present/code.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ func codeLines(src []byte, start, end int) (lines []codeLine) {
238238
return
239239
}
240240

241-
func parseArgs(name string, line int, args []string) (res []interface{}, err error) {
242-
res = make([]interface{}, len(args))
241+
func parseArgs(name string, line int, args []string) (res []any, err error) {
242+
res = make([]any, len(args))
243243
for i, v := range args {
244244
if len(v) == 0 {
245245
return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v)

0 commit comments

Comments
 (0)