Skip to content

Commit

Permalink
Add type for map[string]*Nic (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
iljarotar committed Sep 17, 2024
1 parent 11c9d46 commit 65f0c68
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions cmd/metal-api/internal/metal/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/metal/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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]
}

tests := []struct {
name string
nics Nics
want map[string]*Nic
want NicMap
}{
{
name: "TestNics_ByIdentifier Test 1",
Expand Down

0 comments on commit 65f0c68

Please sign in to comment.