mirror of
https://github.com/tiny-craft/tiny-rdm.git
synced 2025-10-05 15:16:54 +08:00
perf: support display binary key name which unreadable(convert to hex string) #49
This commit is contained in:
19
backend/utils/string/common.go
Normal file
19
backend/utils/string/common.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package strutil
|
||||
|
||||
import "unicode/utf8"
|
||||
|
||||
func containsBinary(str string) bool {
|
||||
//buf := []byte(str)
|
||||
//size := 0
|
||||
//for start := 0; start < len(buf); start += size {
|
||||
// var r rune
|
||||
// if r, size = utf8.DecodeRune(buf[start:]); r == utf8.RuneError {
|
||||
// return true
|
||||
// }
|
||||
//}
|
||||
|
||||
if !utf8.ValidString(str) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
@@ -14,7 +14,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"tinyrdm/backend/types"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ConvertTo convert string to specified type
|
||||
@@ -55,7 +54,7 @@ func ConvertTo(str, targetType string) (value, resultType string) {
|
||||
return
|
||||
|
||||
case types.HEX:
|
||||
if hexStr, ok := decodeHex(str); ok {
|
||||
if hexStr, ok := decodeToHex(str); ok {
|
||||
value = hexStr
|
||||
} else {
|
||||
value = str
|
||||
@@ -162,8 +161,8 @@ func autoToType(str string) (value, resultType string) {
|
||||
return
|
||||
}
|
||||
|
||||
if isBinary(str) {
|
||||
if value, ok = decodeHex(str); ok {
|
||||
if containsBinary(str) {
|
||||
if value, ok = decodeToHex(str); ok {
|
||||
resultType = types.HEX
|
||||
return
|
||||
}
|
||||
@@ -175,22 +174,6 @@ func autoToType(str string) (value, resultType string) {
|
||||
return
|
||||
}
|
||||
|
||||
func isBinary(str string) bool {
|
||||
//buf := []byte(str)
|
||||
//size := 0
|
||||
//for start := 0; start < len(buf); start += size {
|
||||
// var r rune
|
||||
// if r, size = utf8.DecodeRune(buf[start:]); r == utf8.RuneError {
|
||||
// return true
|
||||
// }
|
||||
//}
|
||||
|
||||
if !utf8.ValidString(str) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func decodeJson(str string) (string, bool) {
|
||||
var data any
|
||||
if (strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}")) ||
|
||||
@@ -220,7 +203,7 @@ func decodeBinary(str string) (string, bool) {
|
||||
return binary.String(), true
|
||||
}
|
||||
|
||||
func decodeHex(str string) (string, bool) {
|
||||
func decodeToHex(str string) (string, bool) {
|
||||
decodeStr := hex.EncodeToString([]byte(str))
|
||||
var resultStr strings.Builder
|
||||
for i := 0; i < len(decodeStr); i += 2 {
|
||||
|
76
backend/utils/string/key_convert.go
Normal file
76
backend/utils/string/key_convert.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
sliceutil "tinyrdm/backend/utils/slice"
|
||||
)
|
||||
|
||||
// EncodeRedisKey encode the redis key to integer array
|
||||
// if key contains binary which could not display on ui, convert the key to char array
|
||||
func EncodeRedisKey(key string) any {
|
||||
if containsBinary(key) {
|
||||
b := []byte(key)
|
||||
arr := make([]int, len(b))
|
||||
for i, bb := range b {
|
||||
arr[i] = int(bb)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
// DecodeRedisKey decode redis key to readable string
|
||||
func DecodeRedisKey(key any) string {
|
||||
switch key.(type) {
|
||||
case string:
|
||||
return key.(string)
|
||||
|
||||
case []any:
|
||||
arr := key.([]any)
|
||||
bytes := sliceutil.Map(arr, func(i int) byte {
|
||||
if c, ok := AnyToInt(arr[i]); ok {
|
||||
return byte(c)
|
||||
}
|
||||
return '0'
|
||||
})
|
||||
return string(bytes)
|
||||
|
||||
case []int:
|
||||
arr := key.([]int)
|
||||
b := make([]byte, len(arr))
|
||||
for i, bb := range arr {
|
||||
b[i] = byte(bb)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// AnyToInt convert any value to int
|
||||
func AnyToInt(val any) (int, bool) {
|
||||
switch val.(type) {
|
||||
case string:
|
||||
num, err := strconv.Atoi(val.(string))
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return num, true
|
||||
case float64:
|
||||
return int(val.(float64)), true
|
||||
case float32:
|
||||
return int(val.(float32)), true
|
||||
case int64:
|
||||
return int(val.(int64)), true
|
||||
case int32:
|
||||
return int(val.(int32)), true
|
||||
case int:
|
||||
return val.(int), true
|
||||
case bool:
|
||||
if val.(bool) {
|
||||
return 1, true
|
||||
} else {
|
||||
return 0, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
Reference in New Issue
Block a user