From 9e605dc9e8da545178f0a093bf3dd6754d52e3db Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Wed, 5 Jan 2022 12:59:20 -0500 Subject: internal/fetchdatasource: add directory information When fetching a module, populate information about unit subdirectories. Normally (when using a DB) we don't need to do this because a DB query synthesizes it from the units table, but FetchDataSource returns information for immediate consumption so it has to do it on its own. One difference between this and the DB path: this won't have submodules, since FetchDataSource processes one module at a time. Fixes golang/go#49847 Change-Id: I7a215e03dd9cebe31a14c3c118c42978f3f7c8a3 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/375714 Trust: Jonathan Amsterdam Run-TryBot: Jonathan Amsterdam Reviewed-by: Julie Qiu --- internal/fetchdatasource/fetchdatasource.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'internal/fetchdatasource/fetchdatasource.go') diff --git a/internal/fetchdatasource/fetchdatasource.go b/internal/fetchdatasource/fetchdatasource.go index 4af79cc0..87a69fb1 100644 --- a/internal/fetchdatasource/fetchdatasource.go +++ b/internal/fetchdatasource/fetchdatasource.go @@ -108,6 +108,13 @@ func (ds *FetchDataSource) getModule(ctx context.Context, modulePath, vers strin lmv.PopulateModuleInfo(&m.ModuleInfo) } } + // Populate unit subdirectories. When we use a database, this only happens when we read + // a unit from the DB. + if m != nil { + for _, u := range m.Units { + ds.populateUnitSubdirectories(u, m) + } + } // Cache both successes and failures, but not cancellations. if !errors.Is(err, context.Canceled) { @@ -150,6 +157,25 @@ func (ds *FetchDataSource) fetch(ctx context.Context, modulePath, version string return nil, fmt.Errorf("%s@%s: %w", modulePath, version, derrors.NotFound) } +func (ds *FetchDataSource) populateUnitSubdirectories(u *internal.Unit, m *internal.Module) { + p := u.Path + "/" + for _, u2 := range m.Units { + if strings.HasPrefix(u2.Path, p) { + var syn string + if len(u2.Documentation) > 0 { + syn = u2.Documentation[0].Synopsis + } + u.Subdirectories = append(u.Subdirectories, &internal.PackageMeta{ + Path: u2.Path, + Name: u2.Name, + Synopsis: syn, + IsRedistributable: u2.IsRedistributable, + Licenses: u2.Licenses, + }) + } + } +} + // findModule finds the module with longest module path containing the given // package path. It returns an error if no module is found. func (ds *FetchDataSource) findModule(ctx context.Context, pkgPath, modulePath, version string) (_ *internal.Module, err error) { -- cgit v1.3