fix(lns): make catalog lookup helpers nil-safe

This commit is contained in:
Virgil 2026-04-02 17:58:59 +00:00
parent 23475a014d
commit a1a19b7e82
2 changed files with 26 additions and 2 deletions

8
lns.go
View file

@ -199,9 +199,14 @@ func LookupCatalogName[T any](
byLabel func(string) (T, bool),
byHash func(primitives.Hash) (T, bool),
) (T, bool) {
var zero T
if byLabel == nil || byHash == nil {
return zero, false
}
label, ok := nameutil.CatalogLabel(name)
if !ok {
var zero T
return zero, false
}
@ -213,7 +218,6 @@ func LookupCatalogName[T any](
normalized, ok := nameutil.Canonicalize(name)
if !ok {
var zero T
return zero, false
}

View file

@ -350,6 +350,26 @@ func TestLookupCatalogNamePreservesDottedLabels(t *testing.T) {
}
}
func TestLookupCatalogNameNilCallbacks(t *testing.T) {
if got, ok := LookupCatalogName[string]("foo", nil, nil); ok || got != "" {
t.Fatalf("LookupCatalogName with nil callbacks = (%q, %v), want (\"\", false)", got, ok)
}
if got, ok := GetLookupCatalogName[string]("foo", nil, nil); ok || got != "" {
t.Fatalf("GetLookupCatalogName with nil callbacks = (%q, %v), want (\"\", false)", got, ok)
}
svc := NewService(nil)
if got, ok := svc.LookupCatalogName("foo", nil, nil); ok || got != nil {
t.Fatalf("svc.LookupCatalogName with nil callbacks = (%v, %v), want (nil, false)", got, ok)
}
if got, ok := svc.GetLookupCatalogName("foo", nil, nil); ok || got != nil {
t.Fatalf("svc.GetLookupCatalogName with nil callbacks = (%v, %v), want (nil, false)", got, ok)
}
}
func TestPackageTypeTables(t *testing.T) {
if len(Types) == 0 || len(TypesByVal) == 0 {
t.Fatal("type lookup tables should not be empty")