Skip to content

Commit bec3740

Browse files
committed
internal: add NewRawLatestInfo method
Consolidate creation of RawLatestInfo in one function. For golang/go#43265 Change-Id: Id57d044c33a678b8ff4e57ada1a5569e09f841d0 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/295897 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent 32228ff commit bec3740

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

internal/fetch/raw_latest.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ package fetch
77
import (
88
"context"
99
"errors"
10-
"fmt"
1110

12-
"golang.org/x/mod/modfile"
1311
"golang.org/x/pkgsite/internal"
1412
"golang.org/x/pkgsite/internal/derrors"
1513
"golang.org/x/pkgsite/internal/log"
@@ -35,15 +33,7 @@ func RawLatestInfo(ctx context.Context, modulePath string, prox *proxy.Client, h
3533
if err != nil {
3634
return nil, err
3735
}
38-
f, err := modfile.ParseLax(fmt.Sprintf("%s@%s/go.mod", modulePath, v), modBytes, nil)
39-
if err != nil {
40-
return nil, err
41-
}
42-
return &internal.RawLatestInfo{
43-
ModulePath: modulePath,
44-
Version: v,
45-
GoModFile: f,
46-
}, nil
36+
return internal.NewRawLatestInfo(modulePath, v, modBytes)
4737
}
4838

4939
// fetchRawLatestVersion uses the proxy to determine the latest

internal/postgres/version.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"time"
1313

1414
"github.com/Masterminds/squirrel"
15-
"golang.org/x/mod/modfile"
1615
"golang.org/x/pkgsite/internal"
1716
"golang.org/x/pkgsite/internal/database"
1817
"golang.org/x/pkgsite/internal/derrors"
@@ -246,26 +245,22 @@ func (db *DB) GetRawLatestInfo(ctx context.Context, modulePath string) (_ *inter
246245
defer derrors.WrapStack(&err, "GetRawLatestInfo(%q)", modulePath)
247246

248247
var (
249-
info internal.RawLatestInfo
248+
version string
250249
goModBytes []byte
251250
)
252251
err = db.db.QueryRow(ctx, `
253-
SELECT p.path, r.version, r.go_mod_bytes
252+
SELECT r.version, r.go_mod_bytes
254253
FROM raw_latest_versions r
255254
INNER JOIN paths p ON p.id = r.module_path_id
256255
WHERE p.path = $1`,
257-
modulePath).Scan(&info.ModulePath, &info.Version, &goModBytes)
256+
modulePath).Scan(&version, &goModBytes)
258257
if err != nil {
259258
if err == sql.ErrNoRows {
260259
return nil, nil
261260
}
262261
return nil, err
263262
}
264-
info.GoModFile, err = modfile.ParseLax(modulePath+" from DB", goModBytes, nil)
265-
if err != nil {
266-
return nil, err
267-
}
268-
return &info, nil
263+
return internal.NewRawLatestInfo(modulePath, version, goModBytes)
269264
}
270265

271266
// UpdateRawLatestInfo upserts its argument into the raw_latest_versions table

internal/postgres/version_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"testing"
1111

1212
"github.com/google/go-cmp/cmp"
13-
"golang.org/x/mod/modfile"
1413
"golang.org/x/pkgsite/internal"
1514
"golang.org/x/pkgsite/internal/experiment"
1615
"golang.org/x/pkgsite/internal/source"
@@ -252,15 +251,10 @@ func TestGetVersions(t *testing.T) {
252251
}
253252

254253
func addRawLatest(ctx context.Context, t *testing.T, db *DB, modulePath, version, modFile string) {
255-
f, err := modfile.ParseLax(modulePath+"@"+version, []byte(modFile), nil)
254+
info, err := internal.NewRawLatestInfo(modulePath, version, []byte(modFile))
256255
if err != nil {
257256
t.Fatal(err)
258257
}
259-
info := &internal.RawLatestInfo{
260-
ModulePath: modulePath,
261-
Version: version,
262-
GoModFile: f,
263-
}
264258
if err := db.UpdateRawLatestInfo(ctx, info); err != nil {
265259
t.Fatal(err)
266260
}
@@ -391,12 +385,10 @@ func TestRawLatestInfo(t *testing.T) {
391385
defer release()
392386
ctx := context.Background()
393387

394-
goMod, err := modfile.ParseLax("m from DB", []byte(`module m`), nil)
388+
info, err := internal.NewRawLatestInfo("m", "v1.0.0", []byte(`module m`))
395389
if err != nil {
396390
t.Fatal(err)
397391
}
398-
399-
info := &internal.RawLatestInfo{ModulePath: "m", Version: "v1.0.0", GoModFile: goMod}
400392
if err := testDB.UpdateRawLatestInfo(ctx, info); err != nil {
401393
t.Fatal(err)
402394
}
@@ -408,7 +400,10 @@ func TestRawLatestInfo(t *testing.T) {
408400
t.Fatalf("mismatch (-want, +got): %s", diff)
409401
}
410402

411-
info.Version = "v1.2.3"
403+
info, err = internal.NewRawLatestInfo("m", "v1.2.3", []byte(`module m`))
404+
if err != nil {
405+
t.Fatal(err)
406+
}
412407
if err := testDB.UpdateRawLatestInfo(ctx, info); err != nil {
413408
t.Fatal(err)
414409
}

internal/raw_latest.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package internal
66

77
import (
8+
"fmt"
89
"strings"
910

1011
"golang.org/x/mod/modfile"
@@ -21,6 +22,18 @@ type RawLatestInfo struct {
2122
GoModFile *modfile.File
2223
}
2324

25+
func NewRawLatestInfo(modulePath, version string, modBytes []byte) (*RawLatestInfo, error) {
26+
f, err := modfile.ParseLax(fmt.Sprintf("%s@%s/go.mod", modulePath, version), modBytes, nil)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return &RawLatestInfo{
31+
ModulePath: modulePath,
32+
Version: version,
33+
GoModFile: f,
34+
}, nil
35+
}
36+
2437
// PopulateModuleInfo uses the RawLatestInfo to populate fields of the given module.
2538
func (r *RawLatestInfo) PopulateModuleInfo(mi *ModuleInfo) {
2639
mi.Deprecated, mi.DeprecationComment = isDeprecated(r.GoModFile)

0 commit comments

Comments
 (0)