|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package utils
-
- import (
- "fmt"
- "github.com/syyongx/php2go"
- "reflect"
- "sort"
- "strings"
- )
-
- func Implode(glue string, args ...interface{}) string {
- data := make([]string, len(args))
- for i, s := range args {
- data[i] = fmt.Sprint(s)
- }
- return strings.Join(data, glue)
- }
-
-
- func InArr(target string, str_array []string) bool {
- for _, element := range str_array {
- if target == element {
- return true
- }
- }
- return false
- }
-
-
- func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
- result = make(map[string]interface{})
- t := reflect.TypeOf(array)
- v := reflect.ValueOf(array)
- if t.Kind() != reflect.Slice {
- return nil, nil
- }
- if v.Len() == 0 {
- return nil, nil
- }
- for i := 0; i < v.Len(); i++ {
- indexv := v.Index(i)
- if indexv.Type().Kind() != reflect.Struct {
- return nil, nil
- }
- mapKeyInterface := indexv.FieldByName(key)
- if mapKeyInterface.Kind() == reflect.Invalid {
- return nil, nil
- }
- mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
- if err != nil {
- return nil, err
- }
- result[mapKeyString] = indexv.Interface()
- }
- return result, err
- }
-
-
- func InterfaceToString(v interface{}) (result string, err error) {
- switch reflect.TypeOf(v).Kind() {
- case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
- result = fmt.Sprintf("%v", v)
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- result = fmt.Sprintf("%v", v)
- case reflect.String:
- result = v.(string)
- default:
- err = nil
- }
- return result, err
- }
-
- func HideTrueName(name string) string {
- res := "**"
- if name != "" {
- runs := []rune(name)
- leng := len(runs)
- if leng <= 3 {
- res = string(runs[0:1]) + res
- } else if leng < 5 {
- res = string(runs[0:2]) + res
- } else if leng < 10 {
- res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
- } else if leng < 16 {
- res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
- } else {
- res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
- }
- }
- return res
- }
- func GetQueryParam(uri string) map[string]string {
-
- uriList := strings.Split(uri, "?")
- var query = make(map[string]string, 0)
-
- if len(uriList) == 2 {
-
- var queryList = strings.Split(uriList[1], "&")
- if len(queryList) > 0 {
-
- for _, v := range queryList {
- var valueList = strings.Split(v, "=")
- if len(valueList) == 2 {
- value, _ := php2go.URLDecode(valueList[1])
- if value == "" {
- value = valueList[1]
- }
- query[valueList[0]] = value
- }
- }
- }
- }
- return query
- }
-
-
-
-
-
-
-
- func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
- var list []string
- var keyList []string
- m := make(map[string]int)
- if len(exceptKeys) > 0 {
- for _, except := range exceptKeys {
- m[except] = 1
- }
- }
- for k := range data {
- if _, ok := m[k]; ok {
- continue
- }
- value := data[k]
- if !includeEmpty && value == "" {
- continue
- }
- if onlyValues {
- keyList = append(keyList, k)
- } else {
- list = append(list, fmt.Sprintf("%s=%s", k, value))
- }
- }
- if onlyValues {
- sort.Strings(keyList)
- for _, v := range keyList {
- list = append(list, AnyToString(data[v]))
- }
- } else {
- sort.Strings(list)
- }
- return strings.Join(list, sep)
- }
|