Skip to content

Commit

Permalink
Updated with feedback from @brandonc: comments, improve variable name…
Browse files Browse the repository at this point in the history
…s, remove unneed map reallocation
  • Loading branch information
Maed223 committed Jun 7, 2024
1 parent 3914531 commit ea8f696
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
26 changes: 14 additions & 12 deletions sourcebundle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ type Builder struct {
// selected version of each module registry package.
resolvedRegistry map[registryPackageVersion]sourceaddrs.RemoteSource

resolvedRegistryVersionDeprecations map[registryPackageVersion]*RegistryVersionDeprecation
// resolvedRegistryVersionDeprecations tracks potential deprecations for
// each package version
packageVersionDeprecations map[registryPackageVersion]*RegistryVersionDeprecation

// registryPackageVersions caches responses from module registry calls to
// look up the available versions for a particular module package. Although
Expand Down Expand Up @@ -102,15 +104,15 @@ func NewBuilder(targetDir string, fetcher PackageFetcher, registryClient Registr
return nil, fmt.Errorf("invalid target directory: %w", err)
}
return &Builder{
targetDir: absDir,
fetcher: fetcher,
registryClient: registryClient,
analyzed: make(map[remoteArtifact]struct{}),
remotePackageDirs: make(map[sourceaddrs.RemotePackage]string),
remotePackageMeta: make(map[sourceaddrs.RemotePackage]*PackageMeta),
resolvedRegistry: make(map[registryPackageVersion]sourceaddrs.RemoteSource),
resolvedRegistryVersionDeprecations: make(map[registryPackageVersion]*RegistryVersionDeprecation),
registryPackageVersions: make(map[regaddr.ModulePackage][]ModulePackageInfo),
targetDir: absDir,
fetcher: fetcher,
registryClient: registryClient,
analyzed: make(map[remoteArtifact]struct{}),
remotePackageDirs: make(map[sourceaddrs.RemotePackage]string),
remotePackageMeta: make(map[sourceaddrs.RemotePackage]*PackageMeta),
resolvedRegistry: make(map[registryPackageVersion]sourceaddrs.RemoteSource),
packageVersionDeprecations: make(map[registryPackageVersion]*RegistryVersionDeprecation),
registryPackageVersions: make(map[regaddr.ModulePackage][]ModulePackageInfo),
}, nil
}

Expand Down Expand Up @@ -417,7 +419,7 @@ func (b *Builder) findRegistryPackageSource(ctx context.Context, sourceAddr sour
Link: versionDeprecation.Link,
}
}
b.resolvedRegistryVersionDeprecations[pkgVer] = deprecation
b.packageVersionDeprecations[pkgVer] = deprecation

if cb := trace.RegistryPackageSourceSuccess; cb != nil {
cb(reqCtx, pkgAddr, selectedVersion, realSourceAddr)
Expand Down Expand Up @@ -604,7 +606,7 @@ func (b *Builder) writeManifest(filename string) error {
manifestMeta = &root.RegistryMeta[len(root.RegistryMeta)-1]
registryObjs[rpv.pkg] = manifestMeta
}
deprecation := b.resolvedRegistryVersionDeprecations[rpv]
deprecation := b.packageVersionDeprecations[rpv]
manifestMeta.Versions[rpv.version.String()] = manifestRegistryVersion{
SourceAddr: sourceInfo.String(),
Deprecation: deprecation,
Expand Down
8 changes: 4 additions & 4 deletions sourcebundle/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,15 @@ func testingBuilder(t *testing.T, targetDir string, remotePackages map[string]st
if err != nil {
t.Fatalf("invalid registry package address %q: %s", pkgAddrRaw, err)
}
if registryDeprecations[pkgAddr.Namespace] == nil {
registryDeprecations[pkgAddr.Namespace] = make(map[versions.Version]*ModulePackageVersionDeprecation)
}

for versionRaw, versionDeprecation := range deprecations {
version, err := versions.ParseVersion(versionRaw)
if err != nil {
t.Fatalf("invalid registry package version %q for %s: %s", versionRaw, pkgAddr, err)
}
registryDeprecations[pkgAddr.Namespace][version] = versionDeprecation
registryDeprecations[pkgAddr.Namespace] = map[versions.Version]*ModulePackageVersionDeprecation{
version: versionDeprecation,
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions sourcebundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ func (b *Bundle) RegistryPackageVersions(pkgAddr regaddr.ModulePackage) versions
}

func (b *Bundle) RegistryPackageVersionDeprecation(pkgAddr regaddr.ModulePackage, version versions.Version) *RegistryVersionDeprecation {
deprecation := b.registryPackageVersionDeprecations[pkgAddr][version]
return deprecation
return b.registryPackageVersionDeprecations[pkgAddr][version]
}

// RegistryPackageSourceAddr returns the remote source address corresponding
Expand Down

0 comments on commit ea8f696

Please sign in to comment.