From 65f0c688164523ac0c7426ddf2641780d7639270 Mon Sep 17 00:00:00 2001 From: Ilja Rotar <77339620+iljarotar@users.noreply.github.com> Date: Tue, 17 Sep 2024 13:55:50 +0200 Subject: [PATCH] Add type for `map[string]*Nic` (#574) --- cmd/metal-api/internal/metal/network.go | 11 +++++++---- cmd/metal-api/internal/metal/network_test.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/metal-api/internal/metal/network.go b/cmd/metal-api/internal/metal/network.go index 6068311b..117e0772 100644 --- a/cmd/metal-api/internal/metal/network.go +++ b/cmd/metal-api/internal/metal/network.go @@ -303,9 +303,12 @@ func (nics Nics) FilterByHostname(hostname string) (res Nics) { return res } +// NicMap maps nic names to the corresponding nics +type NicMap map[string]*Nic + // ByName creates a map (nic names --> nic) from a nic list. -func (nics Nics) ByName() map[string]*Nic { - res := make(map[string]*Nic) +func (nics Nics) ByName() NicMap { + res := make(NicMap) for i, n := range nics { res[n.Name] = &nics[i] @@ -315,8 +318,8 @@ func (nics Nics) ByName() map[string]*Nic { } // ByIdentifier creates a map (nic identifier --> nic) from a nic list. -func (nics Nics) ByIdentifier() map[string]*Nic { - res := make(map[string]*Nic) +func (nics Nics) ByIdentifier() NicMap { + res := make(NicMap) for i, n := range nics { res[n.GetIdentifier()] = &nics[i] diff --git a/cmd/metal-api/internal/metal/network_test.go b/cmd/metal-api/internal/metal/network_test.go index a7dcfddf..e935bd74 100644 --- a/cmd/metal-api/internal/metal/network_test.go +++ b/cmd/metal-api/internal/metal/network_test.go @@ -23,7 +23,7 @@ func TestNics_ByIdentifier(t *testing.T) { nicArray[i].Neighbors = append(nicArray[0:i], nicArray[i+1:countOfNics]...) } - map1 := map[string]*Nic{} + map1 := NicMap{} for i, n := range nicArray { map1[string(n.MacAddress)] = &nicArray[i] } @@ -31,7 +31,7 @@ func TestNics_ByIdentifier(t *testing.T) { tests := []struct { name string nics Nics - want map[string]*Nic + want NicMap }{ { name: "TestNics_ByIdentifier Test 1",