diff --git a/lns.go b/lns.go index a44288f..2f24aac 100644 --- a/lns.go +++ b/lns.go @@ -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 } diff --git a/lns_package_test.go b/lns_package_test.go index a46cdfb..8f380eb 100644 --- a/lns_package_test.go +++ b/lns_package_test.go @@ -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")