Skip to content

Commit 8ac8ac1

Browse files
committed
internal/frontend: add internal.PackageMeta
The two functions for creating a frontend.Package, legacyCreatePackage and createPackageNew, are merged into a single createPackage function. createPackage uses the new PackageMeta type, as input. It does not take any legacy structs as input and returns a frontend.Package. In future CLs, PackageMeta will be used to LegacyDirectory, when fetching data for the directories tab. For golang/go#39629 Change-Id: I80ec5272c6f8e237f0752938a89c633e3a1b81f5 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/241441 Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent 3aae7e7 commit 8ac8ac1

File tree

6 files changed

+71
-74
lines changed

6 files changed

+71
-74
lines changed

internal/discovery.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,44 @@ type VersionedDirectory struct {
111111
ModuleInfo
112112
}
113113

114-
// DirectoryMeta represents a folder in a module version, and the metadata
115-
// associated with that folder.
114+
// DirectoryMeta represents the metadata of a directory in a module version.
116115
type DirectoryMeta struct {
117116
Path string
118117
V1Path string
119118
IsRedistributable bool
120119
Licenses []*licenses.Metadata // metadata of applicable licenses
121120
}
122121

123-
// DirectoryNew represents a folder in a module version, and the contents of that folder.
122+
// DirectoryNew represents a directory in a module version, and the contents of that directory.
124123
// It will replace LegacyDirectory once everything has been migrated.
125124
type DirectoryNew struct {
126125
DirectoryMeta
127126
Readme *Readme
128127
Package *PackageNew
129128
}
130129

130+
// PackageMeta represents the metadata of a package in a module version.
131+
type PackageMeta struct {
132+
DirectoryMeta
133+
Name string
134+
Synopsis string
135+
}
136+
137+
// PackageMetaFromLegacyPackage returns a PackageMeta based on data from a
138+
// LegacyPackage.
139+
func PackageMetaFromLegacyPackage(pkg *LegacyPackage) *PackageMeta {
140+
return &PackageMeta{
141+
DirectoryMeta: DirectoryMeta{
142+
Path: pkg.Path,
143+
V1Path: pkg.V1Path,
144+
Licenses: pkg.Licenses,
145+
IsRedistributable: pkg.IsRedistributable,
146+
},
147+
Name: pkg.Name,
148+
Synopsis: pkg.Synopsis,
149+
}
150+
}
151+
131152
// PackageNew is a group of one or more Go source files with the same package
132153
// header. A PackageNew is part of a directory.
133154
// It will replace LegacyPackage once everything has been migrated.
@@ -273,8 +294,8 @@ const (
273294
WithDocumentationHTML
274295
)
275296

