package resolution import ( "fmt" "sort" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) // sortedShares is a requirement's shares as they are handed to the provider: sorted by `to` (a file whose lines move // looks changed), each entry's methods sorted, and a copy so the manifest is not reordered. func sortedShares(shares []mcat.BucketShare) []mcat.BucketShare { out := make([]mcat.BucketShare, 0, len(shares)) for _, s := range shares { methods := append([]string(nil), s.Methods...) sort.Strings(methods) out = append(out, mcat.BucketShare{To: s.To, Methods: methods}) } sort.Slice(out, func(i, j int) bool { return out[i].To < out[j].To }) return out } // withShares adds this module's shares of the requirement to the values it gives the provider. A module whose own // `contributes` already names `shares` is refused: the field is the only way to say it (ADR 0468 §8). func withShares(m mcat.Manifest, requirement string, values map[string]any) (map[string]any, error) { shares := m.RequireShares[requirement] if _, named := values[mcat.SharesValue]; named && requirement == mcat.BucketRequirement { return nil, fmt.Errorf("%s contributing to %s names `shares`; write it on its requirement instead (novox/hq ADR 0468 §8)", m.Module, requirement) } if len(shares) == 0 { return values, nil } out := make(map[string]any, len(values)+1) for k, v := range values { out[k] = v } listed := []any{} for _, s := range sortedShares(shares) { methods := []any{} for _, method := range s.Methods { methods = append(methods, method) } listed = append(listed, map[string]any{"to": s.To, "methods": methods}) } out[mcat.SharesValue] = listed return out, nil }