Skip to content

Commit

Permalink
refact: use generics for contains functions
Browse files Browse the repository at this point in the history
  • Loading branch information
restanrm committed Apr 25, 2022
1 parent ea9aaa6 commit 8061abe
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func excludeCorrectlyTaggedNodes(
out := []Machine{}
tags := []string{}
for tag, ns := range aclPolicy.TagOwners {
if containsString(ns, namespace) {
if contains(ns, namespace) {
tags = append(tags, tag)
}
}
Expand All @@ -342,7 +342,7 @@ func excludeCorrectlyTaggedNodes(

found := false
for _, t := range hi.RequestTags {
if containsString(tags, t) {
if contains(tags, t) {
found = true

break
Expand Down
4 changes: 2 additions & 2 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ func nodesToPtables(
tags += "," + tag
}
for _, tag := range machine.InvalidTags {
if !containsString(machine.ForcedTags, tag) {
if !contains(machine.ForcedTags, tag) {
tags += "," + pterm.LightRed(tag)
}
}
for _, tag := range machine.ValidTags {
if !containsString(machine.ForcedTags, tag) {
if !contains(machine.ForcedTags, tag) {
tags += "," + pterm.LightGreen(tag)
}
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -565,9 +566,9 @@ func GetFileMode(key string) fs.FileMode {
return fs.FileMode(mode)
}

func containsString(ss []string, s string) bool {
for _, v := range ss {
if v == s {
func contains[T string](ts []T, t T) bool {
for _, v := range ts {
if reflect.DeepEqual(v,t) {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (machine Machine) isExpired() bool {

func containsAddresses(inputs []string, addrs []string) bool {
for _, addr := range addrs {
if containsString(inputs, addr) {
if contains(inputs, addr) {
return true
}
}
Expand Down Expand Up @@ -803,7 +803,7 @@ func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error {
}

for _, newRoute := range newRoutes {
if !containsIPPrefix(machine.GetAdvertisedRoutes(), newRoute) {
if !contains(machine.GetAdvertisedRoutes(), newRoute) {
return fmt.Errorf(
"route (%s) is not available on node %s: %w",
machine.Name,
Expand Down
17 changes: 4 additions & 13 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/json"
"fmt"
"net"
"reflect"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -223,16 +224,6 @@ func (h *Headscale) getUsedIPs() (*netaddr.IPSet, error) {
return ipSet, nil
}

func containsString(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}

return false
}

func tailNodesToString(nodes []*tailcfg.Node) string {
temp := make([]string, len(nodes))

Expand Down Expand Up @@ -282,9 +273,9 @@ func stringToIPPrefix(prefixes []string) ([]netaddr.IPPrefix, error) {
return result, nil
}

func containsIPPrefix(prefixes []netaddr.IPPrefix, prefix netaddr.IPPrefix) bool {
for _, p := range prefixes {
if prefix == p {
func contains[T string | netaddr.IPPrefix](ts []T, t T) bool {
for _, v := range ts {
if reflect.DeepEqual(v, t) {
return true
}
}
Expand Down

0 comments on commit 8061abe

Please sign in to comment.