Skip to content

Commit

Permalink
Merge pull request #77 from flanksource/feat/new-utils
Browse files Browse the repository at this point in the history
feat: new utils from incident-commander
  • Loading branch information
moshloop authored Aug 8, 2023
2 parents 258d8ba + a2bbad7 commit 2869e75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions collections/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@ func IniToMap(path string) map[string]string {
}
return result
}

func MapKeys[K comparable](m map[K]any) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}

return keys
}
16 changes: 16 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ import (
log "github.com/sirupsen/logrus"
)

func Ptr[T any](value T) *T {
return &value
}

// Coalesce returns the first non-zero element
func Coalesce[T comparable](arr ...T) T {
var zeroVal T
for _, item := range arr {
if item != zeroVal {
return item
}
}

return zeroVal
}

// GetEnvOrDefault returns the first non-empty environment variable
func GetEnvOrDefault(names ...string) string {
for _, name := range names {
Expand Down

0 comments on commit 2869e75

Please sign in to comment.