Skip to content

Commit 2deba2b

Browse files
committed
internal/proxydatasource: move exported functions to details.go
Functions in internal/proxydatasource that are part of the DataSource interface are moved to details.go for better readability. This CL is only movement, no code changes are made. For golang/go#39629 Change-Id: I24a1cb21d93c30e6017e9edcfd90659ee5876191 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/250678 Run-TryBot: Julie Qiu <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent 8b0a579 commit 2deba2b

File tree

2 files changed

+114
-96
lines changed

2 files changed

+114
-96
lines changed

internal/proxydatasource/datasource.go

-96
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"golang.org/x/pkgsite/internal"
2121
"golang.org/x/pkgsite/internal/derrors"
2222
"golang.org/x/pkgsite/internal/fetch"
23-
"golang.org/x/pkgsite/internal/licenses"
2423
"golang.org/x/pkgsite/internal/proxy"
2524
"golang.org/x/pkgsite/internal/source"
2625
"golang.org/x/pkgsite/internal/stdlib"
@@ -78,60 +77,6 @@ type versionEntry struct {
7877
err error
7978
}
8079

81-
// GetDirectory returns information about a directory at a path.
82-
func (ds *DataSource) GetDirectory(ctx context.Context, fullPath, modulePath, version string) (_ *internal.Directory, err error) {
83-
defer derrors.Wrap(&err, "GetDirectory(%q, %q, %q)", fullPath, modulePath, version)
84-
return ds.directoryFromVersion(ctx, fullPath, modulePath, version)
85-
}
86-
87-
// GetImports returns package imports as extracted from the module zip.
88-
func (ds *DataSource) GetImports(ctx context.Context, pkgPath, modulePath, version string) (_ []string, err error) {
89-
defer derrors.Wrap(&err, "GetImports(%q, %q, %q)", pkgPath, modulePath, version)
90-
vp, err := ds.LegacyGetPackage(ctx, pkgPath, modulePath, version)
91-
if err != nil {
92-
return nil, err
93-
}
94-
return vp.Imports, nil
95-
}
96-
97-
// GetLicenses return licenses at path for the given module path and version.
98-
func (ds *DataSource) GetLicenses(ctx context.Context, fullPath, modulePath, resolvedVersion string) (_ []*licenses.License, err error) {
99-
defer derrors.Wrap(&err, "GetLicenses(%q, %q, %q)", fullPath, modulePath, resolvedVersion)
100-
v, err := ds.getModule(ctx, modulePath, resolvedVersion)
101-
if err != nil {
102-
return nil, err
103-
}
104-
105-
var lics []*licenses.License
106-
107-
// ds.getModule() returns all licenses for the module version. We need to
108-
// filter the licenses that applies to the specified fullPath, i.e.
109-
// A license in the current or any parent directory of the specified
110-
// fullPath applies to it.
111-
for _, license := range v.Licenses {
112-
licensePath := path.Join(modulePath, path.Dir(license.FilePath))
113-
if strings.HasPrefix(fullPath, licensePath) {
114-
lics = append(lics, license)
115-
}
116-
}
117-
118-
if len(lics) == 0 {
119-
return nil, fmt.Errorf("path %s is missing from module %s: %w", fullPath, modulePath, derrors.NotFound)
120-
}
121-
return lics, nil
122-
}
123-
124-
// GetModuleInfo returns the ModuleInfo as fetched from the proxy for module
125-
// version specified by modulePath and version.
126-
func (ds *DataSource) GetModuleInfo(ctx context.Context, modulePath, version string) (_ *internal.ModuleInfo, err error) {
127-
defer derrors.Wrap(&err, "GetModuleInfo(%q, %q)", modulePath, version)
128-
m, err := ds.getModule(ctx, modulePath, version)
129-
if err != nil {
130-
return nil, err
131-
}
132-
return &m.ModuleInfo, nil
133-
}
134-
13580
// getModule retrieves a version from the cache, or failing that queries and
13681
// processes the version from the proxy.
13782
func (ds *DataSource) getModule(ctx context.Context, modulePath, version string) (_ *internal.Module, err error) {
@@ -320,47 +265,6 @@ func packageFromVersion(pkgPath string, m *internal.Module) (_ *internal.LegacyV
320265
return nil, fmt.Errorf("package missing from module %s: %w", m.ModulePath, derrors.NotFound)
321266
}
322267

323-
// GetExperiments is unimplemented.
324-
func (*DataSource) GetExperiments(ctx context.Context) ([]*internal.Experiment, error) {
325-
return nil, nil
326-
}
327-
328-
// GetPathInfo returns information about the given path.
329-
func (ds *DataSource) GetPathInfo(ctx context.Context, path, inModulePath, inVersion string) (outModulePath, outVersion string, isPackage bool, err error) {
330-
defer derrors.Wrap(&err, "GetPathInfo(%q, %q, %q)", path, inModulePath, inVersion)
331-
332-
var info *proxy.VersionInfo
333-
if inModulePath == internal.UnknownModulePath {
334-
inModulePath, info, err = ds.findModule(ctx, path, inVersion)
335-
if err != nil {
336-
return "", "", false, err
337-
}
338-
inVersion = info.Version
339-
}
340-
m, err := ds.getModule(ctx, inModulePath, inVersion)
341-
if err != nil {
342-
return "", "", false, err
343-
}
344-
isPackage = false
345-
for _, p := range m.LegacyPackages {
346-
if p.Path == path {
347-
isPackage = true
348-
break
349-
}
350-
}
351-
return m.ModulePath, m.Version, isPackage, nil
352-
}
353-
354-
// GetDirectoryMeta returns information about a directory at a path.
355-
func (ds *DataSource) GetDirectoryMeta(ctx context.Context, fullPath, modulePath, version string) (_ *internal.DirectoryMeta, err error) {
356-
defer derrors.Wrap(&err, "GetDirectoryMeta(%q, %q, %q)", fullPath, modulePath, version)
357-
d, err := ds.directoryFromVersion(ctx, fullPath, modulePath, version)
358-
if err != nil {
359-
return nil, err
360-
}
361-
return &d.DirectoryMeta, nil
362-
}
363-
364268
// directoryFromVersion returns information about a directory at a path.
365269
func (ds *DataSource) directoryFromVersion(ctx context.Context, fullPath, modulePath, version string) (_ *internal.Directory, err error) {
366270
var m *internal.Module

internal/proxydatasource/details.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package proxydatasource implements an internal.DataSource backed solely by a
6+
// proxy instance.
7+
package proxydatasource
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"path"
13+
"strings"
14+
15+
"golang.org/x/pkgsite/internal"
16+
"golang.org/x/pkgsite/internal/derrors"
17+
"golang.org/x/pkgsite/internal/licenses"
18+
"golang.org/x/pkgsite/internal/proxy"
19+
)
20+
21+
// GetDirectory returns information about a directory at a path.
22+
func (ds *DataSource) GetDirectory(ctx context.Context, fullPath, modulePath, version string) (_ *internal.Directory, err error) {
23+
defer derrors.Wrap(&err, "GetDirectory(%q, %q, %q)", fullPath, modulePath, version)
24+
return ds.directoryFromVersion(ctx, fullPath, modulePath, version)
25+
}
26+
27+
// GetDirectoryMeta returns information about a directory at a path.
28+
func (ds *DataSource) GetDirectoryMeta(ctx context.Context, fullPath, modulePath, version string) (_ *internal.DirectoryMeta, err error) {
29+
defer derrors.Wrap(&err, "GetDirectoryMeta(%q, %q, %q)", fullPath, modulePath, version)
30+
d, err := ds.directoryFromVersion(ctx, fullPath, modulePath, version)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return &d.DirectoryMeta, nil
35+
}
36+
37+
// GetImports returns package imports as extracted from the module zip.
38+
func (ds *DataSource) GetImports(ctx context.Context, pkgPath, modulePath, version string) (_ []string, err error) {
39+
defer derrors.Wrap(&err, "GetImports(%q, %q, %q)", pkgPath, modulePath, version)
40+
vp, err := ds.LegacyGetPackage(ctx, pkgPath, modulePath, version)
41+
if err != nil {
42+
return nil, err
43+
}
44+
return vp.Imports, nil
45+
}
46+
47+
// GetLicenses return licenses at path for the given module path and version.
48+
func (ds *DataSource) GetLicenses(ctx context.Context, fullPath, modulePath, resolvedVersion string) (_ []*licenses.License, err error) {
49+
defer derrors.Wrap(&err, "GetLicenses(%q, %q, %q)", fullPath, modulePath, resolvedVersion)
50+
v, err := ds.getModule(ctx, modulePath, resolvedVersion)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
var lics []*licenses.License
56+
57+
// ds.getModule() returns all licenses for the module version. We need to
58+
// filter the licenses that applies to the specified fullPath, i.e.
59+
// A license in the current or any parent directory of the specified
60+
// fullPath applies to it.
61+
for _, license := range v.Licenses {
62+
licensePath := path.Join(modulePath, path.Dir(license.FilePath))
63+
if strings.HasPrefix(fullPath, licensePath) {
64+
lics = append(lics, license)
65+
}
66+
}
67+
68+
if len(lics) == 0 {
69+
return nil, fmt.Errorf("path %s is missing from module %s: %w", fullPath, modulePath, derrors.NotFound)
70+
}
71+
return lics, nil
72+
}
73+
74+
// GetModuleInfo returns the ModuleInfo as fetched from the proxy for module
75+
// version specified by modulePath and version.
76+
func (ds *DataSource) GetModuleInfo(ctx context.Context, modulePath, version string) (_ *internal.ModuleInfo, err error) {
77+
defer derrors.Wrap(&err, "GetModuleInfo(%q, %q)", modulePath, version)
78+
m, err := ds.getModule(ctx, modulePath, version)
79+
if err != nil {
80+
return nil, err
81+
}
82+
return &m.ModuleInfo, nil
83+
}
84+
85+
// GetPathInfo returns information about the given path.
86+
func (ds *DataSource) GetPathInfo(ctx context.Context, path, inModulePath, inVersion string) (outModulePath, outVersion string, isPackage bool, err error) {
87+
defer derrors.Wrap(&err, "GetPathInfo(%q, %q, %q)", path, inModulePath, inVersion)
88+
89+
var info *proxy.VersionInfo
90+
if inModulePath == internal.UnknownModulePath {
91+
inModulePath, info, err = ds.findModule(ctx, path, inVersion)
92+
if err != nil {
93+
return "", "", false, err
94+
}
95+
inVersion = info.Version
96+
}
97+
m, err := ds.getModule(ctx, inModulePath, inVersion)
98+
if err != nil {
99+
return "", "", false, err
100+
}
101+
isPackage = false
102+
for _, p := range m.LegacyPackages {
103+
if p.Path == path {
104+
isPackage = true
105+
break
106+
}
107+
}
108+
return m.ModulePath, m.Version, isPackage, nil
109+
}
110+
111+
// GetExperiments is unimplemented.
112+
func (*DataSource) GetExperiments(ctx context.Context) ([]*internal.Experiment, error) {
113+
return nil, nil
114+
}

0 commit comments

Comments
 (0)