Skip to content

Commit cf21cb2

Browse files
authored
chore: refactor to remove duplicated code (#5216)
1 parent 61e8894 commit cf21cb2

File tree

1 file changed

+37
-53
lines changed

1 file changed

+37
-53
lines changed

tools/goctl/api/swagger/annotation.go

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,66 @@ import (
88
)
99

1010
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
11-
if len(properties) == 0 {
12-
return def
13-
}
14-
md := metadata.New(properties)
15-
val := md.Get(key)
16-
if len(val) == 0 {
17-
return def
18-
}
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 := val[0]
22-
// Try to unquote if the string is quoted, otherwise use as-is
23-
if unquoted, err := strconv.Unquote(str); err == nil {
24-
str = unquoted
25-
}
26-
if len(str) == 0 {
27-
return def
28-
}
29-
res, _ := strconv.ParseBool(str)
30-
return res
11+
return getOrDefault(properties, key, def, func(str string, def bool) bool {
12+
res, err := strconv.ParseBool(str)
13+
if err != nil {
14+
return def
15+
}
16+
17+
return res
18+
})
3119
}
3220

33-
func getStringFromKVOrDefault(properties map[string]string, key string, def string) string {
34-
if len(properties) == 0 {
35-
return def
36-
}
37-
md := metadata.New(properties)
38-
val := md.Get(key)
39-
if len(val) == 0 {
40-
return def
41-
}
42-
str := val[0]
43-
// Try to unquote if the string is quoted, otherwise use as-is
44-
if unquoted, err := strconv.Unquote(str); err == nil {
45-
str = unquoted
21+
func getFirstUsableString(def ...string) string {
22+
if len(def) == 0 {
23+
return ""
4624
}
47-
if len(str) == 0 {
48-
return def
25+
26+
for _, val := range def {
27+
str, err := strconv.Unquote(val)
28+
if err == nil && len(str) != 0 {
29+
return str
30+
}
4931
}
50-
return str
32+
33+
return ""
5134
}
5235

5336
func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
37+
return getOrDefault(properties, key, def, func(str string, def []string) []string {
38+
resp := util.FieldsAndTrimSpace(str, commaRune)
39+
if len(resp) == 0 {
40+
return def
41+
}
42+
return resp
43+
})
44+
}
45+
46+
// getOrDefault abstracts the common logic for fetching, unquoting, and defaulting.
47+
func getOrDefault[T any](properties map[string]string, key string, def T, convert func(string, T) T) T {
5448
if len(properties) == 0 {
5549
return def
5650
}
51+
5752
md := metadata.New(properties)
5853
val := md.Get(key)
5954
if len(val) == 0 {
6055
return def
6156
}
6257

6358
str := val[0]
64-
// Try to unquote if the string is quoted, otherwise use as-is
6559
if unquoted, err := strconv.Unquote(str); err == nil {
6660
str = unquoted
6761
}
6862
if len(str) == 0 {
6963
return def
7064
}
71-
resp := util.FieldsAndTrimSpace(str, commaRune)
72-
if len(resp) == 0 {
73-
return def
74-
}
75-
return resp
65+
66+
return convert(str, def)
7667
}
7768

78-
func getFirstUsableString(def ...string) string {
79-
if len(def) == 0 {
80-
return ""
81-
}
82-
for _, val := range def {
83-
str, err := strconv.Unquote(val)
84-
if err == nil && len(str) != 0 {
85-
return str
86-
}
87-
}
88-
return ""
69+
func getStringFromKVOrDefault(properties map[string]string, key string, def string) string {
70+
return getOrDefault(properties, key, def, func(str string, def string) string {
71+
return str
72+
})
8973
}

0 commit comments

Comments
 (0)