init code
This commit is contained in:
42
internal/utils/collectionx/count_map.go
Normal file
42
internal/utils/collectionx/count_map.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package collectionx
|
||||
|
||||
import "github.com/samber/lo"
|
||||
|
||||
type CountMapN[T comparable] struct {
|
||||
N int
|
||||
Map map[T][]int
|
||||
}
|
||||
|
||||
func NewCountMapN[T comparable](n int) *CountMapN[T] {
|
||||
return &CountMapN[T]{
|
||||
N: n,
|
||||
Map: make(map[T][]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Inc(key T, ith int, value int) {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
m = make([]int, cm.N)
|
||||
cm.Map[key] = m
|
||||
}
|
||||
|
||||
m[ith] += value
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Get(key T, ith int) int {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
|
||||
return m[ith]
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Sum(key T) int {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return lo.Sum(m)
|
||||
}
|
||||
31
internal/utils/convertx/number.go
Normal file
31
internal/utils/convertx/number.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package convertx
|
||||
|
||||
import "strconv"
|
||||
|
||||
func Int642String(i int64) string {
|
||||
return strconv.FormatInt(i, 10)
|
||||
}
|
||||
|
||||
func String2Float64(s string, defValue ...float64) float64 {
|
||||
f64, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return defValue[0]
|
||||
}
|
||||
return f64
|
||||
}
|
||||
|
||||
func String2Int(s string, defValue ...int) int {
|
||||
i64, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return defValue[0]
|
||||
}
|
||||
return i64
|
||||
}
|
||||
|
||||
func String2Int64(s string, defValue ...int64) int64 {
|
||||
i64, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return defValue[0]
|
||||
}
|
||||
return i64
|
||||
}
|
||||
47
internal/utils/envx/envx.go
Normal file
47
internal/utils/envx/envx.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package envx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"me.thuanle/bbot/internal/configs"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetEnvJsonArray[T any](key string) ([]T, error) {
|
||||
values, found := os.LookupEnv(key)
|
||||
if !found {
|
||||
return nil, errors.New("env not found. Key: " + key)
|
||||
}
|
||||
var des []T
|
||||
err := json.Unmarshal([]byte(values), &des)
|
||||
if configs.LogEnv {
|
||||
if err != nil {
|
||||
log.Error().Err(err).
|
||||
Str("key", key).
|
||||
Str("value", values).
|
||||
Msg("Failed to load env")
|
||||
} else {
|
||||
log.Info().
|
||||
Str("key", key).
|
||||
Interface("value", des).
|
||||
Msg("Loaded env")
|
||||
}
|
||||
}
|
||||
return des, err
|
||||
}
|
||||
|
||||
func GetEnvBool(key string) (bool, error) {
|
||||
value, found := os.LookupEnv(key)
|
||||
if !found {
|
||||
return false, errors.New("env not found. Key: " + key)
|
||||
}
|
||||
val := value == "true" || value == "1"
|
||||
if configs.LogEnv {
|
||||
log.Info().
|
||||
Str("key", key).
|
||||
Bool("value", val).
|
||||
Msg("Loaded env")
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
9
internal/utils/errorx/error.go
Normal file
9
internal/utils/errorx/error.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package errorx
|
||||
|
||||
import "github.com/rs/zerolog/log"
|
||||
|
||||
func PanicOnError(err error, msg string) {
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg(msg)
|
||||
}
|
||||
}
|
||||
23
internal/utils/mathx/maxmin.go
Normal file
23
internal/utils/mathx/maxmin.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package mathx
|
||||
|
||||
import "cmp"
|
||||
|
||||
func MaxN[T cmp.Ordered](a T, b ...T) T {
|
||||
m := a
|
||||
for _, v := range b {
|
||||
if v > m {
|
||||
m = v
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func MinN[T cmp.Ordered](a T, b ...T) T {
|
||||
m := a
|
||||
for _, v := range b {
|
||||
if v < m {
|
||||
m = v
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
21
internal/utils/netx/ip.go
Normal file
21
internal/utils/netx/ip.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package netx
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetPublicIp() string {
|
||||
req, err := http.Get("http://ip.thuanle.me")
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
defer req.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
return string(body)
|
||||
}
|
||||
10
internal/utils/opx/ternary.go
Normal file
10
internal/utils/opx/ternary.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package opx
|
||||
|
||||
// Ite If-then-else
|
||||
func Ite[T any](condition bool, trueValue T, falseValue T) T {
|
||||
if condition {
|
||||
return trueValue
|
||||
} else {
|
||||
return falseValue
|
||||
}
|
||||
}
|
||||
9
internal/utils/slicex/slice.go
Normal file
9
internal/utils/slicex/slice.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package slicex
|
||||
|
||||
import "sort"
|
||||
|
||||
func Sort[T any](slice []T, compareTo func(o1, o2 T) bool) {
|
||||
sort.Slice(slice, func(i, j int) bool {
|
||||
return compareTo(slice[i], slice[j])
|
||||
})
|
||||
}
|
||||
11
internal/utils/stringx/replace.go
Normal file
11
internal/utils/stringx/replace.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package stringx
|
||||
|
||||
import "strings"
|
||||
|
||||
func ReplaceSuffix(s string, suffix string, newSuffix string) (string, bool) {
|
||||
if strings.HasSuffix(s, suffix) {
|
||||
replaced := s[:len(s)-len(suffix)] + newSuffix
|
||||
return replaced, true
|
||||
}
|
||||
return s, false
|
||||
}
|
||||
9
internal/utils/stringx/validation.go
Normal file
9
internal/utils/stringx/validation.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package stringx
|
||||
|
||||
import "regexp"
|
||||
|
||||
func IsAlphaNumeric(s string) bool {
|
||||
match, _ := regexp.MatchString("^[a-zA-Z0-9]*$", s)
|
||||
return match
|
||||
|
||||
}
|
||||
19
internal/utils/timex/cd.go
Normal file
19
internal/utils/timex/cd.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package timex
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CdMinuteStringDuration(dur time.Duration) string {
|
||||
dur -= dur % time.Minute
|
||||
rString := dur.String()
|
||||
if strings.HasSuffix(rString, "0s") {
|
||||
rString = rString[:len(rString)-2]
|
||||
}
|
||||
return rString
|
||||
}
|
||||
|
||||
func CdMinuteStringTime(t time.Time) string {
|
||||
return CdMinuteStringDuration(-time.Since(t))
|
||||
}
|
||||
6
internal/utils/typex/pair.go
Normal file
6
internal/utils/typex/pair.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package typex
|
||||
|
||||
type Pair[T any, V any] struct {
|
||||
First T
|
||||
Second V
|
||||
}
|
||||
Reference in New Issue
Block a user