From 34edebc0803b121acd1cbf363eef074e0a13ab6d Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 25 Mar 2026 18:18:15 +0000 Subject: 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 kokoro-CI: kokoro LUCI-TryBot-Result: Go LUCI Auto-Submit: Ethan Lee --- internal/fetchdatasource/fetchdatasource.go | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) (limited to 'internal/fetchdatasource/fetchdatasource.go') diff --git a/internal/fetchdatasource/fetchdatasource.go b/internal/fetchdatasource/fetchdatasource.go index 7a5f5669..dfe30fc9 100644 --- a/internal/fetchdatasource/fetchdatasource.go +++ b/internal/fetchdatasource/fetchdatasource.go @@ -220,7 +220,7 @@ func (ds *FetchDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, f // Since we cache the module and its units, we have to copy this unit before we modify it. // It can be a shallow copy, since we're only modifying the Unit.Documentation field. u2 := *u - 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 @@ -252,23 +252,6 @@ func findUnitMeta(m *fetch.LazyModule, path string) (*internal.UnitMeta, error) return nil, derrors.NotFound } -// 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{GOOS: "unk", GOARCH: "unk"} // sorts last - ) - for _, d := range docs { - dbc := d.BuildContext() - if bc.Match(dbc) && internal.CompareBuildContexts(dbc, bcMin) < 0 { - dMin = d - bcMin = dbc - } - } - return dMin -} - // GetLatestInfo returns latest information for unitPath and modulePath. func (ds *FetchDataSource) GetLatestInfo(ctx context.Context, unitPath, modulePath string, latestUnitMeta *internal.UnitMeta) (latest internal.LatestInfo, err error) { defer derrors.Wrap(&err, "FetchDataSource.GetLatestInfo(ctx, %q, %q)", unitPath, modulePath) @@ -447,7 +430,7 @@ func (ds *FetchDataSource) GetSymbols(ctx context.Context, pkgPath, modulePath, return nil, err } - doc := matchingDoc(unit.Documentation, bc) + doc := internal.DocumentationForBuildContext(unit.Documentation, bc) if doc == nil || len(doc.API) == 0 { return nil, derrors.NotFound } -- cgit v1.3-5-g45d5