Skip to content

Commit b624851

Browse files
committed
Splitted goext into multiple files
1 parent 4144b28 commit b624851

6 files changed

+166
-124
lines changed

goext/env_extensions.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package goext
2+
3+
import "os"
4+
5+
// EnvExists checks if the given environment variable exists or not.
6+
func EnvExists(key string) bool {
7+
if _, ok := os.LookupEnv(key); ok {
8+
return true
9+
}
10+
return false
11+
}
12+
13+
// GetEnvOrDefault returns the value if the environment variable exists or the default otherwise.
14+
func GetEnvOrDefault(key string, defaultValue string) (string, bool) {
15+
if _, ok := os.LookupEnv(key); ok {
16+
return key, true
17+
}
18+
return defaultValue, false
19+
}

goext/file_extensions.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package goext
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"os"
7+
)
8+
9+
// WriteJsonToFile writes the given object into a file.
10+
func WriteJsonToFile(object any, outputFilePath string, indented bool) error {
11+
var data []byte
12+
var err error
13+
if indented {
14+
data, err = json.MarshalIndent(object, "", " ")
15+
} else {
16+
data, err = json.Marshal(object)
17+
}
18+
if err != nil {
19+
return err
20+
}
21+
if err := os.WriteFile(outputFilePath, data, os.ModePerm); err != nil {
22+
return err
23+
}
24+
return nil
25+
}
26+
27+
// FileExists checks if a file exists (and it is not a directory).
28+
func FileExists(filePath string) (bool, error) {
29+
info, err := os.Stat(filePath)
30+
if err == nil {
31+
return !info.IsDir(), nil
32+
}
33+
if errors.Is(err, os.ErrNotExist) {
34+
return false, nil
35+
}
36+
return false, err
37+
}

goext/goext.go

-124
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,11 @@
22
package goext
33

44
import (
5-
"encoding/json"
6-
"errors"
75
"fmt"
86
"os"
9-
"sort"
107
"strconv"
11-
"strings"
128
)
139

