diff options
| author | Ethan Lee <ethanalee@google.com> | 2026-03-25 18:18:15 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-26 14:02:59 -0700 |
| commit | 34edebc0803b121acd1cbf363eef074e0a13ab6d (patch) | |
| tree | 72af43d88fd8e6c83d93beae0dba3cc62395e70d /internal/testing | |
| parent | 8378ff811c1228f6a50808cb7557e6c08782205a (diff) | |
| download | go-x-pkgsite-34edebc0803b121acd1cbf363eef074e0a13ab6d.tar.xz | |
internal: consolidate build context and unit resolution logic
- Centralize logic for resolving units and selecting the best matching
build context.
- New helpers MatchingBuildContext, SortedBuildContexts provide a
consistent, efficient approach to build context selection.
- DocumentationForBuildContext now finds the best match based on
precdence rules rather than just returning the first match.
- A new getUnitContext helper resolves unit metadata and the best build
context in a single query.
- GetSymbols and GetUnit are now both optimized to use pre-resolved IDs,
simplifying their queries and removing redundant joins.
- Replaced redundant matchingDoc functions in fetchdatasource and
fakedatasource.
Change-Id: I9207d3bfe03404483c816090a0b99666f14a36e3
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/759162
Reviewed-by: Jonathan Amsterdam <jba@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ethan Lee <ethanalee@google.com>
Diffstat (limited to 'internal/testing')
| -rw-r--r-- | internal/testing/fakedatasource/fakedatasource.go | 34 |
1 files changed, 6 insertions, 28 deletions
diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go index b44212a0..1075d147 100644 --- a/internal/testing/fakedatasource/fakedatasource.go +++ b/internal/testing/fakedatasource/fakedatasource.go @@ -140,13 +140,13 @@ func (ds *FakeDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, fi // It can be a shallow copy, since we're only modifying the Unit.Documentation field. u2 := *u if fields&internal.WithDocsSource != 0 { - if d := matchingDoc(u.Documentation, bc); d != nil { + if d := internal.DocumentationForBuildContext(u.Documentation, bc); d != nil { u2.Documentation = []*internal.Documentation{d} } else { u2.Documentation = nil } } else if fields&internal.WithMain != 0 { - if d := matchingDoc(u.Documentation, bc); d != nil { + if d := internal.DocumentationForBuildContext(u.Documentation, bc); d != nil { u2.Documentation = []*internal.Documentation{{GOOS: d.GOOS, GOARCH: d.GOARCH, Synopsis: d.Synopsis}} } else { u2.Documentation = nil @@ -157,23 +157,6 @@ func (ds *FakeDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, fi return &u2, nil } -// matchingDoc returns the Documentation that matches the given build context -// and comes earliest in build-context order. It returns nil if there is none. -func matchingDoc(docs []*internal.Documentation, bc internal.BuildContext) *internal.Documentation { - var ( - dMin *internal.Documentation - bcMin *internal.BuildContext // sorts last - ) - for _, d := range docs { - dbc := d.BuildContext() - if bc.Match(dbc) && (bcMin == nil || internal.CompareBuildContexts(dbc, *bcMin) < 0) { - dMin = d - bcMin = &dbc - } - } - return dMin -} - // GetUnitMeta returns information about a path. func (ds *FakeDataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) { module := ds.findModule(path, requestedModulePath, requestedVersion) @@ -350,19 +333,14 @@ func (ds *FakeDataSource) GetSymbols(ctx context.Context, pkgPath, modulePath, v var bcs []internal.BuildContext for b := range u.Symbols { - if bc.Match(b) { - bcs = append(bcs, b) - } + bcs = append(bcs, b) } - if len(bcs) == 0 { + matchedBC, ok := internal.MatchingBuildContext(bcs, bc) + if !ok { return nil, derrors.NotFound } - sort.Slice(bcs, func(i, j int) bool { - return internal.CompareBuildContexts(bcs[i], bcs[j]) < 0 - }) - - return u.Symbols[bcs[0]], nil + return u.Symbols[matchedBC], nil } // SearchSupport reports the search types supported by this datasource. |