276-
// LegacyDirectory represents a folder in a module version, and all of the
277-
// packages inside that folder.
297+
// LegacyDirectory represents a directory in a module version, and all of the
298+
// packages inside that directory.
278299
type LegacyDirectory struct {
279300
LegacyModuleInfo
280301
Path string

internal/frontend/directory.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,14 @@ func legacyCreateDirectory(dbDir *internal.LegacyDirectory, licmetas []*licenses
132132
if !includeDirPath && pkg.Path == dbDir.Path {
133133
continue
134134
}
135-
newPkg, err := legacyCreatePackage(pkg, &dbDir.ModuleInfo, false)
135+
pm := internal.PackageMetaFromLegacyPackage(pkg)
136+
newPkg, err := createPackage(pm, &dbDir.ModuleInfo, false)
136137
if err != nil {
137138
return nil, err
138139
}
139-
if pkg.IsRedistributable {
140-
newPkg.Synopsis = pkg.Synopsis
141-
}
142-
newPkg.PathAfterDirectory = strings.TrimPrefix(strings.TrimPrefix(pkg.Path, dbDir.Path), "/")
140+
newPkg.PathAfterDirectory = strings.TrimPrefix(strings.TrimPrefix(pm.Path, dbDir.Path), "/")
143141
if newPkg.PathAfterDirectory == "" {
144-
newPkg.PathAfterDirectory = effectiveName(pkg) + " (root)"
142+
newPkg.PathAfterDirectory = effectiveName(pm.Path, pm.Name) + " (root)"
145143
}
146144
packages = append(packages, newPkg)
147145
}

internal/frontend/directory_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ func TestFetchDirectoryDetails(t *testing.T) {
3434
var wantPkgs []*Package
3535
for _, suffix := range suffixes {
3636
sp := sample.LegacyPackage(modulePath, suffix)
37-
pkg, err := legacyCreatePackage(sp, mi, false)
37+
pm := internal.PackageMetaFromLegacyPackage(sp)
38+
pkg, err := createPackage(pm, mi, false)
3839
if err != nil {
3940
t.Fatal(err)
4041
}
42+
pkg.PathAfterDirectory = strings.TrimPrefix(strings.TrimPrefix(pm.Path, dirPath), "/")
43+
if pkg.PathAfterDirectory == "" {
44+
pkg.PathAfterDirectory = effectiveName(pm.Path, pm.Name) + " (root)"
45+
}
46+
pkg.Synopsis = sp.Synopsis
4147
pkg.PathAfterDirectory = strings.TrimPrefix(strings.TrimPrefix(sp.Path, dirPath), "/")
4248
if pkg.PathAfterDirectory == "" {
43-
pkg.PathAfterDirectory = fmt.Sprintf("%s (root)", effectiveName(sp))
49+
pkg.PathAfterDirectory = fmt.Sprintf("%s (root)", effectiveName(sp.Path, sp.Name))
4450
}
4551
wantPkgs = append(wantPkgs, pkg)
4652
}

internal/frontend/header.go

+15-55
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
type Package struct {
2323
Module
2424
Path string // full import path
25-
PathAfterDirectory string // for display only; used only for directory
26-
Synopsis string
27-
IsRedistributable bool
2825
URL string // relative to this site
2926
LatestURL string // link with latest-version placeholder, relative to this site
27+
IsRedistributable bool
3028
Licenses []LicenseMetadata
29+
PathAfterDirectory string // for display on the directories tab; used by Directory
30+
Synopsis string // for display on the directories tab; used by Directory
3131
}
3232

3333
// Module contains information for an individual module.
@@ -42,18 +42,14 @@ type Module struct {
4242
Licenses []LicenseMetadata
4343
}
4444

45-
// legacyCreatePackage returns a *Package based on the fields of the specified
45+
// createPackage returns a *Package based on the fields of the specified
4646
// internal package and version info.
4747
//
4848
// latestRequested indicates whether the user requested the latest
4949
// version of the package. If so, the returned Package.URL will have the
5050
// structure /<path> instead of /<path>@<version>.
51-
func legacyCreatePackage(pkg *internal.LegacyPackage, mi *internal.ModuleInfo, latestRequested bool) (_ *Package, err error) {
52-
defer derrors.Wrap(&err, "legacyCreatePackage(%v, %v)", pkg, mi)
53-
54-
if pkg == nil || mi == nil {
55-
return nil, fmt.Errorf("package and version info must not be nil")
56-
}
51+
func createPackage(pkg *internal.PackageMeta, mi *internal.ModuleInfo, latestRequested bool) (_ *Package, err error) {
52+
defer derrors.Wrap(&err, "createPackage(%v, %v, %t)", pkg, mi, latestRequested)
5753

5854
var modLicenses []*licenses.Metadata
5955
for _, lm := range pkg.Licenses {
@@ -69,48 +65,12 @@ func legacyCreatePackage(pkg *internal.LegacyPackage, mi *internal.ModuleInfo, l
6965
}
7066
return &Package{
7167
Path: pkg.Path,
72-
Synopsis: pkg.Synopsis,
7368
IsRedistributable: pkg.IsRedistributable,
7469
Licenses: transformLicenseMetadata(pkg.Licenses),
7570
Module: *m,
7671
URL: constructPackageURL(pkg.Path, mi.ModulePath, urlVersion),
7772
LatestURL: constructPackageURL(pkg.Path, mi.ModulePath, middleware.LatestVersionPlaceholder),
78-
}, nil
79-
}
80-
81-
// createPackageNew returns a *Package based on the fields of the specified
82-
// internal package and version info.
83-
//
84-
// latestRequested indicates whether the user requested the latest
85-
// version of the package. If so, the returned Package.URL will have the
86-
// structure /<path> instead of /<path>@<version>.
87-
func createPackageNew(vdir *internal.VersionedDirectory, latestRequested bool) (_ *Package, err error) {
88-
defer derrors.Wrap(&err, "createPackageNew(%v, %t)", vdir, latestRequested)
89-
90-
if vdir == nil || vdir.Package == nil {
91-
return nil, fmt.Errorf("package info must not be nil")
92-
}
93-
94-
var modLicenses []*licenses.Metadata
95-
for _, lm := range vdir.Licenses {
96-
if path.Dir(lm.FilePath) == "." {
97-
modLicenses = append(modLicenses, lm)
98-
}
99-
}
100-
101-
m := createModule(&vdir.ModuleInfo, modLicenses, latestRequested)
102-
urlVersion := m.LinkVersion
103-
if latestRequested {
104-
urlVersion = internal.LatestVersion
105-
}
106-
return &Package{
107-
Path: vdir.Path,
108-
Synopsis: vdir.Package.Documentation.Synopsis,
109-
IsRedistributable: vdir.DirectoryNew.IsRedistributable,
110-
Licenses: transformLicenseMetadata(vdir.Licenses),
111-
Module: *m,
112-
URL: constructPackageURL(vdir.Path, vdir.ModulePath, urlVersion),
113-
LatestURL: constructPackageURL(vdir.Path, vdir.ModulePath, middleware.LatestVersionPlaceholder),
73+
Synopsis: pkg.Synopsis,
11474
}, nil
11575
}
11676

@@ -160,15 +120,15 @@ func constructPackageURL(pkgPath, modulePath, linkVersion string) string {
160120
}
161121

162122
// effectiveName returns either the command name or package name.
163-
func effectiveName(pkg *internal.LegacyPackage) string {
164-
if pkg.Name != "main" {
165-
return pkg.Name
123+
func effectiveName(pkgPath, pkgName string) string {
124+
if pkgName != "main" {
125+
return pkgName
166126
}
167127
var prefix string // package path without version
168-
if pkg.Path[len(pkg.Path)-3:] == "/v1" {
169-
prefix = pkg.Path[:len(pkg.Path)-3]
128+
if pkgPath[len(pkgPath)-3:] == "/v1" {
129+
prefix = pkgPath[:len(pkgPath)-3]
170130
} else {
171-
prefix, _, _ = module.SplitPathVersion(pkg.Path)
131+
prefix, _, _ = module.SplitPathVersion(pkgPath)
172132
}
173133
_, base := path.Split(prefix)
174134
return base
@@ -196,7 +156,7 @@ func packageHTMLTitle(pkg *internal.LegacyPackage) string {
196156
if pkg.Name != "main" {
197157
return pkg.Name + " package"
198158
}
199-
return effectiveName(pkg) + " command"
159+
return effectiveName(pkg.Path, pkg.Name) + " command"
200160
}
201161

202162
// packageHTMLTitleNew constructs the details page title for pkg.
@@ -215,7 +175,7 @@ func packageTitle(pkg *internal.LegacyPackage) string {
215175
if pkg.Name != "main" {
216176
return "package " + pkg.Name
217177
}
218-
return "command " + effectiveName(pkg)
178+
return "command " + effectiveName(pkg.Path, pkg.Name)
219179
}
220180

221181
// packageTitleNew returns the package title as it will

internal/frontend/header_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
func samplePackage(mutators ...func(*Package)) *Package {
2020
p := &Package{
2121
Path: sample.PackagePath,
22-
Synopsis: sample.Synopsis,
2322
IsRedistributable: true,
2423
Licenses: transformLicenseMetadata(sample.LicenseMetadata),
24+
Synopsis: sample.Synopsis,
2525
Module: Module{
2626
DisplayVersion: sample.VersionString,
2727
LinkVersion: sample.VersionString,
@@ -91,7 +91,7 @@ func TestElapsedTime(t *testing.T) {
9191
}
9292
}
9393

94-
func TestCreatePackageHeader(t *testing.T) {
94+
func TestCreatePackage(t *testing.T) {
9595
vpkg := func(modulePath, suffix, name string) *internal.LegacyVersionedPackage {
9696
vp := &internal.LegacyVersionedPackage{
9797
LegacyModuleInfo: *sample.LegacyModuleInfo(modulePath, sample.VersionString),
@@ -136,12 +136,13 @@ func TestCreatePackageHeader(t *testing.T) {
136136
},
137137
} {
138138
t.Run(tc.label, func(t *testing.T) {
139-
got, err := legacyCreatePackage(&tc.pkg.LegacyPackage, &tc.pkg.ModuleInfo, false)
139+
pm := internal.PackageMetaFromLegacyPackage(&tc.pkg.LegacyPackage)
140+
got, err := createPackage(pm, &tc.pkg.ModuleInfo, false)
140141
if err != nil {
141142
t.Fatal(err)
142143
}
143144
if diff := cmp.Diff(tc.wantPkg, got, cmp.AllowUnexported(safehtml.Identifier{})); diff != "" {
144-
t.Errorf("legacyCreatePackage(%v) mismatch (-want +got):\n%s", tc.pkg, diff)
145+
t.Errorf("createPackage(%v) mismatch (-want +got):\n%s", tc.pkg, diff)
145146
}
146147
})
147148
}

internal/frontend/package.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ func (s *Server) legacyServePackagePageWithPackage(ctx context.Context, w http.R
8888
derrors.Wrap(&err, "legacyServePackagePageWithPackage(w, r, %q, %q, %q)", pkg.Path, pkg.ModulePath, requestedVersion)
8989
}
9090
}()
91-
pkgHeader, err := legacyCreatePackage(&pkg.LegacyPackage, &pkg.ModuleInfo, requestedVersion == internal.LatestVersion)
91+
pkgHeader, err := createPackage(
92+
internal.PackageMetaFromLegacyPackage(&pkg.LegacyPackage),
93+
&pkg.ModuleInfo,
94+
requestedVersion == internal.LatestVersion)
9295
if err != nil {
9396
return fmt.Errorf("creating package header for %s@%s: %v", pkg.Path, pkg.Version, err)
9497
}
@@ -155,7 +158,15 @@ func (s *Server) stdlibPathForShortcut(ctx context.Context, shortcut string) (pa
155158

156159
func (s *Server) servePackagePageWithVersionedDirectory(ctx context.Context,
157160
w http.ResponseWriter, r *http.Request, vdir *internal.VersionedDirectory, requestedVersion string) error {
158-
pkgHeader, err := createPackageNew(vdir, requestedVersion == internal.LatestVersion)
161+
pkgHeader, err := createPackage(&internal.PackageMeta{
162+
DirectoryMeta: internal.DirectoryMeta{
163+
Path: vdir.Path,
164+
V1Path: vdir.V1Path,
165+
Licenses: vdir.Licenses,
166+
IsRedistributable: vdir.IsRedistributable,
167+
},
168+
Name: vdir.Package.Name,
169+
Synopsis: vdir.Package.Documentation.Synopsis}, &vdir.ModuleInfo, requestedVersion == internal.LatestVersion)
159170
if err != nil {
160171
return fmt.Errorf("creating package header for %s@%s: %v", vdir.Path, vdir.Version, err)
161172
}

0 commit comments

Comments
 (0)