Skip to content

Commit 06502d1

Browse files
authored
update:optimize slice find and Unquote func (#5108)
1 parent 3854d6d commit 06502d1

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

tools/goctl/api/swagger/annotation.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ func getBoolFromKVOrDefault(properties map[string]string, key string, def bool)
1616
if len(val) == 0 {
1717
return def
1818
}
19-
str := util.Unquote(val[0])
20-
if len(str) == 0 {
19+
//I think this function and those below should handle error, but they didn't.
20+
//Since a default value (def) is provided, any parsing errors will result in the default being returned.
21+
str, err := strconv.Unquote(val[0])
22+
if err != nil || len(str) == 0 {
2123
return def
2224
}
2325
res, _ := strconv.ParseBool(str)
@@ -33,8 +35,8 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri
3335
if len(val) == 0 {
3436
return def
3537
}
36-
str := util.Unquote(val[0])
37-
if len(str) == 0 {
38+
str, err := strconv.Unquote(val[0])
39+
if err != nil || len(str) == 0 {
3840
return def
3941
}
4042
return str
@@ -50,8 +52,8 @@ func getListFromInfoOrDefault(properties map[string]string, key string, def []st
5052
return def
5153
}
5254

53-
str := util.Unquote(val[0])
54-
if len(str) == 0 {
55+
str, err := strconv.Unquote(val[0])
56+
if err != nil || len(str) == 0 {
5557
return def
5658
}
5759
resp := util.FieldsAndTrimSpace(str, commaRune)
@@ -66,8 +68,8 @@ func getFirstUsableString(def ...string) string {
6668
return ""
6769
}
6870
for _, val := range def {
69-
str := util.Unquote(val)
70-
if len(str) != 0 {
71+
str, err := strconv.Unquote(val)
72+
if err == nil && len(str) != 0 {
7173
return str
7274
}
7375
}

tools/goctl/util/string.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package util
22

33
import (
4+
"slices"
5+
"strconv"
46
"strings"
57

68
"github.com/zeromicro/go-zero/tools/goctl/util/console"
@@ -54,14 +56,9 @@ func Untitle(s string) string {
5456
}
5557

5658
// Index returns the index where the item equal,it will return -1 if mismatched
59+
// Deprecated: use slices.Index instead
5760
func Index(slice []string, item string) int {
58-
for i := range slice {
59-
if slice[i] == item {
60-
return i
61-
}
62-
}
63-
64-
return -1
61+
return slices.Index(slice, item)
6562
}
6663

6764
// 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 {
134131
return resp
135132
}
136133

134+
//Deprecated: This function implementation is incomplete and does not properly handle exceptional input cases.
135+
//We strongly recommend using the standard library's strconv.Unquote function instead,
136+
//which provides robust error handling and comprehensive support for various input formats.
137137
func Unquote(s string) string {
138-
if len(s) == 0 {
139-
return s
140-
}
141-
left := s[0]
142-
143-
if left == '`' || left == '"' {
144-
s = s[1:len(s)]
145-
}
146-
if len(s) == 0 {
147-
return s
148-
}
149-
right := s[len(s)-1]
150-
if right == '`' || right == '"' {
151-
s = s[0 : len(s)-1]
138+
ns, err := strconv.Unquote(s)
139+
if err != nil {
140+
return ""
152141
}
153-
return s
142+
return ns
154143
}

0 commit comments

Comments
 (0)