godoc/vfs: improve implementation of RootType

- Removed the StandAlone and Asset root types as they were just there
for other vfses to satisfy the FileSystem interface and causing unnecessary
confusion. Returning just empty strings in those scenarios now to clarify
that it is a dummy placeholder.

- Removed the prefix "Fs" from RootType as it was unnecessary.

- Using the RootType type to pass down to the html templates
instead of converting to string. The templates are capable of converting
to the actual string representation when comparing the value.

Change-Id: Iadc039f1354ecd814eec0af1e52cdbaaeff0cc89
Reviewed-on: https://go-review.googlesource.com/106196
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Agniva De Sarker 2018-04-11 02:08:02 +05:30 committed by Brad Fitzpatrick
parent ccd319aab1
commit 8b3cccae50
11 changed files with 39 additions and 46 deletions

View File

@ -26,13 +26,13 @@ import (
const testdataDirName = "testdata" const testdataDirName = "testdata"
type Directory struct { type Directory struct {
Depth int Depth int
Path string // directory path; includes Name Path string // directory path; includes Name
Name string // directory name Name string // directory name
HasPkg bool // true if the directory contains at least one package HasPkg bool // true if the directory contains at least one package
Synopsis string // package documentation, if any Synopsis string // package documentation, if any
FsRootType string // string representation of vfs.RootType RootType vfs.RootType // root type of the filesystem containing the directory
Dirs []*Directory // subdirectories Dirs []*Directory // subdirectories
} }
func isGoFile(fi os.FileInfo) bool { func isGoFile(fi os.FileInfo) bool {
@ -198,13 +198,13 @@ func (b *treeBuilder) newDirTree(fset *token.FileSet, path, name string, depth i
} }
return &Directory{ return &Directory{
Depth: depth, Depth: depth,
Path: path, Path: path,
Name: name, Name: name,
HasPkg: hasPkgFiles && show, // TODO(bradfitz): add proper Hide field? HasPkg: hasPkgFiles && show, // TODO(bradfitz): add proper Hide field?
Synopsis: synopsis, Synopsis: synopsis,
FsRootType: string(b.c.fs.RootType(path)), RootType: b.c.fs.RootType(path),
Dirs: dirs, Dirs: dirs,
} }
} }
@ -302,13 +302,13 @@ func (dir *Directory) lookup(path string) *Directory {
// are useful for presenting an entry in an indented fashion. // are useful for presenting an entry in an indented fashion.
// //
type DirEntry struct { type DirEntry struct {
Depth int // >= 0 Depth int // >= 0
Height int // = DirList.MaxHeight - Depth, > 0 Height int // = DirList.MaxHeight - Depth, > 0
Path string // directory path; includes Name, relative to DirList root Path string // directory path; includes Name, relative to DirList root
Name string // directory name Name string // directory name
HasPkg bool // true if the directory contains at least one package HasPkg bool // true if the directory contains at least one package
Synopsis string // package documentation, if any Synopsis string // package documentation, if any
FsRootType string // string representation of vfs.RootType RootType vfs.RootType // root type of the filesystem containing the direntry
} }
type DirList struct { type DirList struct {
@ -320,7 +320,7 @@ type DirList struct {
// the standard library or not. // the standard library or not.
func hasThirdParty(list []DirEntry) bool { func hasThirdParty(list []DirEntry) bool {
for _, entry := range list { for _, entry := range list {
if entry.FsRootType == string(vfs.RootTypeGoPath) { if entry.RootType == vfs.RootTypeGoPath {
return true return true
} }
} }
@ -375,7 +375,7 @@ func (root *Directory) listing(skipRoot bool, filter func(string) bool) *DirList
p.Name = d.Name p.Name = d.Name
p.HasPkg = d.HasPkg p.HasPkg = d.HasPkg
p.Synopsis = d.Synopsis p.Synopsis = d.Synopsis
p.FsRootType = d.FsRootType p.RootType = d.RootType
list = append(list, p) list = append(list, p)
} }

View File

@ -48,7 +48,7 @@
{{range .List}} {{range .List}}
<tr> <tr>
{{if eq .FsRootType "GOROOT"}} {{if eq .RootType "GOROOT"}}
{{if $.DirFlat}} {{if $.DirFlat}}
{{if .HasPkg}} {{if .HasPkg}}
<td class="pkg-name"> <td class="pkg-name">
@ -87,7 +87,7 @@
{{range .List}} {{range .List}}
<tr> <tr>
{{if eq .FsRootType "GOPATH"}} {{if eq .RootType "GOPATH"}}
{{if $.DirFlat}} {{if $.DirFlat}}
{{if .HasPkg}} {{if .HasPkg}}
<td class="pkg-name"> <td class="pkg-name">

View File

@ -1951,7 +1951,7 @@ function cgAddChild(tree, ul, cgn) {
{{range .List}} {{range .List}}
<tr> <tr>
{{if eq .FsRootType "GOROOT"}} {{if eq .RootType "GOROOT"}}
{{if $.DirFlat}} {{if $.DirFlat}}
{{if .HasPkg}} {{if .HasPkg}}
<td class="pkg-name"> <td class="pkg-name">
@ -1990,7 +1990,7 @@ function cgAddChild(tree, ul, cgn) {
{{range .List}} {{range .List}}
<tr> <tr>
{{if eq .FsRootType "GOPATH"}} {{if eq .RootType "GOPATH"}}
{{if $.DirFlat}} {{if $.DirFlat}}
{{if .HasPkg}} {{if .HasPkg}}
<td class="pkg-name"> <td class="pkg-name">

View File

@ -58,7 +58,7 @@ func (e *emptyVFS) String() string {
} }
func (e *emptyVFS) RootType(path string) RootType { func (e *emptyVFS) RootType(path string) RootType {
return RootTypeStandAlone return ""
} }
// These functions below implement os.FileInfo for the single // These functions below implement os.FileInfo for the single

View File

@ -17,7 +17,7 @@ func TestRootType(t *testing.T) {
goPath := os.Getenv("GOPATH") goPath := os.Getenv("GOPATH")
var expectedType vfs.RootType var expectedType vfs.RootType
if goPath == "" { if goPath == "" {
expectedType = vfs.RootTypeStandAlone expectedType = ""
} else { } else {
expectedType = vfs.RootTypeGoPath expectedType = vfs.RootTypeGoPath
} }
@ -27,7 +27,7 @@ func TestRootType(t *testing.T) {
}{ }{
{runtime.GOROOT(), vfs.RootTypeGoRoot}, {runtime.GOROOT(), vfs.RootTypeGoRoot},
{goPath, expectedType}, {goPath, expectedType},
{"/tmp/", vfs.RootTypeStandAlone}, {"/tmp/", ""},
} }
for _, item := range tests { for _, item := range tests {

View File

@ -29,11 +29,8 @@ type mapFS map[string]string
func (fs mapFS) String() string { return "mapfs" } func (fs mapFS) String() string { return "mapfs" }
// RootType directly returns vfs.RootTypeAsset because
// mapFs is only used to return static assets and not for
// resolving Go files.
func (fs mapFS) RootType(p string) vfs.RootType { func (fs mapFS) RootType(p string) vfs.RootType {
return vfs.RootTypeAsset return ""
} }
func (fs mapFS) Close() error { return nil } func (fs mapFS) Close() error { return nil }

View File

@ -392,7 +392,7 @@ func (ns NameSpace) RootType(path string) RootType {
return m.fs.RootType(path) return m.fs.RootType(path)
} }
} }
return RootTypeStandAlone return ""
} }
// byName implements sort.Interface. // byName implements sort.Interface.

View File

@ -26,8 +26,6 @@ func OS(root string) FileSystem {
t = RootTypeGoRoot t = RootTypeGoRoot
case isGoPath(root): case isGoPath(root):
t = RootTypeGoPath t = RootTypeGoPath
default:
t = RootTypeStandAlone
} }
return osFS{rootPath: root, rootType: t} return osFS{rootPath: root, rootType: t}
} }

