Skip to content

Commit 76bcd82

Browse files
committed
added general utils pckg
1 parent 5a99b60 commit 76bcd82

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

utils/utils.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package utils
2+
3+
/*
4+
* General Functions (utils)
5+
* Might move Functions into seperate files
6+
*/
7+
8+
import (
9+
"encoding/json"
10+
"errors"
11+
"regexp"
12+
"strings"
13+
)
14+
15+
func StringToArray(sliceStr string) ([]string, error) {
16+
if sliceStr == "" {
17+
return []string{}, errors.New("sliceStr is empty")
18+
}
19+
20+
re, err := regexp.Compile(`\s+`)
21+
22+
if err != nil {
23+
return []string{}, err
24+
}
25+
26+
normalized := re.ReplaceAllString(sliceStr, "")
27+
28+
tokens := strings.Split(normalized, ",")
29+
30+
return tokens, nil
31+
}
32+
33+
func Contains[T comparable](list []T, item T) (bool){
34+
for _, match := range list {
35+
if match == item {
36+
return true
37+
}
38+
}
39+
40+
return false
41+
}
42+
43+
func GetJsonSafe[T any](jsonStr string) (T, error) {
44+
var result T
45+
46+
err := json.Unmarshal([]byte(jsonStr), &result)
47+
48+
return result, err
49+
}
50+
51+
func GetJson[T any](jsonStr string) (T) {
52+
var result T
53+
54+
err := json.Unmarshal([]byte(jsonStr), &result)
55+
56+
if err != nil {
57+
// JSON is empty
58+
}
59+
60+
return result
61+
}

0 commit comments

Comments
 (0)