-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosl.go
145 lines (125 loc) · 5.16 KB
/
gosl.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
// Package gosl provides a snippet collection for working with routine operations
// in your Go programs with a super user-friendly API and the most efficient
// performance.
package gosl
import "github.com/charmbracelet/lipgloss"
// Utility represents struct for a regular function.
type Utility struct{}
// GenericUtility represents struct with T any and K comparable types for a
// generic function.
type GenericUtility[T any, K comparable] struct{}
// Concat concatenate strings using the built-in copy and "unsafe" package with
// unsafe.String function.
//
// If s has no elements returns zero-value for a string.
func (u *Utility) Concat(s ...string) string {
return Concat(s...)
}
// ContainsCaseInsensitive reports if substr is within s string using built-in
// "strings" package with strings.Contains. Case-insensitive for input values by
// default.
//
// If s and/or substr have an "" (empty) value returns false for a bool.
func (u *Utility) ContainsCaseInsensitive(s, substr string) bool {
return ContainsCaseInsensitive(s, substr)
}
// IsFileExist reports whether a file exists on the specified path.
func (u *Utility) IsFileExist(path string) bool {
return IsFileExist(path)
}
// IsDirExist reports whether a dir exists on the specified path.
func (u *Utility) IsDirExist(path string) bool {
return IsDirExist(path)
}
// RandomString generates a random string with a given size using built-in
// "crypto/rand" and "encoding/hex" packages.
//
// If err != nil returns zero-value for a string and error.
func (u *Utility) RandomString(size int) (string, error) {
return RandomString(size)
}
// RenderStyled render a styled string with a given lipgloss.Style template
// using "charmbracelet/lipgloss" package.
//
// If s have an "" (empty) value returns zero-value for a string.
func (u *Utility) RenderStyled(s string, template lipgloss.Style) string {
return RenderStyled(s, template)
}
// ToBytes converts string to byte slice using the built-in "unsafe" package
// with unsafe.Slice function.
//
// If err != nil returns zero-value for a byte slice and error.
func (u *Utility) ToBytes(s string) ([]byte, error) {
return ToBytes(s)
}
// ToString converts byte slice to string using the built-in "unsafe" package
// with unsafe.String function.
//
// If err != nil returns zero-value for a string and error.
func (u *Utility) ToString(b []byte) (string, error) {
return ToString(b)
}
// ModifyByValue modify an unknown key in the given map[string]any by it value.
// Supports nested maps, but only if their type is map[string]any.
func (u *Utility) ModifyByValue(m map[string]any, foundValue, newValue any) (foundKey bool, results map[string]any) {
return ModifyByValue(m, foundValue, newValue)
}
// ContainsInSlice reports if value T is within slice []T.
//
// If s have a zero-value returns false for a bool.
func (g *GenericUtility[T, K]) ContainsInSlice(s []K, value K) bool {
return ContainsInSlice(s, value)
}
// ContainsInMap reports if key T is within map[T]K.
//
// If m have a zero-value returns false for a bool.
func (g *GenericUtility[T, K]) ContainsInMap(m map[K]T, key K) bool {
return ContainsInMap(m, key)
}
// Equals compares two values of type K, returns true if they are equal.
func (g *GenericUtility[T, K]) Equals(value1, value2 K) bool {
return Equals(value1, value2)
}
// NotEquals compares two values of type K, returns true if they are not equal.
func (g *GenericUtility[T, K]) NotEquals(value1, value2 K) bool {
return NotEquals(value1, value2)
}
// ParseFileToStruct parses the given file from path to struct *T using
// "knadh/koanf" package.
//
// You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The
// structured file can be placed both locally (by system path) and accessible
// via HTTP (by URL).
//
// If err != nil, returns zero-value for a struct and error.
func (g *GenericUtility[T, K]) ParseFileToStruct(path string, model *T) (*T, error) {
return ParseFileToStruct(path, model)
}
// ParseFileWithEnvToStruct parses the given file from path to struct *T using
// "knadh/koanf" package with an (optional) environment variables for a secret
// data.
//
// You can use any of the supported file formats (JSON, YAML, TOML, or HCL). The
// structured file can be placed both locally (by system path) and accessible
// via HTTP (by URL).
//
// If err != nil, returns zero-value for a struct and error.
func (g *GenericUtility[T, K]) ParseFileWithEnvToStruct(path, envPrefix string, model *T) (*T, error) {
return ParseFileWithEnvToStruct(path, envPrefix, model)
}
// Marshal converts struct *T to JSON data (byte slice) using jsoniter.Marshal
// with a default configuration. A 100% compatible drop-in replacement of
// "encoding/json" standard lib.
//
// If err != nil returns zero-value for a byte slice and error.
func (g *GenericUtility[T, K]) Marshal(model *T) ([]byte, error) {
return Marshal(model)
}
// Unmarshal converts JSON data (byte slice) to struct *T using
// jsoniter.Unmarshal with a default configuration. A 100% compatible drop-in
// replacement of "encoding/json" standard lib.
//
// If err != nil returns zero-value for a struct and error.
func (g *GenericUtility[T, K]) Unmarshal(data []byte, model *T) (*T, error) {
return Unmarshal(data, model)
}