Skip to content

Commit d2458ea

Browse files
committed
internal/datasource: factor out GetUnit and GetUnitMeta
Move both methods into the shared datasource, and clean up the logic. For golang/go#47780 Change-Id: I7ce0d29742652c63db2d2e55dedf6e378b838445 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/344950 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent e71d1f3 commit d2458ea

File tree

6 files changed

+92
-146
lines changed

6 files changed

+92
-146
lines changed

internal/datasource/datasource.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"errors"
1313
"fmt"
14+
"strings"
1415
"time"
1516

1617
lru "github.com/hashicorp/golang-lru"
@@ -135,3 +136,68 @@ func (ds *dataSource) fetch(ctx context.Context, modulePath, version string) (_
135136
}
136137
return nil, fmt.Errorf("%s@%s: %w", modulePath, version, derrors.NotFound)
137138
}
139+
140+
// findModule finds the module with longest module path containing the given
141+
// package path. It returns an error if no module is found.
142+
func (ds *dataSource) findModule(ctx context.Context, pkgPath, modulePath, version string) (_ *internal.Module, err error) {
143+
defer derrors.Wrap(&err, "findModule(%q, %q, %q)", pkgPath, modulePath, version)
144+
145+
if modulePath != internal.UnknownModulePath {
146+
return ds.getModule(ctx, modulePath, version)
147+
}
148+
pkgPath = strings.TrimLeft(pkgPath, "/")
149+
for _, modulePath := range internal.CandidateModulePaths(pkgPath) {
150+
m, err := ds.getModule(ctx, modulePath, version)
151+
if err == nil {
152+
return m, nil
153+
}
154+
if !errors.Is(err, derrors.NotFound) {
155+
return nil, err
156+
}
157+
}
158+
return nil, fmt.Errorf("could not find module for import path %s: %w", pkgPath, derrors.NotFound)
159+
}
160+
161+
// GetUnitMeta returns information about a path.
162+
func (ds *dataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
163+
defer derrors.Wrap(&err, "GetUnitMeta(%q, %q, %q)", path, requestedModulePath, requestedVersion)
164+
165+
module, err := ds.findModule(ctx, path, requestedModulePath, requestedVersion)
166+
if err != nil {
167+
return nil, err
168+
}
169+
um := &internal.UnitMeta{
170+
Path: path,
171+
ModuleInfo: module.ModuleInfo,
172+
}
173+
if u := findUnit(module, path); u != nil {
174+
um.Name = u.Name
175+
um.IsRedistributable = u.IsRedistributable
176+
}
177+
return um, nil
178+
}
179+
180+
// GetUnit returns information about a unit. Both the module path and package
181+
// path must be known.
182+
func (ds *dataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, fields internal.FieldSet, bc internal.BuildContext) (_ *internal.Unit, err error) {
183+
defer derrors.Wrap(&err, "GetUnit(%q, %q)", um.Path, um.ModulePath)
184+
185+
m, err := ds.getModule(ctx, um.ModulePath, um.Version)
186+
if err != nil {
187+
return nil, err
188+
}
189+
if u := findUnit(m, um.Path); u != nil {
190+
return u, nil
191+
}
192+
return nil, fmt.Errorf("import path %s not found in module %s: %w", um.Path, um.ModulePath, derrors.NotFound)
193+
}
194+
195+
// findUnit returns the unit with the given path in m, or nil if none.
196+
func findUnit(m *internal.Module, path string) *internal.Unit {
197+
for _, u := range m.Units {
198+
if u.Path == path {
199+
return u
200+
}
201+
}
202+
return nil
203+
}

internal/datasource/local.go

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ package datasource
66

