mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 09:50:00 +08:00
feature/goctl-api-swagger (#4780)
This commit is contained in:
@@ -121,3 +121,34 @@ func IsEmptyStringOrWhiteSpace(s string) bool {
|
||||
v := TrimWhiteSpace(s)
|
||||
return len(v) == 0
|
||||
}
|
||||
|
||||
func FieldsAndTrimSpace(s string, f func(r rune) bool) []string {
|
||||
fields := strings.FieldsFunc(s, f)
|
||||
var resp []string
|
||||
for _, v := range fields {
|
||||
val := TrimWhiteSpace(v)
|
||||
if len(val) > 0 {
|
||||
resp = append(resp, v)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func Unquote(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
}
|
||||
left := s[0]
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -72,3 +73,67 @@ func TestEscapeGoKeyword(t *testing.T) {
|
||||
assert.False(t, isGolangKeyword(strings.Title(k)))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldsAndTrimSpace(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input string
|
||||
delimiter func(r rune) bool
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "Comma-separated values",
|
||||
input: "a, b, c",
|
||||
delimiter: func(r rune) bool { return r == ',' },
|
||||
expected: []string{"a", " b", " c"},
|
||||
},
|
||||
{
|
||||
name: "Space-separated values",
|
||||
input: "a b c",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
name: "Mixed whitespace",
|
||||
input: "a\tb\nc",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
name: "Empty input",
|
||||
input: "",
|
||||
delimiter: unicode.IsSpace,
|
||||
expected: []string(nil),
|
||||
},
|
||||
{
|
||||
name: "Trailing and leading spaces",
|
||||
input: " a , b , c ",
|
||||
delimiter: func(r rune) bool { return r == ',' },
|
||||
expected: []string{" a ", " b ", " c "},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := FieldsAndTrimSpace(tc.input, tc.delimiter)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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