Skip to content

Commit

Permalink
godoc,present,refactor: modernize
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pjweinbgo committed Jan 29, 2025
1 parent 8171d94 commit e426616
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 54 deletions.
6 changes: 3 additions & 3 deletions godoc/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ type Link interface {
// FileInfo holds analysis information for the source file view.
// Clients must not mutate it.
type FileInfo struct {
Data []interface{} // JSON serializable values
Links []Link // HTML link markup
Data []any // JSON serializable values
Links []Link // HTML link markup
}

// A fileInfo is the server's store of hyperlinks and JSON data for a
// particular file.
type fileInfo struct {
mu sync.Mutex
data []interface{} // JSON objects
data []any // JSON objects
links []Link
sorted bool
hasErrors bool // TODO(adonovan): surface this in the UI
Expand Down
12 changes: 6 additions & 6 deletions godoc/godoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ func (p *Presentation) infoSnippet_htmlFunc(info SpotInfo) string {
return `<span class="alert">no snippet text available</span>`
}

func (p *Presentation) nodeFunc(info *PageInfo, node interface{}) string {
func (p *Presentation) nodeFunc(info *PageInfo, node any) string {
var buf bytes.Buffer
p.writeNode(&buf, info, info.FSet, node)
return buf.String()
}

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

Expand Down Expand Up @@ -477,9 +477,9 @@ func srcBreadcrumbFunc(relpath string) string {
return buf.String()
}

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

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

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

Expand Down
30 changes: 15 additions & 15 deletions godoc/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ import (
// InterfaceSlice is a helper type for sorting interface
// slices according to some slice-specific sort criteria.

type comparer func(x, y interface{}) bool
type comparer func(x, y any) bool

type interfaceSlice struct {
slice []interface{}
slice []any
less comparer
}

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

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

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

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

// newKindRun allocates a new KindRun from the SpotInfo run h.
func newKindRun(h RunList) interface{} {
func newKindRun(h RunList) any {
run := make(KindRun, len(h))
for i, x := range h {
run[i] = x.(SpotInfo)
Expand Down Expand Up @@ -214,7 +214,7 @@ type FileRun struct {
}

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

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

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

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

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

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

func reduce(h0 RunList) HitList {
// reduce a list of Spots into a list of FileRuns
Expand Down Expand Up @@ -325,10 +325,10 @@ type AltWords struct {
}

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

// newAltWords allocates a new AltWords from the *wordPair run h.
func newAltWords(h RunList) interface{} {
func newAltWords(h RunList) any {
canon := h[0].(*wordPair).canon
alts := make([]string, len(h))
for i, x := range h {
Expand Down Expand Up @@ -1159,7 +1159,7 @@ func (x *Index) WriteTo(w io.Writer) (n int64, err error) {
return 0, err
}
if fulltext {
encode := func(x interface{}) error {
encode := func(x any) error {
return gob.NewEncoder(w).Encode(x)
}
if err := x.fset.Write(encode); err != nil {
Expand Down Expand Up @@ -1199,7 +1199,7 @@ func (x *Index) ReadFrom(r io.Reader) (n int64, err error) {
x.opts = fx.Opts
if fx.Fulltext {
x.fset = token.NewFileSet()
decode := func(x interface{}) error {
decode := func(x any) error {
return gob.NewDecoder(r).Decode(x)
}
if err := x.fset.Read(decode); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions godoc/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Corpus) Lookup(query string) SearchResult {
// identifier search
if r, err := index.Lookup(query); err == nil {
result = r
} else if err != nil && !c.IndexFullText {
} else if !c.IndexFullText {
// ignore the error if full text search is enabled
// since the query may be a valid regular expression
result.Alert = "Error in query string: " + err.Error()
Expand Down Expand Up @@ -127,7 +127,7 @@ func (p *Presentation) HandleSearch(w http.ResponseWriter, r *http.Request) {

func (p *Presentation) serveSearchDesc(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
data := map[string]interface{}{
data := map[string]any{
"BaseURL": fmt.Sprintf("http://%s", r.Host),
}
applyTemplateToResponseWriter(w, p.SearchDescXML, &data)
Expand Down
6 changes: 3 additions & 3 deletions godoc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func packageExports(fset *token.FileSet, pkg *ast.Package) {
}
}

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

func marshalJSON(x interface{}) []byte {
func marshalJSON(x any) []byte {
var data []byte
var err error
const indentJSON = false // for easier debugging
Expand Down
2 changes: 1 addition & 1 deletion godoc/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p *ebnfParser) next() {
p.lit = p.scanner.TokenText()
}

func (p *ebnfParser) printf(format string, args ...interface{}) {
func (p *ebnfParser) printf(format string, args ...any) {
p.flush()
fmt.Fprintf(p.out, format, args...)
}
Expand Down
12 changes: 6 additions & 6 deletions godoc/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *Corpus) contents(name string) string {
}

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

func (p *Presentation) code(file string, arg ...interface{}) (s string, err error) {
func (p *Presentation) code(file string, arg ...any) (s string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
Expand All @@ -85,7 +85,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro
command = fmt.Sprintf("code %q", file)
case 1:
command = fmt.Sprintf("code %q %s", file, stringFor(arg[0]))
text = p.Corpus.oneLine(file, text, arg[0])
text = p.Corpus.oneLine(file, arg[0])
case 2:
command = fmt.Sprintf("code %q %s %s", file, stringFor(arg[0]), stringFor(arg[1]))
text = p.Corpus.multipleLines(file, text, arg[0], arg[1])
Expand All @@ -105,7 +105,7 @@ func (p *Presentation) code(file string, arg ...interface{}) (s string, err erro
}

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

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

// multipleLines returns the text generated by a three-argument code invocation.
func (c *Corpus) multipleLines(file, text string, arg1, arg2 interface{}) string {
func (c *Corpus) multipleLines(file, text string, arg1, arg2 any) string {
lines := strings.SplitAfter(c.contents(file), "\n")
line1, pattern1, isInt1 := parseArg(arg1, file, len(lines))
line2, pattern2, isInt2 := parseArg(arg2, file, len(lines))
Expand Down
6 changes: 3 additions & 3 deletions godoc/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ import (
// access to it and records the time the value was last set.
type RWValue struct {
mutex sync.RWMutex
value interface{}
value any
timestamp time.Time // time of last set()
}

func (v *RWValue) Set(value interface{}) {
func (v *RWValue) Set(value any) {
v.mutex.Lock()
v.value = value
v.timestamp = time.Now()
v.mutex.Unlock()
}

func (v *RWValue) Get() (interface{}, time.Time) {
func (v *RWValue) Get() (any, time.Time) {
v.mutex.RLock()
defer v.mutex.RUnlock()
return v.value, v.timestamp
Expand Down
2 changes: 1 addition & 1 deletion godoc/vfs/emptyvfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ func (e *emptyVFS) IsDir() bool {
return true
}

func (e *emptyVFS) Sys() interface{} {
func (e *emptyVFS) Sys() any {
return nil
}
6 changes: 3 additions & 3 deletions godoc/vfs/mapfs/mapfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func (fi mapFI) Mode() os.FileMode {
}
return 0444
}
func (fi mapFI) Name() string { return pathpkg.Base(fi.name) }
func (fi mapFI) Size() int64 { return int64(fi.size) }
func (fi mapFI) Sys() interface{} { return nil }
func (fi mapFI) Name() string { return pathpkg.Base(fi.name) }
func (fi mapFI) Size() int64 { return int64(fi.size) }
func (fi mapFI) Sys() any { return nil }

type nopCloser struct {
io.ReadSeeker
Expand Down
2 changes: 1 addition & 1 deletion godoc/vfs/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (d dirInfo) Size() int64 { return 0 }
func (d dirInfo) Mode() os.FileMode { return os.ModeDir | 0555 }
func (d dirInfo) ModTime() time.Time { return startTime }
func (d dirInfo) IsDir() bool { return true }
func (d dirInfo) Sys() interface{} { return nil }
func (d dirInfo) Sys() any { return nil }

var startTime = time.Now()

Expand Down
2 changes: 1 addition & 1 deletion godoc/vfs/zipfs/zipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (fi zipFI) IsDir() bool {
return fi.file == nil
}

func (fi zipFI) Sys() interface{} {
func (fi zipFI) Sys() any {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions present/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err error
j = i
}
pattern := addr[1:i]
lo, hi, err = addrRegexp(data, lo, hi, dir, pattern)
lo, hi, err = addrRegexp(data, hi, dir, pattern)
prevc = c
addr = addr[j:]
continue
Expand Down Expand Up @@ -202,7 +202,7 @@ func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int,
// addrRegexp searches for pattern in the given direction starting at lo, hi.
// The direction dir is '+' (search forward from hi) or '-' (search backward from lo).
// Backward searches are unimplemented.
func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, error) {
func addrRegexp(data []byte, hi int, dir byte, pattern string) (int, int, error) {
// We want ^ and $ to work as in sam/acme, so use ?m.
re, err := regexp.Compile("(?m:" + pattern + ")")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions present/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ func codeLines(src []byte, start, end int) (lines []codeLine) {
return
}

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

0 comments on commit e426616

Please sign in to comment.