14-
// AddIf adds the given values to a slice if the condition is fulfilled.
15-
func AddIf[T any](slice []T, cond bool, values ...T) []T {
16-
if cond {
17-
return append(slice, values...)
18-
}
19-
return slice
20-
}
21-
22-
// AppendIfMissing appends the given element if it is missing in the slice.
23-
func AppendIfMissing[T comparable](slice []T, value T) []T {
24-
for _, ele := range slice {
25-
if ele == value {
26-
return slice
27-
}
28-
}
29-
return append(slice, value)
30-
}
31-
3210
// Ternary adds the missing ternary operator.
3311
func Ternary[T any](cond bool, vtrue, vfalse T) T {
3412
if cond {
@@ -43,17 +21,6 @@ func Printfln(format string, a ...any) (n int, err error) {
4321
return fmt.Println(text)
4422
}
4523

46-
// RemoveEmpty removes all empty strings from an array.
47-
func RemoveEmpty(s []string) []string {
48-
var r []string
49-
for _, str := range s {
50-
if str != "" {
51-
r = append(r, str)
52-
}
53-
}
54-
return r
55-
}
56-
5724
// RunInDirectory runs a given function inside the passed directory as working directory.
5825
// It resets to the previous directory when finished (or an error occured).
5926
func RunInDirectory(path string, f func() error) (err error) {
@@ -95,99 +62,8 @@ func Noop() error {
9562
return nil
9663
}
9764

98-
// WriteJsonToFile writes the given object into a file.
99-
func WriteJsonToFile(object any, outputFilePath string, indented bool) error {
100-
var data []byte
101-
var err error
102-
if indented {
103-
data, err = json.MarshalIndent(object, "", " ")
104-
} else {
105-
data, err = json.Marshal(object)
106-
}
107-
if err != nil {
108-
return err
109-
}
110-
if err := os.WriteFile(outputFilePath, data, os.ModePerm); err != nil {
111-
return err
112-
}
113-
return nil
114-
}
115-
116-
// EnvExists checks if the given environment variable exists or not.
117-
func EnvExists(key string) bool {
118-
if _, ok := os.LookupEnv(key); ok {
119-
return true
120-
}
121-
return false
122-
}
123-
124-
// GetEnvOrDefault returns the value if the environment variable exists or the default otherwise.
125-
func GetEnvOrDefault(key string, defaultValue string) (string, bool) {
126-
if _, ok := os.LookupEnv(key); ok {
127-
return key, true
128-
}
129-
return defaultValue, false
130-
}
131-
132-
// FileExists checks if a file exists (and it is not a directory).
133-
func FileExists(filePath string) (bool, error) {
134-
info, err := os.Stat(filePath)
135-
if err == nil {
136-
return !info.IsDir(), nil
137-
}
138-
if errors.Is(err, os.ErrNotExist) {
139-
return false, nil
140-
}
141-
return false, err
142-
}
143-
14465
// Pass is a no-op function that can be used to set variables to used.
14566
// Usefull during development but must be removed afterwards!
14667
func Pass(i ...interface{}) {
14768
// No-Op
14869
}
149-
150-
func TrimAllPrefix(s, prefix string) string {
151-
for strings.HasPrefix(s, prefix) {
152-
s = s[len(prefix):]
153-
}
154-
return s
155-
}
156-
157-
func TrimAllSuffix(s, suffix string) string {
158-
for strings.HasSuffix(s, suffix) {
159-
s = s[:len(s)-len(suffix)]
160-
}
161-
return s
162-
}
163-
164-
func TrimNewlineSuffix(v string) string {
165-
return TrimAllSuffix(TrimAllSuffix(v, "\r\n"), "\n")
166-
}
167-
168-
// SplitByNewLine splits the given value by newlines.
169-
func SplitByNewLine(value string) []string {
170-
return strings.Split(strings.ReplaceAll(value, "\r\n", "\n"), "\n")
171-
}
172-
173-
// ProcessMapSorted calls the given function on all entries of the map, sorted by their key value (by string).
174-
func ProcessMapSorted[TK comparable, TV any](m map[TK]TV, pf func(TK, TV) error) error {
175-
// Get all keys
176-
keys := make([]TK, 0, len(m))
177-
for k := range m {
178-
keys = append(keys, k)
179-
}
180-
181-
// Sort the keys alphanumeric
182-
sort.Slice(keys, func(i, j int) bool {
183-
return fmt.Sprint(keys[i]) < fmt.Sprint(keys[j])
184-
})
185-
186-
// Call the method for each function
187-
for _, k := range keys {
188-
if err := pf(k, m[k]); err != nil {
189-
return err
190-
}
191-
}
192-
return nil
193-
}

goext/map_extensions.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package goext
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
)
8+
9+
// GetMapKeys gets all the keys of the given map in alpahnummeric sorted order.
10+
func GetMapKeys[TK comparable, TV any](m map[TK]TV) []TK {
11+
// Get all keys
12+
keys := make([]TK, 0, len(m))
13+
for k := range m {
14+
keys = append(keys, k)
15+
}
16+
17+
// Sort the keys alphanumeric
18+
sort.Slice(keys, func(i, j int) bool {
19+
return fmt.Sprint(keys[i]) < fmt.Sprint(keys[j])
20+
})
21+
22+
return keys
23+
}
24+
25+
// ProcessMapSorted calls the given function on all entries of the map, sorted by their key value (by string).
26+
func ProcessMapSorted[TK comparable, TV any](m map[TK]TV, pf func(TK, TV) error) error {
27+
// Get all keys
28+
keys := GetMapKeys(m)
29+
30+
// Call the method for each function
31+
for _, k := range keys {
32+
if err := pf(k, m[k]); err != nil {
33+
return err
34+
}
35+
}
36+
return nil
37+
}
38+
39+
// ConvertMapToSingleString converts the given map into a joined string with the given separators
40+
func ConvertMapToSingleString(m map[string]string, kvpSeparator string, entrySeparator string) string {
41+
var sb strings.Builder
42+
mapKeys := GetMapKeys(m)
43+
isFirst := true
44+
for _, key := range mapKeys {
45+
if !isFirst {
46+
sb.WriteString(entrySeparator)
47+
}
48+
sb.WriteString(key)
49+
sb.WriteString(kvpSeparator)
50+
sb.WriteString(m[key])
51+
isFirst = false
52+
}
53+
return sb.String()
54+
}

goext/slice_extensions.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package goext
2+
3+
// AddIf adds the given values to a slice if the condition is fulfilled.
4+
func AddIf[T any](slice []T, cond bool, values ...T) []T {
5+
if cond {
6+
return append(slice, values...)
7+
}
8+
return slice
9+
}
10+
11+
// AppendIfMissing appends the given element if it is missing in the slice.
12+
func AppendIfMissing[T comparable](slice []T, value T) []T {
13+
for _, ele := range slice {
14+
if ele == value {
15+
return slice
16+
}
17+
}
18+
return append(slice, value)
19+
}
20+
21+
// RemoveEmpty removes all empty strings from a slice.
22+
func RemoveEmpty(slice []string) []string {
23+
var r []string
24+
for _, str := range slice {
25+
if str != "" {
26+
r = append(r, str)
27+
}
28+
}
29+
return r
30+
}

goext/string_extensions.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package goext
2+
3+
import "strings"
4+
5+
func TrimAllPrefix(s, prefix string) string {
6+
for strings.HasPrefix(s, prefix) {
7+
s = s[len(prefix):]
8+
}
9+
return s
10+
}
11+
12+
func TrimAllSuffix(s, suffix string) string {
13+
for strings.HasSuffix(s, suffix) {
14+
s = s[:len(s)-len(suffix)]
15+
}
16+
return s
17+
}
18+
19+
func TrimNewlineSuffix(v string) string {
20+
return TrimAllSuffix(TrimAllSuffix(v, "\r\n"), "\n")
21+
}
22+
23+
// SplitByNewLine splits the given value by newlines and returns a slice.
24+
func SplitByNewLine(value string) []string {
25+
return strings.Split(strings.ReplaceAll(value, "\r\n", "\n"), "\n")
26+
}

0 commit comments

Comments
 (0)