2022-08-28 13:27:41 +00:00
|
|
|
package helper
|
|
|
|
|
2022-09-08 02:54:37 +00:00
|
|
|
import (
|
2023-03-19 14:48:23 +00:00
|
|
|
"context"
|
2023-03-12 12:28:57 +00:00
|
|
|
"fmt"
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-02-15 16:32:02 +00:00
|
|
|
"net/url"
|
2022-09-08 02:54:37 +00:00
|
|
|
"reflect"
|
2023-03-12 12:28:57 +00:00
|
|
|
"strconv"
|
2023-02-15 16:32:02 +00:00
|
|
|
"strings"
|
2022-09-08 02:54:37 +00:00
|
|
|
)
|
|
|
|
|
2022-09-26 13:25:41 +00:00
|
|
|
func ToAny[T any](v T) any {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2023-02-05 12:33:33 +00:00
|
|
|
func Or[T any](is bool, left, right T) T {
|
|
|
|
if is {
|
|
|
|
return left
|
|
|
|
}
|
|
|
|
return right
|
|
|
|
}
|
|
|
|
|
2023-01-21 11:31:23 +00:00
|
|
|
func StructColumnToSlice[T any, M any](arr []M, field string) (r []T) {
|
2022-09-08 02:54:37 +00:00
|
|
|
for i := 0; i < len(arr); i++ {
|
|
|
|
v := reflect.ValueOf(arr[i]).FieldByName(field).Interface()
|
|
|
|
if val, ok := v.(T); ok {
|
|
|
|
r = append(r, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-02-15 16:32:02 +00:00
|
|
|
|
|
|
|
func UrlScheme(u string, isHttps bool) string {
|
|
|
|
return Or(isHttps,
|
|
|
|
strings.Replace(u, "http://", "https://", 1),
|
|
|
|
strings.Replace(u, "https://", "http://", 1),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CutUrlHost(u string) string {
|
|
|
|
ur, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
ur.Scheme = ""
|
|
|
|
ur.Host = ""
|
|
|
|
return ur.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Defaults[T comparable](v, defaults T) T {
|
|
|
|
var zero T
|
|
|
|
if v == zero {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
func DefaultVal[T any](v, defaults T) T {
|
|
|
|
var zero T
|
|
|
|
if reflect.DeepEqual(zero, v) {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2023-03-12 12:28:57 +00:00
|
|
|
|
|
|
|
func IsZero[T comparable](t T) bool {
|
|
|
|
var vv T
|
|
|
|
return vv != t
|
|
|
|
}
|
|
|
|
func IsZeros(v any) bool {
|
|
|
|
switch v.(type) {
|
|
|
|
case int64, int, int8, int16, int32, uint64, uint, uint8, uint16, uint32:
|
|
|
|
i := fmt.Sprintf("%d", v)
|
2023-03-14 05:50:13 +00:00
|
|
|
return str.ToInt[int64](i) == 0
|
2023-03-12 12:28:57 +00:00
|
|
|
case float32, float64:
|
|
|
|
f := fmt.Sprintf("%v", v)
|
|
|
|
ff, _ := strconv.ParseFloat(f, 64)
|
|
|
|
return ff == float64(0)
|
|
|
|
case bool:
|
|
|
|
return v.(bool) == false
|
|
|
|
case string:
|
|
|
|
s := v.(string)
|
|
|
|
return s == ""
|
|
|
|
}
|
2023-03-14 05:50:13 +00:00
|
|
|
return reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface())
|
2023-03-12 12:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToBool[T comparable](t T) bool {
|
|
|
|
v := any(t)
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
s := v.(string)
|
|
|
|
return s != "" && s != "0"
|
|
|
|
}
|
|
|
|
var vv T
|
|
|
|
return vv != t
|
|
|
|
}
|
2023-03-19 14:48:23 +00:00
|
|
|
|
2023-03-20 05:02:40 +00:00
|
|
|
func GetContextVal[V, K any](ctx context.Context, k K, defaults V) V {
|
2023-03-19 14:48:23 +00:00
|
|
|
v := ctx.Value(k)
|
|
|
|
if v == nil {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
vv, ok := v.(V)
|
|
|
|
if !ok {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
return vv
|
|
|
|
}
|