mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 16:59:59 +08:00
Compare commits
3 Commits
v1.9.2
...
tools/goct
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80c320b46e | ||
|
|
bea9d150a1 | ||
|
|
3f756a2cbf |
@@ -24,10 +24,15 @@ func getFirstUsableString(def ...string) string {
|
||||
}
|
||||
|
||||
for _, val := range def {
|
||||
str, err := strconv.Unquote(val)
|
||||
if err == nil && len(str) != 0 {
|
||||
// Try to unquote if it's a quoted string
|
||||
if str, err := strconv.Unquote(val); err == nil && len(str) != 0 {
|
||||
return str
|
||||
}
|
||||
|
||||
// Otherwise, use the value as-is if it's not empty
|
||||
if len(val) != 0 {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -89,3 +89,108 @@ func Test_getListFromInfoOrDefault(t *testing.T) {
|
||||
assert.Equal(t, []string{"query"}, getListFromInfoOrDefault(unquotedProperties, "tags", []string{"default"}))
|
||||
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(unquotedProperties, "empty", []string{"default"}))
|
||||
}
|
||||
|
||||
func Test_getFirstUsableString(t *testing.T) {
|
||||
t.Run("empty input", func(t *testing.T) {
|
||||
result := getFirstUsableString()
|
||||
assert.Equal(t, "", result, "should return empty string for no arguments")
|
||||
})
|
||||
|
||||
t.Run("single plain string", func(t *testing.T) {
|
||||
result := getFirstUsableString("Check server health status.")
|
||||
assert.Equal(t, "Check server health status.", result)
|
||||
})
|
||||
|
||||
t.Run("single quoted string", func(t *testing.T) {
|
||||
// This is how Go would represent a quoted string literal
|
||||
result := getFirstUsableString(`"Check server health status."`)
|
||||
assert.Equal(t, "Check server health status.", result, "should unquote quoted strings")
|
||||
})
|
||||
|
||||
t.Run("multiple plain strings", func(t *testing.T) {
|
||||
result := getFirstUsableString("", "second", "third")
|
||||
assert.Equal(t, "second", result, "should return first non-empty string")
|
||||
})
|
||||
|
||||
t.Run("handler name fallback", func(t *testing.T) {
|
||||
// Simulates the real use case: @doc text, handler name
|
||||
result := getFirstUsableString("", "HealthCheck")
|
||||
assert.Equal(t, "HealthCheck", result, "should fallback to handler name")
|
||||
})
|
||||
|
||||
t.Run("doc text over handler name", func(t *testing.T) {
|
||||
// Simulates the real use case with @doc text
|
||||
result := getFirstUsableString("Check server health status.", "HealthCheck")
|
||||
assert.Equal(t, "Check server health status.", result, "should use doc text over handler name")
|
||||
})
|
||||
|
||||
t.Run("empty strings before valid", func(t *testing.T) {
|
||||
result := getFirstUsableString("", "", "valid")
|
||||
assert.Equal(t, "valid", result, "should skip empty strings")
|
||||
})
|
||||
|
||||
t.Run("all empty strings", func(t *testing.T) {
|
||||
result := getFirstUsableString("", "", "")
|
||||
assert.Equal(t, "", result, "should return empty if all are empty")
|
||||
})
|
||||
|
||||
t.Run("quoted then plain", func(t *testing.T) {
|
||||
result := getFirstUsableString(`"quoted"`, "plain")
|
||||
assert.Equal(t, "quoted", result, "should unquote first quoted string")
|
||||
})
|
||||
|
||||
t.Run("plain then quoted", func(t *testing.T) {
|
||||
result := getFirstUsableString("plain", `"quoted"`)
|
||||
assert.Equal(t, "plain", result, "should use first plain string")
|
||||
})
|
||||
|
||||
t.Run("invalid quoted string", func(t *testing.T) {
|
||||
// String that looks quoted but isn't valid Go syntax
|
||||
result := getFirstUsableString(`"incomplete`, "fallback")
|
||||
assert.Equal(t, `"incomplete`, result, "should use as-is if unquote fails but not empty")
|
||||
})
|
||||
|
||||
t.Run("whitespace only", func(t *testing.T) {
|
||||
result := getFirstUsableString(" ", "fallback")
|
||||
assert.Equal(t, " ", result, "should not trim whitespace, return as-is")
|
||||
})
|
||||
|
||||
t.Run("real world API doc scenario", func(t *testing.T) {
|
||||
// This is the actual bug scenario from issue #5229
|
||||
atDocText := "Check server health status."
|
||||
handlerName := "HealthCheck"
|
||||
|
||||
result := getFirstUsableString(atDocText, handlerName)
|
||||
assert.Equal(t, "Check server health status.", result,
|
||||
"should use @doc text for API summary")
|
||||
})
|
||||
|
||||
t.Run("real world with empty doc", func(t *testing.T) {
|
||||
// When @doc is empty, should fall back to handler name
|
||||
atDocText := ""
|
||||
handlerName := "HealthCheck"
|
||||
|
||||
result := getFirstUsableString(atDocText, handlerName)
|
||||
assert.Equal(t, "HealthCheck", result,
|
||||
"should fallback to handler name when @doc is empty")
|
||||
})
|
||||
|
||||
t.Run("complex summary with special characters", func(t *testing.T) {
|
||||
result := getFirstUsableString("Get user by ID: /users/{id}")
|
||||
assert.Equal(t, "Get user by ID: /users/{id}", result,
|
||||
"should handle special characters in plain strings")
|
||||
})
|
||||
|
||||
t.Run("multiline string", func(t *testing.T) {
|
||||
result := getFirstUsableString("Line 1\nLine 2")
|
||||
assert.Equal(t, "Line 1\nLine 2", result,
|
||||
"should handle multiline strings")
|
||||
})
|
||||
|
||||
t.Run("unicode characters", func(t *testing.T) {
|
||||
result := getFirstUsableString("健康检查", "HealthCheck")
|
||||
assert.Equal(t, "健康检查", result,
|
||||
"should handle unicode characters")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ require (
|
||||
github.com/withfig/autocomplete-tools/integrations/cobra v1.2.1
|
||||
github.com/zeromicro/antlr v0.0.1
|
||||
github.com/zeromicro/ddl-parser v1.0.5
|
||||
github.com/zeromicro/go-zero v1.9.1
|
||||
github.com/zeromicro/go-zero v1.9.2
|
||||
golang.org/x/text v0.22.0
|
||||
google.golang.org/grpc v1.65.0
|
||||
google.golang.org/protobuf v1.36.5
|
||||
|
||||
@@ -185,8 +185,8 @@ github.com/zeromicro/antlr v0.0.1 h1:CQpIn/dc0pUjgGQ81y98s/NGOm2Hfru2NNio2I9mQgk
|
||||
github.com/zeromicro/antlr v0.0.1/go.mod h1:nfpjEwFR6Q4xGDJMcZnCL9tEfQRgszMwu3rDz2Z+p5M=
|
||||
github.com/zeromicro/ddl-parser v1.0.5 h1:LaVqHdzMTjasua1yYpIYaksxKqRzFrEukj2Wi2EbWaQ=
|
||||
github.com/zeromicro/ddl-parser v1.0.5/go.mod h1:ISU/8NuPyEpl9pa17Py9TBPetMjtsiHrb9f5XGiYbo8=
|
||||
github.com/zeromicro/go-zero v1.9.1 h1:GZCl4jun/ZgZHnSvX3SSNDHf+tEGmEQ8x2Z23xjHa9g=
|
||||
github.com/zeromicro/go-zero v1.9.1/go.mod h1:bHOl7Xr7EV/iHZWEqsUNJwFc/9WgAMrPpPagYvOaMtY=
|
||||
github.com/zeromicro/go-zero v1.9.2 h1:ZXOXBIcazZ1pWAMiHyVnDQ3Sxwy7DYPzjE89Qtj9vqM=
|
||||
github.com/zeromicro/go-zero v1.9.2/go.mod h1:k8YBMEFZKjTd4q/qO5RCW+zDgUlNyAs5vue3P4/Kmn0=
|
||||
go.etcd.io/etcd/api/v3 v3.5.15 h1:3KpLJir1ZEBrYuV2v+Twaa/e2MdDCEZ/70H+lzEiwsk=
|
||||
go.etcd.io/etcd/api/v3 v3.5.15/go.mod h1:N9EhGzXq58WuMllgH9ZvnEr7SI9pS0k0+DHZezGp7jM=
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 h1:fo0HpWz/KlHGMCC+YejpiCmyWDEuIpnTDzpJLB5fWlA=
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// BuildVersion is the version of goctl.
|
||||
const BuildVersion = "1.9.1"
|
||||
const BuildVersion = "1.9.2"
|
||||
|
||||
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-beta": 2, "beta": 3, "released": 4, "": 5}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package util
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/console"
|
||||
@@ -130,14 +129,3 @@ func FieldsAndTrimSpace(s string, f func(r rune) bool) []string {
|
||||
}
|
||||
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 {
|
||||
ns, err := strconv.Unquote(s)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return ns
|
||||
}
|
||||
|
||||
@@ -76,40 +76,40 @@ func TestEscapeGoKeyword(t *testing.T) {
|
||||
|
||||
func TestFieldsAndTrimSpace(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
name string
|
||||
input string
|
||||
delimiter func(r rune) bool
|
||||
expected []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "Comma-separated values",
|
||||
input: "a, b, c",
|
||||
name: "Comma-separated values",
|
||||
input: "a, b, c",
|
||||
delimiter: func(r rune) bool { return r == ',' },
|
||||
expected: []string{"a", " b", " c"},
|
||||
expected: []string{"a", " b", " c"},
|
||||
},
|
||||
{
|
||||
name: "Space-separated values",
|
||||
input: "a b c",
|
||||
name: "Space-separated values",
|
||||
input: "a b c",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string{"a", "b", "c"},
|
||||
expected: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
name: "Mixed whitespace",
|
||||
input: "a\tb\nc",
|
||||
name: "Mixed whitespace",
|
||||
input: "a\tb\nc",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string{"a", "b", "c"},
|
||||
expected: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
name: "Empty input",
|
||||
input: "",
|
||||
name: "Empty input",
|
||||
input: "",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string(nil),
|
||||
expected: []string(nil),
|
||||
},
|
||||
{
|
||||
name: "Trailing and leading spaces",
|
||||
input: " a , b , c ",
|
||||
name: "Trailing and leading spaces",
|
||||
input: " a , b , c ",
|
||||
delimiter: func(r rune) bool { return r == ',' },
|
||||
expected: []string{" a ", " b ", " c "},
|
||||
expected: []string{" a ", " b ", " c "},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -120,20 +120,3 @@ func TestFieldsAndTrimSpace(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnquote(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{input: `"hello"`, expected: `hello`},
|
||||
{input: "`world`", expected: `world`},
|
||||
{input: `"foo'bar"`, expected: `foo'bar`},
|
||||
{input: "", expected: ""},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := Unquote(tc.input)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user