x/model: NamePart -> PartKind

This commit is contained in:
Blake Mizerany 2024-04-07 18:07:18 -07:00
parent 6a4b3c3823
commit 6c1c0f9f1a
2 changed files with 11 additions and 11 deletions

View File

@ -28,11 +28,11 @@ var (
const MaxNamePartLen = 128 const MaxNamePartLen = 128
type NamePart int type PartKind int
// Levels of concreteness // Levels of concreteness
const ( const (
PartHost NamePart = iota PartHost PartKind = iota
PartNamespace PartNamespace
PartModel PartModel
PartTag PartTag
@ -46,7 +46,7 @@ const (
PartInvalid PartInvalid
) )
var kindNames = map[NamePart]string{ var kindNames = map[PartKind]string{
PartHost: "Host", PartHost: "Host",
PartNamespace: "Namespace", PartNamespace: "Namespace",
PartModel: "Name", PartModel: "Name",
@ -56,7 +56,7 @@ var kindNames = map[NamePart]string{
PartInvalid: "Invalid", PartInvalid: "Invalid",
} }
func (k NamePart) String() string { func (k PartKind) String() string {
return cmp.Or(kindNames[k], "Unknown") return cmp.Or(kindNames[k], "Unknown")
} }
@ -198,7 +198,7 @@ func (r Name) MapHash() uint64 {
return h.Sum64() return h.Sum64()
} }
func (r Name) slice(from, to NamePart) Name { func (r Name) slice(from, to PartKind) Name {
var v Name var v Name
copy(v.parts[from:to+1], r.parts[from:to+1]) copy(v.parts[from:to+1], r.parts[from:to+1])
return v return v
@ -452,8 +452,8 @@ func (r Name) Parts() []string {
// //
// It normalizes the input string by removing "http://" and "https://" only. // It normalizes the input string by removing "http://" and "https://" only.
// No other normalization is done. // No other normalization is done.
func Parts(s string) iter.Seq2[NamePart, string] { func Parts(s string) iter.Seq2[PartKind, string] {
return func(yield func(NamePart, string) bool) { return func(yield func(PartKind, string) bool) {
if strings.HasPrefix(s, "http://") { if strings.HasPrefix(s, "http://") {
s = s[len("http://"):] s = s[len("http://"):]
} }
@ -465,7 +465,7 @@ func Parts(s string) iter.Seq2[NamePart, string] {
return return
} }
yieldValid := func(kind NamePart, part string) bool { yieldValid := func(kind PartKind, part string) bool {
if !isValidPart(kind, part) { if !isValidPart(kind, part) {
yield(PartInvalid, "") yield(PartInvalid, "")
return false return false
@ -567,7 +567,7 @@ func (r Name) Valid() bool {
} }
// isValidPart returns Parttrue if given part is valid ascii [a-zA-Z0-9_\.-] // isValidPart returns Parttrue if given part is valid ascii [a-zA-Z0-9_\.-]
func isValidPart(kind NamePart, s string) bool { func isValidPart(kind PartKind, s string) bool {
if s == "" { if s == "" {
return false return false
} }
@ -579,7 +579,7 @@ func isValidPart(kind NamePart, s string) bool {
return true return true
} }
func isValidByte(kind NamePart, c byte) bool { func isValidByte(kind PartKind, c byte) bool {
if kind == PartNamespace && c == '.' { if kind == PartNamespace && c == '.' {
return false return false
} }

View File

@ -109,7 +109,7 @@ func TestNameParts(t *testing.T) {
} }
func TestNamePartString(t *testing.T) { func TestNamePartString(t *testing.T) {
if g := NamePart(-2).String(); g != "Unknown" { if g := PartKind(-2).String(); g != "Unknown" {
t.Errorf("Unknown part = %q; want %q", g, "Unknown") t.Errorf("Unknown part = %q; want %q", g, "Unknown")
} }
for kind, name := range kindNames { for kind, name := range kindNames {