mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
update:optimize slice find and Unquote func (#5108)
This commit is contained in:
@@ -16,8 +16,10 @@ func getBoolFromKVOrDefault(properties map[string]string, key string, def bool)
|
|||||||
if len(val) == 0 {
|
if len(val) == 0 {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
str := util.Unquote(val[0])
|
//I think this function and those below should handle error, but they didn't.
|
||||||
if len(str) == 0 {
|
//Since a default value (def) is provided, any parsing errors will result in the default being returned.
|
||||||
|
str, err := strconv.Unquote(val[0])
|
||||||
|
if err != nil || len(str) == 0 {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
res, _ := strconv.ParseBool(str)
|
res, _ := strconv.ParseBool(str)
|
||||||
@@ -33,8 +35,8 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri
|
|||||||
if len(val) == 0 {
|
if len(val) == 0 {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
str := util.Unquote(val[0])
|
str, err := strconv.Unquote(val[0])
|
||||||
if len(str) == 0 {
|
if err != nil || len(str) == 0 {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
@@ -50,8 +52,8 @@ func getListFromInfoOrDefault(properties map[string]string, key string, def []st
|
|||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
str := util.Unquote(val[0])
|
str, err := strconv.Unquote(val[0])
|
||||||
if len(str) == 0 {
|
if err != nil || len(str) == 0 {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
resp := util.FieldsAndTrimSpace(str, commaRune)
|
resp := util.FieldsAndTrimSpace(str, commaRune)
|
||||||
@@ -66,8 +68,8 @@ func getFirstUsableString(def ...string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
for _, val := range def {
|
for _, val := range def {
|
||||||
str := util.Unquote(val)
|
str, err := strconv.Unquote(val)
|
||||||
if len(str) != 0 {
|
if err == nil && len(str) != 0 {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/console"
|
"github.com/zeromicro/go-zero/tools/goctl/util/console"
|
||||||
@@ -54,14 +56,9 @@ func Untitle(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Index returns the index where the item equal,it will return -1 if mismatched
|
// Index returns the index where the item equal,it will return -1 if mismatched
|
||||||
|
// Deprecated: use slices.Index instead
|
||||||
func Index(slice []string, item string) int {
|
func Index(slice []string, item string) int {
|
||||||
for i := range slice {
|
return slices.Index(slice, item)
|
||||||
if slice[i] == item {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SafeString converts the input string into a safe naming style in golang
|
// SafeString converts the input string into a safe naming style in golang
|
||||||
@@ -134,21 +131,13 @@ func FieldsAndTrimSpace(s string, f func(r rune) bool) []string {
|
|||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Deprecated: This function implementation is incomplete and does not properly handle exceptional input cases.
|
||||||
|
//We strongly recommend using the standard library's strconv.Unquote function instead,
|
||||||
|
//which provides robust error handling and comprehensive support for various input formats.
|
||||||
func Unquote(s string) string {
|
func Unquote(s string) string {
|
||||||
if len(s) == 0 {
|
ns, err := strconv.Unquote(s)
|
||||||
return s
|
if err != nil {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
left := s[0]
|
return ns
|
||||||
|
|
||||||
if left == '`' || left == '"' {
|
|
||||||
s = s[1:len(s)]
|
|
||||||
}
|
|
||||||
if len(s) == 0 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
right := s[len(s)-1]
|
|
||||||
if right == '`' || right == '"' {
|
|
||||||
s = s[0 : len(s)-1]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user