/
utils.go
170 lines (141 loc) · 3.67 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package utils
import (
"bufio"
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"math/rand"
"os"
"runtime"
"time"
)
//GenericJSONParsing Parse into struct
func GenericJSONParsing[T any](filePath string) (T, error) {
var x T
rawContent, err := ioutil.ReadFile(filePath)
if err != nil {
return x, fmt.Errorf("Error when reading the configuration file: %w", err)
}
err = json.Unmarshal(rawContent, &x)
if err != nil {
return x, fmt.Errorf("Error when unmarshalling the JSON file: %w", err)
}
return x, err
}
//GenericYAMLParsing Parse into struct
func GenericYAMLParsing[T any](filePath string) (T, error) {
var x T
rawContent, err := ioutil.ReadFile(filePath)
if err != nil {
return x, fmt.Errorf("Error when reading the configuration file: %w", err)
}
err = yaml.Unmarshal(rawContent, &x)
if err != nil {
return x, fmt.Errorf("Error when unmarshalling the YAML file: %w", err)
}
return x, err
}
//ValueEqual check that 2 pointers to comparable items point to values that are equal
func ValueEqual[T comparable](item1, item2 *T) bool {
if item1 == nil {
if item2 != nil {
return false
}
} else {
if item2 == nil {
return false
} else {
if *item1 != *item2 {
return false
}
}
}
return true
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func CastNumberAsFloat64(val any) (float64, error) {
switch v1 := val.(type) {
case float32:
return float64(v1), nil
case float64:
return float64(v1), nil
case int:
return float64(v1), nil
case int8:
return float64(v1), nil
case int16:
return float64(v1), nil
case int32:
return float64(v1), nil
case int64:
return float64(v1), nil
default:
return 0, fmt.Errorf("item %v cannot be cast as float64", val)
}
}
// IsALetter Check if char is an ASCII letter or not
func IsALetter(element byte) bool {
val := ((element >= 'a') && (element <= 'z')) || ((element >= 'A') && (element <= 'Z'))
return val
}
//AppendIfNew add item to string array if not already in slice
func AppendIfNew(strList []string, newItem string) []string {
_, found := Find(strList, newItem)
if !found {
strList = append(strList, newItem)
}
return strList
}
// Find takes a slice and looks for an element in it. If found it will
// return it's key, otherwise it will return -1 and a bool of false.
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
//GetFileAsLines reads a file and return the content as a string array.
//
//If some processed lines are longer than 65536 characters, maxLineLength needs to be added as an argument.
func GetFileAsLines(filePath string, maxLineLength ...int) ([]string, error) {
readFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer readFile.Close()
fileScanner := bufio.NewScanner(readFile)
var buf []byte
if len(maxLineLength) != 0 {
buf = make([]byte, maxLineLength[0])
fileScanner.Buffer(buf, maxLineLength[0])
}
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
if err := fileScanner.Err(); err != nil {
return nil, err
}
output := make([]string, len(fileLines))
copy(output, fileLines)
return output, nil
}
func GenerateRandomInt(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}