346 lines
6.6 KiB
Go
346 lines
6.6 KiB
Go
package Utils
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"iter"
|
|
"maps"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func Get[T any](inputs any, _default *T) T {
|
|
|
|
var value T
|
|
|
|
if _default != nil {
|
|
value = *_default
|
|
}
|
|
|
|
switch input_value := inputs.(type) {
|
|
case T:
|
|
value = input_value
|
|
case *T:
|
|
value = *input_value
|
|
case []byte:
|
|
value = any(input_value).(T)
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
func GetPointer[T any](value T) *T {
|
|
return &value
|
|
}
|
|
|
|
func Trim(_string string) string {
|
|
return strings.Trim(_string, " \r\t\n")
|
|
}
|
|
|
|
func GetStrings(inputs any) []string {
|
|
|
|
var _strings []string = []string{}
|
|
|
|
switch value := inputs.(type) {
|
|
case string:
|
|
_strings = append(_strings, value)
|
|
case []string:
|
|
_strings = append(_strings, value...)
|
|
case []any:
|
|
for _, subvalue := range value {
|
|
_strings = append(_strings, GetStrings(subvalue)...)
|
|
}
|
|
}
|
|
|
|
return _strings
|
|
}
|
|
|
|
func GetStringsFilled(inputs any) []string {
|
|
|
|
var _strings []string = []string{}
|
|
|
|
switch value := inputs.(type) {
|
|
case string:
|
|
value = Trim(value)
|
|
if value != "" {
|
|
_strings = append(_strings, value)
|
|
}
|
|
case []string:
|
|
for _, subvalue := range value {
|
|
_strings = append(_strings, GetStringsFilled(subvalue)...)
|
|
}
|
|
case []any:
|
|
for _, subvalue := range value {
|
|
_strings = append(_strings, GetStringsFilled(subvalue)...)
|
|
}
|
|
}
|
|
|
|
return _strings
|
|
}
|
|
|
|
func GetKeys(inputs any) []string {
|
|
|
|
var keys []string = []string{}
|
|
|
|
switch value := inputs.(type) {
|
|
case string:
|
|
if IsKey(value) {
|
|
keys = append(keys, value)
|
|
}
|
|
case []string:
|
|
for _, subvalue := range value {
|
|
if IsKey(subvalue) && !slices.Contains(keys, subvalue) {
|
|
keys = append(keys, subvalue)
|
|
}
|
|
}
|
|
case []any:
|
|
for _, value := range value {
|
|
for _, subvalue := range GetKeys(value) {
|
|
if !slices.Contains(keys, subvalue) {
|
|
keys = append(keys, GetKeys(subvalue)...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return keys
|
|
}
|
|
|
|
func GetPascalKeys(inputs any) []string {
|
|
|
|
var keys []string
|
|
|
|
switch value := inputs.(type) {
|
|
case string:
|
|
if IsPascalKey(value) {
|
|
keys = append(keys, value)
|
|
}
|
|
case []string:
|
|
for _, subvalue := range value {
|
|
if IsPascalKey(subvalue) && !slices.Contains(keys, subvalue) {
|
|
keys = append(keys, subvalue)
|
|
}
|
|
}
|
|
case []any:
|
|
for _, value := range value {
|
|
for _, subvalue := range GetPascalKeys(value) {
|
|
if !slices.Contains(keys, subvalue) {
|
|
keys = append(keys, GetPascalKeys(subvalue)...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return keys
|
|
}
|
|
|
|
func get_value[T any](keys []string, inputs any, _default *T) (T, bool) {
|
|
|
|
var response T
|
|
var ok bool = false
|
|
|
|
if _default != nil {
|
|
response = *_default
|
|
}
|
|
|
|
switch block := inputs.(type) {
|
|
case map[string]any:
|
|
for _, key := range keys {
|
|
value, subok := block[key]
|
|
if subok {
|
|
response = Get[T](value, nil)
|
|
ok = true
|
|
break
|
|
}
|
|
}
|
|
case []any:
|
|
for _, subinputs := range block {
|
|
value, subok := get_value[T](keys, subinputs, nil)
|
|
if subok {
|
|
response = value
|
|
ok = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return response, ok
|
|
}
|
|
|
|
func GetValue[T any](keys any, inputs any, _default *T) T {
|
|
|
|
response, _ := get_value(GetKeys(keys), inputs, _default)
|
|
|
|
return response
|
|
}
|
|
|
|
func AppendInDictionary(dictionary *map[string]any, new_dictionary map[string]any, overwrite bool) {
|
|
for key, value := range new_dictionary {
|
|
_, exists := (*dictionary)[key]
|
|
if overwrite || !exists {
|
|
(*dictionary)[key] = value
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetDictionary(inputs any, overwrite bool) map[string]any {
|
|
|
|
var dictionary map[string]any = map[string]any{}
|
|
|
|
switch block := inputs.(type) {
|
|
case map[string]any:
|
|
AppendInDictionary(&dictionary, block, overwrite)
|
|
case []any:
|
|
for _, subinputs := range block {
|
|
AppendInDictionary(&dictionary, GetDictionary(subinputs, overwrite), overwrite)
|
|
}
|
|
}
|
|
|
|
return dictionary
|
|
}
|
|
|
|
func IterSeqToSlice[T any](iterator iter.Seq[T]) []T {
|
|
|
|
var slice []T
|
|
|
|
for item := range iterator {
|
|
slice = append(slice, item)
|
|
}
|
|
|
|
return slice
|
|
}
|
|
|
|
func GetMapKeys(value any) []string {
|
|
switch dictionary := value.(type) {
|
|
case map[string]any:
|
|
return IterSeqToSlice(maps.Keys(dictionary))
|
|
}
|
|
return []string{}
|
|
}
|
|
|
|
func ToString(data any) string {
|
|
if data == nil {
|
|
return "null"
|
|
}
|
|
switch value := data.(type) {
|
|
case string:
|
|
return value
|
|
case int:
|
|
return strconv.Itoa(value)
|
|
case int8:
|
|
return strconv.FormatInt(int64(value), 10)
|
|
case int16:
|
|
return strconv.FormatInt(int64(value), 10)
|
|
case int32:
|
|
return strconv.FormatInt(int64(value), 10)
|
|
case int64:
|
|
return strconv.FormatInt(value, 10)
|
|
case uint:
|
|
return strconv.FormatUint(uint64(value), 10)
|
|
case uint8:
|
|
return strconv.FormatUint(uint64(value), 10)
|
|
case uint16:
|
|
return strconv.FormatUint(uint64(value), 10)
|
|
case uint32:
|
|
return strconv.FormatUint(uint64(value), 10)
|
|
case uint64:
|
|
return strconv.FormatUint(value, 10)
|
|
case float32:
|
|
return strconv.FormatFloat(float64(value), 'f', -1, 32)
|
|
case float64:
|
|
return strconv.FormatFloat(value, 'f', -1, 64)
|
|
case bool:
|
|
return strconv.FormatBool(value)
|
|
}
|
|
json, error := json.Marshal(data)
|
|
if error != nil {
|
|
return "[object]"
|
|
}
|
|
return string(json)
|
|
}
|
|
|
|
func StringVariables(_string string, variables any, _default *any) string {
|
|
|
|
var dictionary map[string]any = GetDictionary(variables, false)
|
|
var has_default bool = _default != nil
|
|
var default_processed string = Get[string](_default, nil)
|
|
|
|
return Patterns.Replace(_string, Patterns.RE_STRING_VARIABLES, func(match PatternMatch) string {
|
|
if value, exists := dictionary[match.Groups[1]]; exists {
|
|
return ToString(value)
|
|
}
|
|
if has_default {
|
|
return default_processed
|
|
}
|
|
return match.Groups[0]
|
|
})
|
|
}
|
|
|
|
func ToSliceAny[T any](data []T) []any {
|
|
|
|
var slice_any []any = []any{}
|
|
|
|
for _, value := range data {
|
|
slice_any = append(slice_any, value)
|
|
}
|
|
|
|
return slice_any
|
|
}
|
|
|
|
func ToMapAny[T any](data map[string]T) map[string]any {
|
|
|
|
var map_any map[string]any = map[string]any{}
|
|
|
|
for key, value := range data {
|
|
map_any[key] = value
|
|
}
|
|
|
|
return map_any
|
|
}
|
|
|
|
func JSONEncode(data any) string {
|
|
|
|
json, _ := json.Marshal(data)
|
|
|
|
return string(json)
|
|
}
|
|
|
|
func GetArray(data any) []any {
|
|
switch value := data.(type) {
|
|
case []any:
|
|
return value
|
|
}
|
|
return []any{data}
|
|
}
|
|
|
|
func GetArgument[T any](arguments []any, i int, _default *T) *T {
|
|
if arguments != nil && i >= 0 && i < len(arguments) {
|
|
switch value := arguments[i].(type) {
|
|
case *T:
|
|
return value
|
|
case T:
|
|
return &value
|
|
}
|
|
}
|
|
return _default
|
|
}
|
|
|
|
func Base64Encode(_string string) string {
|
|
return base64.StdEncoding.EncodeToString([]byte(_string))
|
|
}
|
|
|
|
func Base64Decode(_string string) string {
|
|
|
|
data, error := base64.StdEncoding.DecodeString(_string)
|
|
|
|
if error != nil {
|
|
return ""
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func VariablesEncode(value any) string {
|
|
return Base64Encode(JSONEncode(value))
|
|
}
|