The builder reads package names from the catalog half's entries alone and never the indexer's state (design 47 G §348): PackagesState, NamesIn, NamedModule, Carriers and ModuleOf, which read that state and are wired nowhere, go; the reservation's name test reads its carriers from the entries. Builds: design 47 G §348, spec 25
39 lines
1.3 KiB
Go
39 lines
1.3 KiB
Go
package versions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Built is a module's artifact, which a package's name is the name of.
|
|
type Built struct{ Module, Artifact string }
|
|
|
|
// moduleOfCarriers is the module and artifact a package's name is built from, found false for a name no entry carries; more
|
|
// than one entry carrying it is an error naming them. The carriers are read from the catalog half's entries (CarriersFrom,
|
|
// design 47 G §348), never from the indexer's state.
|
|
func moduleOfCarriers(carriers func(ctx context.Context, name string) ([]Built, error)) func(ctx context.Context, name string) (module, artifact string, found bool, err error) {
|
|
return func(ctx context.Context, name string) (string, string, bool, error) {
|
|
got, err := carriers(ctx, name)
|
|
switch {
|
|
case err != nil:
|
|
return "", "", false, err
|
|
case len(got) == 0:
|
|
return "", "", false, nil
|
|
case len(got) > 1:
|
|
return "", "", false, errors.New(MoreThanOne(name, got))
|
|
}
|
|
return got[0].Module, got[0].Artifact, true, nil
|
|
}
|
|
}
|
|
|
|
// MoreThanOne is the words of a name that more than one entry carries.
|
|
func MoreThanOne(name string, got []Built) string {
|
|
var refs []string
|
|
for _, b := range got {
|
|
refs = append(refs, b.Module+"/"+b.Artifact)
|
|
}
|
|
return fmt.Sprintf("the name %s is carried by more than one entry: %s", name, strings.Join(refs, ", "))
|
|
}
|