forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/kavenegar/kavenegar-go/utils.go

170 lines
2.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package kavenegar
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
2024-06-14 08:41:36 +00:00
// ToString ...
2024-02-18 10:42:21 +00:00
func ToString(i interface{}) string {
2024-02-18 10:42:21 +00:00
return strings.Trim(strings.Replace(fmt.Sprint(i), " ", ",", -1), "[]")
2024-02-18 10:42:21 +00:00
}
2024-06-14 08:41:36 +00:00
// ToJson ...
2024-02-18 10:42:21 +00:00
func ToJson(i interface{}) string {
2024-02-18 10:42:21 +00:00
_json, _ := json.Marshal(i)
2024-02-18 10:42:21 +00:00
return string(_json)
2024-02-18 10:42:21 +00:00
}
2024-06-14 08:41:36 +00:00
// ToUnix ...
2024-02-18 10:42:21 +00:00
func ToUnix(t time.Time) string {
2024-02-18 10:42:21 +00:00
return strconv.FormatInt(t.Unix(), 10)
2024-02-18 10:42:21 +00:00
}
2024-06-14 08:41:36 +00:00
// structToUrlValues ...
2024-02-18 10:42:21 +00:00
func structToURLValues(i interface{}) url.Values {
2024-02-18 10:42:21 +00:00
v := url.Values{}
2024-02-18 10:42:21 +00:00
if reflect.ValueOf(i).IsNil() {
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
m := structToMapString(i)
2024-02-18 10:42:21 +00:00
for k, s := range m {
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case len(s) == 1:
2024-02-18 10:42:21 +00:00
v.Set(k, s[0])
2024-02-18 10:42:21 +00:00
case len(s) > 1:
2024-02-18 10:42:21 +00:00
for i := range s {
2024-02-18 10:42:21 +00:00
v.Add(k, s[i])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return v
2024-02-18 10:42:21 +00:00
}
// structToMapString converts struct as map string
2024-02-18 10:42:21 +00:00
func structToMapString(i interface{}) map[string][]string {
2024-02-18 10:42:21 +00:00
ms := map[string][]string{}
2024-02-18 10:42:21 +00:00
iv := reflect.ValueOf(i).Elem()
2024-02-18 10:42:21 +00:00
tp := iv.Type()
for i := 0; i < iv.NumField(); i++ {
2024-02-18 10:42:21 +00:00
if isMap(iv.Field(i)) {
2024-02-18 10:42:21 +00:00
m := iv.Field(i).Interface()
2024-02-18 10:42:21 +00:00
for key, value := range m.(map[string]string) {
2024-02-18 10:42:21 +00:00
ms[key] = []string{value}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
k := tp.Field(i).Name
2024-02-18 10:42:21 +00:00
f := iv.Field(i)
2024-02-18 10:42:21 +00:00
ms[k] = valueToString(f)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return ms
2024-02-18 10:42:21 +00:00
}
func isMap(f reflect.Value) bool {
2024-02-18 10:42:21 +00:00
return reflect.TypeOf(f.Interface()).Kind() == reflect.Map
2024-02-18 10:42:21 +00:00
}
// valueToString converts supported type of f as slice string
2024-02-18 10:42:21 +00:00
func valueToString(f reflect.Value) []string {
2024-02-18 10:42:21 +00:00
var v []string
switch reflect.TypeOf(f.Interface()).Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
v = []string{strconv.FormatInt(f.Int(), 10)}
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
2024-02-18 10:42:21 +00:00
v = []string{strconv.FormatUint(f.Uint(), 10)}
2024-02-18 10:42:21 +00:00
case reflect.Float32:
2024-02-18 10:42:21 +00:00
v = []string{strconv.FormatFloat(f.Float(), 'f', 4, 32)}
2024-02-18 10:42:21 +00:00
case reflect.Float64:
2024-02-18 10:42:21 +00:00
v = []string{strconv.FormatFloat(f.Float(), 'f', 4, 64)}
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
v = []string{strconv.FormatBool(f.Bool())}
2024-02-18 10:42:21 +00:00
case reflect.Slice:
2024-02-18 10:42:21 +00:00
for i := 0; i < f.Len(); i++ {
2024-02-18 10:42:21 +00:00
if s := valueToString(f.Index(i)); len(s) == 1 {
2024-02-18 10:42:21 +00:00
v = append(v, s[0])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.String:
2024-02-18 10:42:21 +00:00
v = []string{f.String()}
2024-02-18 10:42:21 +00:00
}
return v
2024-02-18 10:42:21 +00:00
}