73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
package versions
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
)
|
|
|
|
// PackagesState is the indexer's packages state as the builder reads it (its manifest's reads grant): its keys, and one
|
|
// key's value (found false when it holds none).
|
|
type PackagesState interface {
|
|
Keys(ctx context.Context) ([]string, error)
|
|
Get(ctx context.Context, key string) (value []byte, found bool, err error)
|
|
}
|
|
|
|
// NamesIn are a package's names in its ecosystem as the indexer's packages state holds them (design 32): the name of the
|
|
// series key that names module and artifact as what it is built from. A series key is named `<name>.<series>`; a key
|
|
// with no dot is a mesh Go module's published packages, not a series. A value that does not decode is passed over.
|
|
func NamesIn(s PackagesState) PackageNames {
|
|
return func(ctx context.Context, module, artifact string) (string, bool, error) {
|
|
keys, err := s.Keys(ctx)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
sort.Strings(keys)
|
|
built := NameOf(module, artifact)
|
|
for _, key := range keys {
|
|
if !isSeriesKey(key) {
|
|
continue
|
|
}
|
|
value, found, err := s.Get(ctx, key)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if !found {
|
|
continue
|
|
}
|
|
v, err := meshcatalog.DecodePackagesByPackageSeriesValue(value) // the indexer's value of a series key (mesh-sdk's contract, J4)
|
|
if err != nil || v.Package != built {
|
|
continue
|
|
}
|
|
return v.Name, true, nil
|
|
}
|
|
return "", false, nil
|
|
}
|
|
}
|
|
|
|
// NamedModule is the module a value of the indexer's packages state names a package's name for (design 32), and that
|
|
// name: a series key whose value names a name and the <module>/<artifact> it is built from. The builder looks at that module again when
|
|
// the value is heard, so a done build that left the package unnumbered (its name not known at its claim) is numbered then.
|
|
func NamedModule(key string, value []byte) (module, name string, ok bool) {
|
|
if !isSeriesKey(key) {
|
|
return "", "", false
|
|
}
|
|
v, err := meshcatalog.DecodePackagesByPackageSeriesValue(value)
|
|
if err != nil || v.Name == "" {
|
|
return "", "", false
|
|
}
|
|
module, artifact, cut := strings.Cut(v.Package, "/")
|
|
if !cut || module == "" || artifact == "" {
|
|
return "", "", false
|
|
}
|
|
return module, v.Name, true
|
|
}
|
|
|
|
// isSeriesKey says whether a key of the indexer's packages state is a series key (`<package>.<series>`), not a per-module one.
|
|
func isSeriesKey(key string) bool {
|
|
_, _, ok := meshcatalog.ParsePackagesByPackageSeriesKey(key)
|
|
return ok
|
|
}
|