77
import (
88
"context"
9-
"errors"
109
"fmt"
1110
"os"
1211
"path/filepath"
13-
"strings"
1412

1513
"golang.org/x/pkgsite/internal"
1614
"golang.org/x/pkgsite/internal/derrors"
@@ -61,66 +59,14 @@ func getFullPath(modulePath string) string {
6159
return ""
6260
}
6361

64-
// GetUnit returns information about a unit. Both the module path and package
65-
// path must be known.
66-
func (ds *LocalDataSource) GetUnit(ctx context.Context, pathInfo *internal.UnitMeta, fields internal.FieldSet, bc internal.BuildContext) (_ *internal.Unit, err error) {
67-
defer derrors.Wrap(&err, "GetUnit(%q, %q)", pathInfo.Path, pathInfo.ModulePath)
68-
69-
module, err := ds.ds.getModule(ctx, pathInfo.ModulePath, pathInfo.Version)
70-
if err != nil {
71-
return nil, err
72-
}
73-
for _, unit := range module.Units {
74-
if unit.Path == pathInfo.Path {
75-
return unit, nil
76-
}
77-
}
78-
79-
return nil, fmt.Errorf("import path %s not found in module %s: %w", pathInfo.Path, pathInfo.ModulePath, derrors.NotFound)
80-
}
81-
82-
// GetUnitMeta returns information about a path.
8362
func (ds *LocalDataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
84-
defer derrors.Wrap(&err, "GetUnitMeta(%q, %q, %q)", path, requestedModulePath, requestedVersion)
85-
86-
module, err := ds.findModule(ctx, path, requestedModulePath, requestedVersion)
87-
if err != nil {
88-
return nil, err
89-
}
90-
um := &internal.UnitMeta{
91-
Path: path,
92-
ModuleInfo: module.ModuleInfo,
93-
}
94-
95-
for _, u := range module.Units {
96-
if u.Path == path {
97-
um.Name = u.Name
98-
um.IsRedistributable = u.IsRedistributable
99-
}
100-
}
101-
102-
return um, nil
63+
return ds.ds.GetUnitMeta(ctx, path, requestedModulePath, requestedVersion)
10364
}
10465

105-
// findModule finds the module with longest module path containing the given
106-
// package path. It returns an error if no module is found.
107-
func (ds *LocalDataSource) findModule(ctx context.Context, pkgPath, modulePath, version string) (_ *internal.Module, err error) {
108-
defer derrors.Wrap(&err, "findModule(%q, %q, %q)", pkgPath, modulePath, version)
109-
110-
if modulePath != internal.UnknownModulePath {
111-
return ds.ds.getModule(ctx, modulePath, version)
112-
}
113-
pkgPath = strings.TrimLeft(pkgPath, "/")
114-
for _, modulePath := range internal.CandidateModulePaths(pkgPath) {
115-
m, err := ds.ds.getModule(ctx, modulePath, version)
116-
if err == nil {
117-
return m, nil
118-
}
119-
if !errors.Is(err, derrors.NotFound) {
120-
return nil, err
121-
}
122-
}
123-
return nil, fmt.Errorf("could not find module for import path %s: %w", pkgPath, derrors.NotFound)
66+
// GetUnit returns information about a unit. Both the module path and package
67+
// path must be known.
68+
func (ds *LocalDataSource) GetUnit(ctx context.Context, pathInfo *internal.UnitMeta, fields internal.FieldSet, bc internal.BuildContext) (_ *internal.Unit, err error) {
69+
return ds.ds.GetUnit(ctx, pathInfo, fields, bc)
12470
}
12571

12672
// GetLatestInfo is not implemented.

internal/datasource/proxy.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,6 @@ type ProxyDataSource struct {
5252
ds *dataSource
5353
}
5454

55-
// findModule finds the longest module path containing the given package path,
56-
// using the given finder func and iteratively testing parent directories of
57-
// the import path. It performs no testing as to whether the specified module
58-
// version that was found actually contains a package corresponding to pkgPath.
59-
func (ds *ProxyDataSource) findModule(ctx context.Context, pkgPath string, version string) (_ string, _ *proxy.VersionInfo, err error) {
60-
defer derrors.Wrap(&err, "findModule(%q, ...)", pkgPath)
61-
pkgPath = strings.TrimLeft(pkgPath, "/")
62-
for _, modulePath := range internal.CandidateModulePaths(pkgPath) {
63-
info, err := ds.ds.prox.Info(ctx, modulePath, version)
64-
if errors.Is(err, derrors.NotFound) {
65-
continue
66-
}
67-
if err != nil {
68-
return "", nil, err
69-
}
70-
return modulePath, info, nil
71-
}
72-
return "", nil, fmt.Errorf("unable to find module: %w", derrors.NotFound)
73-
}
74-
75-
// getUnit returns information about a unit.
76-
func (ds *ProxyDataSource) getUnit(ctx context.Context, fullPath, modulePath, version string, _ internal.BuildContext) (_ *internal.Unit, err error) {
77-
var m *internal.Module
78-
m, err = ds.ds.getModule(ctx, modulePath, version)
79-
if err != nil {
80-
return nil, err
81-
}
82-
for _, d := range m.Units {
83-
if d.Path == fullPath {
84-
return d, nil
85-
}
86-
}
87-
return nil, fmt.Errorf("%q missing from module %s: %w", fullPath, m.ModulePath, derrors.NotFound)
88-
}
89-
9055
// GetLatestInfo returns latest information for unitPath and modulePath.
9156
func (ds *ProxyDataSource) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) (latest internal.LatestInfo, err error) {
9257
defer derrors.Wrap(&err, "GetLatestInfo(ctx, %q, %q)", unitPath, modulePath)

internal/datasource/proxy_details.go

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99

1010
"golang.org/x/pkgsite/internal"
1111
"golang.org/x/pkgsite/internal/derrors"
12-
"golang.org/x/pkgsite/internal/proxy"
1312
)
1413

1514
// GetUnit returns information about a directory at a path.
1615
func (ds *ProxyDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, field internal.FieldSet, bc internal.BuildContext) (_ *internal.Unit, err error) {
1716
defer derrors.Wrap(&err, "GetUnit(%q, %q, %q)", um.Path, um.ModulePath, um.Version)
18-
return ds.getUnit(ctx, um.Path, um.ModulePath, um.Version, bc)
17+
return ds.ds.GetUnit(ctx, um, field, bc)
1918
}
2019

2120
// GetModuleInfo returns the ModuleInfo as fetched from the proxy for module
@@ -29,38 +28,8 @@ func (ds *ProxyDataSource) GetModuleInfo(ctx context.Context, modulePath, versio
2928
return &m.ModuleInfo, nil
3029
}
3130

32-
// GetUnitMeta returns information about the given path.
33-
func (ds *ProxyDataSource) GetUnitMeta(ctx context.Context, path, inModulePath, inVersion string) (_ *internal.UnitMeta, err error) {
34-
defer derrors.Wrap(&err, "GetUnitMeta(%q, %q, %q)", path, inModulePath, inVersion)
35-
36-
var info *proxy.VersionInfo
37-
if inModulePath == internal.UnknownModulePath {
38-
inModulePath, info, err = ds.findModule(ctx, path, inVersion)
39-
if err != nil {
40-
return nil, err
41-
}
42-
inVersion = info.Version
43-
}
44-
m, err := ds.ds.getModule(ctx, inModulePath, inVersion)
45-
if err != nil {
46-
return nil, err
47-
}
48-
um := &internal.UnitMeta{
49-
Path: path,
50-
ModuleInfo: internal.ModuleInfo{
51-
ModulePath: inModulePath,
52-
Version: inVersion,
53-
IsRedistributable: m.IsRedistributable,
54-
},
55-
}
56-
for _, d := range m.Units {
57-
if d.Path == path {
58-
um.Name = d.Name
59-
um.IsRedistributable = d.IsRedistributable
60-
break
61-
}
62-
}
63-
return um, nil
31+
func (ds *ProxyDataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
32+
return ds.ds.GetUnitMeta(ctx, path, requestedModulePath, requestedVersion)
6433
}
6534

6635
// GetExperiments is unimplemented.

internal/datasource/proxy_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ func TestProxyGetUnitMeta(t *testing.T) {
128128
ctx, ds, teardown := setup(t, false)
129129
defer teardown()
130130

131+
singleModInfo := internal.ModuleInfo{
132+
ModulePath: "example.com/single",
133+
Version: "v1.0.0",
134+
IsRedistributable: true,
135+
CommitTime: proxytest.CommitTime,
136+
HasGoMod: true,
137+
}
138+
131139
for _, test := range []struct {
132140
path, modulePath, version string
133141
want *internal.UnitMeta
@@ -137,11 +145,7 @@ func TestProxyGetUnitMeta(t *testing.T) {
137145
modulePath: "example.com/single",
138146
version: "v1.0.0",
139147
want: &internal.UnitMeta{
140-
ModuleInfo: internal.ModuleInfo{
141-
ModulePath: "example.com/single",
142-
Version: "v1.0.0",
143-
IsRedistributable: true,
144-
},
148+
ModuleInfo: singleModInfo,
145149
IsRedistributable: true,
146150
},
147151
},
@@ -150,11 +154,7 @@ func TestProxyGetUnitMeta(t *testing.T) {
150154
modulePath: "example.com/single",
151155
version: "v1.0.0",
152156
want: &internal.UnitMeta{
153-
ModuleInfo: internal.ModuleInfo{
154-
ModulePath: "example.com/single",
155-
Version: "v1.0.0",
156-
IsRedistributable: true,
157-
},
157+
ModuleInfo: singleModInfo,
158158
Name: "pkg",
159159
IsRedistributable: true,
160160
},
@@ -164,11 +164,7 @@ func TestProxyGetUnitMeta(t *testing.T) {
164164
modulePath: internal.UnknownModulePath,
165165
version: "v1.0.0",
166166
want: &internal.UnitMeta{
167-
ModuleInfo: internal.ModuleInfo{
168-
ModulePath: "example.com/single",
169-
Version: "v1.0.0",
170-
IsRedistributable: true,
171-
},
167+
ModuleInfo: singleModInfo,
172168
Name: "pkg",
173169
IsRedistributable: true,
174170
},
@@ -182,6 +178,8 @@ func TestProxyGetUnitMeta(t *testing.T) {
182178
ModulePath: "example.com/basic",
183179
Version: "v1.1.0",
184180
IsRedistributable: true,
181+
CommitTime: proxytest.CommitTime,
182+
HasGoMod: true,
185183
},
186184
Name: "basic",
187185
IsRedistributable: true,
@@ -194,7 +192,7 @@ func TestProxyGetUnitMeta(t *testing.T) {
194192
t.Fatal(err)
195193
}
196194
test.want.Path = test.path
197-
if diff := cmp.Diff(got, test.want); diff != "" {
195+
if diff := cmp.Diff(test.want, got, cmpopts.IgnoreFields(internal.ModuleInfo{}, "SourceInfo")); diff != "" {
198196
t.Errorf("mismatch (-want +got):\n%s", diff)
199197
}
200198
})

internal/proxy/proxytest/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (s *Server) ZipRequests() int {
167167
return s.zipRequests
168168
}
169169

170-
const versionTime = "2019-01-30T00:00:00Z"
170+
// CommitTime is the time returned by all calls to the .info endpoint.
171+
var CommitTime = time.Date(2019, time.January, 30, 0, 0, 0, 0, time.UTC)
171172

172173
func cleanModule(m *Module) *Module {
173174
if m.Version == "" {
@@ -188,5 +189,6 @@ func cleanModule(m *Module) *Module {
188189
}
189190

190191
func defaultInfo(resolvedVersion string) *strings.Reader {
191-
return strings.NewReader(fmt.Sprintf("{\n\t\"Version\": %q,\n\t\"Time\": %q\n}", resolvedVersion, versionTime))
192+
return strings.NewReader(fmt.Sprintf("{\n\t\"Version\": %q,\n\t\"Time\": %q\n}",
193+
resolvedVersion, CommitTime.Format(time.RFC3339)))
192194
}

0 commit comments

Comments
 (0)