View File

@ -16,7 +16,7 @@ func TestRootType(t *testing.T) {
goPath := os.Getenv("GOPATH") goPath := os.Getenv("GOPATH")
var expectedType vfs.RootType var expectedType vfs.RootType
if goPath == "" { if goPath == "" {
expectedType = vfs.RootTypeStandAlone expectedType = ""
} else { } else {
expectedType = vfs.RootTypeGoPath expectedType = vfs.RootTypeGoPath
} }
@ -26,7 +26,7 @@ func TestRootType(t *testing.T) {
}{ }{
{runtime.GOROOT(), vfs.RootTypeGoRoot}, {runtime.GOROOT(), vfs.RootTypeGoRoot},
{goPath, expectedType}, {goPath, expectedType},
{"/tmp/", vfs.RootTypeStandAlone}, {"/tmp/", ""},
} }
for _, item := range tests { for _, item := range tests {

View File

@ -14,14 +14,14 @@ import (
// RootType indicates the type of files contained within a directory. // RootType indicates the type of files contained within a directory.
// //
// The two main types are a GOROOT or a GOPATH directory. // It is used to indicate whether a directory is the root
// of a GOROOT, a GOPATH, or neither.
// An empty string represents the case when a directory is neither.
type RootType string type RootType string
const ( const (
RootTypeGoRoot RootType = "GOROOT" RootTypeGoRoot RootType = "GOROOT"
RootTypeGoPath RootType = "GOPATH" RootTypeGoPath RootType = "GOPATH"
RootTypeStandAlone RootType = "StandAlone" // used by emptyvfs
RootTypeAsset RootType = "Assets" // serves static assets using mapfs
) )
// The FileSystem interface specifies the methods godoc is using // The FileSystem interface specifies the methods godoc is using

View File

@ -91,8 +91,6 @@ func (fs *zipFS) RootType(abspath string) vfs.RootType {
t = vfs.RootTypeGoRoot t = vfs.RootTypeGoRoot
case isGoPath(abspath): case isGoPath(abspath):
t = vfs.RootTypeGoPath t = vfs.RootTypeGoPath
default:
t = vfs.RootTypeStandAlone
} }
return t return t
} }