From 1445dcf18a669e6cf9f1cea5354e6bb049b25daf Mon Sep 17 00:00:00 2001 From: Julie Qiu Date: Sat, 27 Apr 2019 10:51:34 -0400 Subject: [PATCH] content,internal/frontend: update imports, importedby and modules templates The imports, importedby and module are updated to match the alpha designs. Imports and ImportedBy now link to the latest version of each package and display the package name. A bug is fixed in HandleDetails, where an panic would occur if GetLatestPackage returned an error and pkg.VersionInfo.Version was accessed immediately after. Updates b/131593473 Change-Id: Icb42d1752f2d274cccc44cf5d47f994a3eaf2f62 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/455286 Reviewed-by: Robert Findley --- content/static/html/pages/importedby.tmpl | 3 +- content/static/html/pages/imports.tmpl | 13 +++------ content/static/html/pages/module.tmpl | 23 ++++++--------- internal/frontend/details.go | 34 +++++++++++++++-------- internal/frontend/details_test.go | 10 +++++-- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/content/static/html/pages/importedby.tmpl b/content/static/html/pages/importedby.tmpl index 2c39e04a..31995d93 100644 --- a/content/static/html/pages/importedby.tmpl +++ b/content/static/html/pages/importedby.tmpl @@ -7,8 +7,9 @@ {{ define "content" }}

Imported by

+

Known importers: {{ len .ImportedBy }} packages

{{ range .ImportedBy }} -

{{ . }}

+

{{ .Name }} - {{ .Path }}

{{ end }}
{{ end }} diff --git a/content/static/html/pages/imports.tmpl b/content/static/html/pages/imports.tmpl index d94282fd..03628d93 100644 --- a/content/static/html/pages/imports.tmpl +++ b/content/static/html/pages/imports.tmpl @@ -6,16 +6,11 @@ {{ define "content" }}
- {{ if .Imports }} -

Imports

- {{ range .Imports }} -

-

{{ .Name }}
-
{{ .Path }}
-

- {{ end }} +

Imports

+

Packages: {{ len .Imports }}

+ {{ range .Imports }} +

{{ .Name }} - {{ .Path }}

{{ end }} - {{ if .StdLib }}

Standard Library

{{ range .StdLib }} diff --git a/content/static/html/pages/module.tmpl b/content/static/html/pages/module.tmpl index 096082e2..b1de0b7f 100644 --- a/content/static/html/pages/module.tmpl +++ b/content/static/html/pages/module.tmpl @@ -6,24 +6,17 @@ {{ define "content" }}
-

Module {{ .ModulePath }} @ {{ .Version }} contains packages:

-
-

Package

-

Synopsis

-
-
+

Packages in this module

{{ range .Packages }} -
- +
{{ .Synopsis }}
+

