Skip to content

Commit

Permalink
Use the new map/slice features in Go 1.23 (#883)
Browse files Browse the repository at this point in the history
Signed-off-by: Ziv Nevo <[email protected]>
  • Loading branch information
zivnevo authored Sep 23, 2024
1 parent d84416f commit 58498fc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/np-guard/vpc-network-config-analyzer

go 1.22.4
go 1.23.1

require (
github.com/IBM/networking-go-sdk v0.49.0
Expand Down
24 changes: 3 additions & 21 deletions pkg/common/genericSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package common

import (
"fmt"
"maps"
"reflect"
"slices"
"sort"
"strings"
)
Expand Down Expand Up @@ -40,7 +42,7 @@ func (s GenericSet[T]) AsKey() SetAsKey {
}

func (s GenericSet[T]) AsList() []T {
return MapKeys(s)
return slices.Collect(maps.Keys(s))
}

func FromList[T comparable](l []T) GenericSet[T] {
Expand Down Expand Up @@ -74,23 +76,3 @@ func AnyMapEntry[K comparable, V any](m map[K]V) (k K, v V) {
}
return k, v
}

// todo - these two methods will be available on go 1.23:
func MapValues[K comparable, V any](m map[K]V) []V {
vals := make([]V, len(m))
i := 0
for _, v := range m {
vals[i] = v
i++
}
return vals
}
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}
3 changes: 2 additions & 1 deletion pkg/drawio/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package drawio

import (
"maps"
"slices"
"sort"

Expand Down Expand Up @@ -466,7 +467,7 @@ func sortIconsBySGs(sgs []SquareTreeNodeInterface) [][]TreeNodeInterface {
sgsToIcons[sgsAsKey] = append(sgsToIcons[sgsAsKey], icon)
}
// covert to list:
return common.MapValues(sgsToIcons)
return slices.Collect(maps.Values(sgsToIcons))
}

// ///////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions pkg/linter/linterExecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package linter

import (
"fmt"
"maps"
"slices"
"sort"
"strings"

"github.com/np-guard/vpc-network-config-analyzer/pkg/common"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
)

Expand All @@ -37,7 +37,7 @@ var linterGenerators = map[string]linterGenerator{
}

func ValidLintersNames() string {
return strings.Join(common.MapKeys(linterGenerators), ",")
return strings.Join(slices.Collect(maps.Keys(linterGenerators)), ",")
}
func IsValidLintersNames(name string) bool {
_, ok := linterGenerators[name]
Expand Down

0 comments on commit 58498fc

Please sign in to comment.