-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
103 lines (88 loc) · 1.53 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package utils
import (
"log"
"os"
"golang.org/x/exp/constraints"
)
func GetKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func SetDiff[T comparable](orig, changed map[T]struct{}) map[T]struct{} {
result := make(map[T]struct{})
for k, _ := range changed {
if _, exists := orig[k]; !exists {
result[k] = struct{}{}
}
}
return result
}
func Contains[T comparable](elems []T, v T) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}
func AreElementsUnique[T comparable](elems []T) bool {
if len(elems) == len(GetUnique(elems)) {
return true
}
return false
}
func GetUnique[T comparable](arr []T) []T {
occurred := map[T]bool{}
result := []T{}
for e := range arr {
if occurred[arr[e]] != true {
occurred[arr[e]] = true
result = append(result, arr[e])
}
}
return result
}
func GetEnvWithDefault(key, def string) string {
result := os.Getenv(key)
if result == "" {
return def
}
return result
}
func GetEnv(key string) string {
result := os.Getenv(key)
if result == "" {
log.Fatalf("Environment variable %s not found", key)
return ""
}
return result
}
func Max[T constraints.Ordered](s ...T) T {
if len(s) == 0 {
var zero T
return zero
}
m := s[0]
for _, v := range s {
if m < v {
m = v
}
}
return m
}
func Min[T constraints.Ordered](s ...T) T {
if len(s) == 0 {
var zero T
return zero
}
m := s[0]
for _, v := range s {
if m > v {
m = v
}
}
return m
}