{{ end }} -
- {{ .ReadMe }}
{{ end }} diff --git a/internal/frontend/details.go b/internal/frontend/details.go index 561de3b4..0a52837a 100644 --- a/internal/frontend/details.go +++ b/internal/frontend/details.go @@ -63,12 +63,12 @@ type ImportsDetails struct { StdLib []*internal.Import } -// ImportedByDetails contains information for all packages that import a given -// package. +// ImportedByDetails contains information for the collection of packages that +// import a given package. type ImportedByDetails struct { - // ImportedBy is an array of the package paths that import a given + // ImportedBy is the collection of packages that import the given // package. - ImportedBy []string + ImportedBy []*internal.Import } // License contains information used for a single license section. @@ -119,15 +119,11 @@ type Package struct { Synopsis string CommitTime string Title string + Suffix string Licenses []LicenseInfo IsCommand bool } -// Dir returns the directory of the package relative to the root of the module. -func (p *Package) Dir() string { - return strings.TrimPrefix(p.Path, fmt.Sprintf("%s/", p.ModulePath)) -} - // transformLicenseInfos transforms an internal.LicenseInfo into a LicenseInfo, // by adding an anchor field. func transformLicenseInfos(dbLicenses []*internal.LicenseInfo) []LicenseInfo { @@ -160,6 +156,7 @@ func createPackageHeader(pkg *internal.VersionedPackage) (*Package, error) { Version: pkg.VersionInfo.Version, Path: pkg.Path, Synopsis: pkg.Package.Synopsis, + Suffix: pkg.Package.Suffix, Licenses: transformLicenseInfos(pkg.Licenses), CommitTime: elapsedTime(pkg.VersionInfo.CommitTime), }, nil @@ -249,6 +246,11 @@ func fetchModuleDetails(ctx context.Context, db *postgres.DB, pkg *internal.Vers var packages []*Package for _, p := range version.Packages { + if p.Suffix == "" { + // Display the package name if the package is at the + // root of the module. + p.Suffix = p.Name + } packages = append(packages, &Package{ Name: p.Name, Path: p.Path, @@ -256,6 +258,7 @@ func fetchModuleDetails(ctx context.Context, db *postgres.DB, pkg *internal.Vers Licenses: transformLicenseInfos(p.Licenses), Version: version.Version, ModulePath: version.ModulePath, + Suffix: p.Suffix, }) } @@ -392,12 +395,19 @@ func fetchImportsDetails(ctx context.Context, db *postgres.DB, pkg *internal.Ver } // fetchImportedByDetails fetches importers for the package version specified by -// path and version from the database and returns a ImportersDetails. +// path and version from the database and returns a ImportedByDetails. func fetchImportedByDetails(ctx context.Context, db *postgres.DB, pkg *internal.Package) (*ImportedByDetails, error) { - importedBy, err := db.GetImportedBy(ctx, pkg.Path) + importedByPaths, err := db.GetImportedBy(ctx, pkg.Path) if err != nil { return nil, fmt.Errorf("db.GetImportedBy(ctx, %q): %v", pkg.Path, err) } + var importedBy []*internal.Import + for _, p := range importedByPaths { + importedBy = append(importedBy, &internal.Import{ + Name: packageName("main", p), + Path: p, + }) + } return &ImportedByDetails{ ImportedBy: importedBy, }, nil @@ -450,7 +460,6 @@ func (c *Controller) HandleDetails(w http.ResponseWriter, r *http.Request) { if version == "" { pkg, err = c.db.GetLatestPackage(ctx, path) - version = pkg.VersionInfo.Version } else { pkg, err = c.db.GetPackage(ctx, path, version) } @@ -464,6 +473,7 @@ func (c *Controller) HandleDetails(w http.ResponseWriter, r *http.Request) { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } + version = pkg.VersionInfo.Version pkgHeader, err := createPackageHeader(pkg) if err != nil { log.Printf("error creating package header for %s@%s: %v", path, version, err) diff --git a/internal/frontend/details_test.go b/internal/frontend/details_test.go index aaaa0c38..fb75b2a4 100644 --- a/internal/frontend/details_test.go +++ b/internal/frontend/details_test.go @@ -54,6 +54,7 @@ var ( ModulePath: "test.com/module", Version: "v1.0.0", Path: "test.com/module/pkg_name", + Suffix: "pkg_name", Synopsis: "Test package synopsis", Licenses: transformLicenseInfos(sampleLicenseInfos), } @@ -660,13 +661,18 @@ func TestFetchImportedByDetails(t *testing.T) { { pkg: pkg2, wantDetails: &ImportedByDetails{ - ImportedBy: []string{pkg3.Path}, + ImportedBy: []*internal.Import{ + {Path: pkg3.Path, Name: pkg3.Name}, + }, }, }, { pkg: pkg1, wantDetails: &ImportedByDetails{ - ImportedBy: []string{pkg2.Path, pkg3.Path}, + ImportedBy: []*internal.Import{ + {Name: pkg2.Name, Path: pkg2.Path}, + {Name: pkg3.Name, Path: pkg3.Path}, + }, }, }, } {