mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-22 14:18:18 +08:00
Compare commits
6 Commits
tools/goct
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03fd74b955 | ||
|
|
f8716fe6fa | ||
|
|
cf21cb2b0b | ||
|
|
61e8894c31 | ||
|
|
7a6c3c8129 | ||
|
|
875fec3e1a |
@@ -92,7 +92,7 @@ Port: 0
|
|||||||
Path: "/",
|
Path: "/",
|
||||||
Handler: nil,
|
Handler: nil,
|
||||||
}, WithJwt("thesecret"), WithSignature(SignatureConf{}),
|
}, WithJwt("thesecret"), WithSignature(SignatureConf{}),
|
||||||
WithJwtTransition("preivous", "thenewone"))
|
WithJwtTransition("previous", "thenewone"))
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
@@ -8,70 +8,66 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
|
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
|
||||||
if len(properties) == 0 {
|
return getOrDefault(properties, key, def, func(str string, def bool) bool {
|
||||||
return def
|
res, err := strconv.ParseBool(str)
|
||||||
}
|
if err != nil {
|
||||||
md := metadata.New(properties)
|
return def
|
||||||
val := md.Get(key)
|
}
|
||||||
if len(val) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
//I think this function and those below should handle error, but they didn't.
|
|
||||||
//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
|
|
||||||
}
|
|
||||||
res, _ := strconv.ParseBool(str)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
func getStringFromKVOrDefault(properties map[string]string, key string, def string) string {
|
return res
|
||||||
if len(properties) == 0 {
|
})
|
||||||
return def
|
|
||||||
}
|
|
||||||
md := metadata.New(properties)
|
|
||||||
val := md.Get(key)
|
|
||||||
if len(val) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
str, err := strconv.Unquote(val[0])
|
|
||||||
if err != nil || len(str) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
|
|
||||||
if len(properties) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
md := metadata.New(properties)
|
|
||||||
val := md.Get(key)
|
|
||||||
if len(val) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
|
|
||||||
str, err := strconv.Unquote(val[0])
|
|
||||||
if err != nil || len(str) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
resp := util.FieldsAndTrimSpace(str, commaRune)
|
|
||||||
if len(resp) == 0 {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFirstUsableString(def ...string) string {
|
func getFirstUsableString(def ...string) string {
|
||||||
if len(def) == 0 {
|
if len(def) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, val := range def {
|
for _, val := range def {
|
||||||
str, err := strconv.Unquote(val)
|
str, err := strconv.Unquote(val)
|
||||||
if err == nil && len(str) != 0 {
|
if err == nil && len(str) != 0 {
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
|
||||||
|
return getOrDefault(properties, key, def, func(str string, def []string) []string {
|
||||||
|
resp := util.FieldsAndTrimSpace(str, commaRune)
|
||||||
|
if len(resp) == 0 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// getOrDefault abstracts the common logic for fetching, unquoting, and defaulting.
|
||||||
|
func getOrDefault[T any](properties map[string]string, key string, def T, convert func(string, T) T) T {
|
||||||
|
if len(properties) == 0 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
md := metadata.New(properties)
|
||||||
|
val := md.Get(key)
|
||||||
|
if len(val) == 0 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
str := val[0]
|
||||||
|
if unquoted, err := strconv.Unquote(str); err == nil {
|
||||||
|
str = unquoted
|
||||||
|
}
|
||||||
|
if len(str) == 0 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
||||||
|
return convert(str, def)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStringFromKVOrDefault(properties map[string]string, key string, def string) string {
|
||||||
|
return getOrDefault(properties, key, def, func(str string, def string) string {
|
||||||
|
return str
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,19 @@ func Test_getBoolFromKVOrDefault(t *testing.T) {
|
|||||||
assert.False(t, getBoolFromKVOrDefault(properties, "empty_value", false))
|
assert.False(t, getBoolFromKVOrDefault(properties, "empty_value", false))
|
||||||
assert.False(t, getBoolFromKVOrDefault(nil, "nil", false))
|
assert.False(t, getBoolFromKVOrDefault(nil, "nil", false))
|
||||||
assert.False(t, getBoolFromKVOrDefault(map[string]string{}, "empty", false))
|
assert.False(t, getBoolFromKVOrDefault(map[string]string{}, "empty", false))
|
||||||
|
|
||||||
|
// Test with unquoted values (as stored by RawText())
|
||||||
|
unquotedProperties := map[string]string{
|
||||||
|
"enabled": "true",
|
||||||
|
"disabled": "false",
|
||||||
|
"invalid": "notabool",
|
||||||
|
"empty_value": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.True(t, getBoolFromKVOrDefault(unquotedProperties, "enabled", false))
|
||||||
|
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "disabled", true))
|
||||||
|
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "invalid", false))
|
||||||
|
assert.False(t, getBoolFromKVOrDefault(unquotedProperties, "empty_value", false))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_getStringFromKVOrDefault(t *testing.T) {
|
func Test_getStringFromKVOrDefault(t *testing.T) {
|
||||||
@@ -34,6 +47,17 @@ func Test_getStringFromKVOrDefault(t *testing.T) {
|
|||||||
assert.Equal(t, "default", getStringFromKVOrDefault(properties, "missing", "default"))
|
assert.Equal(t, "default", getStringFromKVOrDefault(properties, "missing", "default"))
|
||||||
assert.Equal(t, "default", getStringFromKVOrDefault(nil, "nil", "default"))
|
assert.Equal(t, "default", getStringFromKVOrDefault(nil, "nil", "default"))
|
||||||
assert.Equal(t, "default", getStringFromKVOrDefault(map[string]string{}, "empty", "default"))
|
assert.Equal(t, "default", getStringFromKVOrDefault(map[string]string{}, "empty", "default"))
|
||||||
|
|
||||||
|
// Test with unquoted values (as stored by RawText())
|
||||||
|
unquotedProperties := map[string]string{
|
||||||
|
"name": "example",
|
||||||
|
"title": "Demo API",
|
||||||
|
"empty": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "example", getStringFromKVOrDefault(unquotedProperties, "name", "default"))
|
||||||
|
assert.Equal(t, "Demo API", getStringFromKVOrDefault(unquotedProperties, "title", "default"))
|
||||||
|
assert.Equal(t, "default", getStringFromKVOrDefault(unquotedProperties, "empty", "default"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_getListFromInfoOrDefault(t *testing.T) {
|
func Test_getListFromInfoOrDefault(t *testing.T) {
|
||||||
@@ -50,4 +74,18 @@ func Test_getListFromInfoOrDefault(t *testing.T) {
|
|||||||
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(map[string]string{
|
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(map[string]string{
|
||||||
"foo": ",,",
|
"foo": ",,",
|
||||||
}, "foo", []string{"default"}))
|
}, "foo", []string{"default"}))
|
||||||
|
|
||||||
|
// Test with unquoted values (as stored by RawText())
|
||||||
|
unquotedProperties := map[string]string{
|
||||||
|
"list": "a, b, c",
|
||||||
|
"schemes": "http,https",
|
||||||
|
"tags": "query",
|
||||||
|
"empty": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: FieldsAndTrimSpace doesn't actually trim the spaces from returned values
|
||||||
|
assert.Equal(t, []string{"a", " b", " c"}, getListFromInfoOrDefault(unquotedProperties, "list", []string{"default"}))
|
||||||
|
assert.Equal(t, []string{"http", "https"}, getListFromInfoOrDefault(unquotedProperties, "schemes", []string{"default"}))
|
||||||
|
assert.Equal(t, []string{"query"}, getListFromInfoOrDefault(unquotedProperties, "tags", []string{"default"}))
|
||||||
|
assert.Equal(t, []string{"default"}, getListFromInfoOrDefault(unquotedProperties, "empty", []string{"default"}))
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -19,7 +19,11 @@ func spec2Paths(ctx Context, srv apiSpec.Service) *spec.Paths {
|
|||||||
for _, route := range group.Routes {
|
for _, route := range group.Routes {
|
||||||
routPath := pathVariable2SwaggerVariable(ctx, route.Path)
|
routPath := pathVariable2SwaggerVariable(ctx, route.Path)
|
||||||
if len(prefix) > 0 && prefix != "." {
|
if len(prefix) > 0 && prefix != "." {
|
||||||
routPath = "/" + path.Clean(prefix) + routPath
|
if routPath == "/" {
|
||||||
|
routPath = "/" + path.Clean(prefix)
|
||||||
|
} else {
|
||||||
|
routPath = "/" + path.Clean(prefix) + routPath
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pathItem := spec2Path(ctx, group, route)
|
pathItem := spec2Path(ctx, group, route)
|
||||||
existPathItem, ok := paths.Paths[routPath]
|
existPathItem, ok := paths.Paths[routPath]
|
||||||
|
|||||||
90
tools/goctl/api/swagger/path_test.go
Normal file
90
tools/goctl/api/swagger/path_test.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package swagger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSpec2PathsWithRootRoute(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
prefix string
|
||||||
|
routePath string
|
||||||
|
expectedPath string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "prefix with root route",
|
||||||
|
prefix: "/api/v1/shoppings",
|
||||||
|
routePath: "/",
|
||||||
|
expectedPath: "/api/v1/shoppings",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "prefix with sub route",
|
||||||
|
prefix: "/api/v1/shoppings",
|
||||||
|
routePath: "/list",
|
||||||
|
expectedPath: "/api/v1/shoppings/list",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty prefix with root route",
|
||||||
|
prefix: "",
|
||||||
|
routePath: "/",
|
||||||
|
expectedPath: "/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty prefix with sub route",
|
||||||
|
prefix: "",
|
||||||
|
routePath: "/list",
|
||||||
|
expectedPath: "/list",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "prefix with trailing slash and root route",
|
||||||
|
prefix: "/api/v1/shoppings/",
|
||||||
|
routePath: "/",
|
||||||
|
expectedPath: "/api/v1/shoppings",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "prefix without leading slash and root route",
|
||||||
|
prefix: "api/v1/shoppings",
|
||||||
|
routePath: "/",
|
||||||
|
expectedPath: "/api/v1/shoppings",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single level prefix with root route",
|
||||||
|
prefix: "/api",
|
||||||
|
routePath: "/",
|
||||||
|
expectedPath: "/api",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
srv := spec.Service{
|
||||||
|
Groups: []spec.Group{
|
||||||
|
{
|
||||||
|
Annotation: spec.Annotation{
|
||||||
|
Properties: map[string]string{
|
||||||
|
propertyKeyPrefix: tt.prefix,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Routes: []spec.Route{
|
||||||
|
{
|
||||||
|
Method: "get",
|
||||||
|
Path: tt.routePath,
|
||||||
|
Handler: "TestHandler",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testingContext(t)
|
||||||
|
paths := spec2Paths(ctx, srv)
|
||||||
|
|
||||||
|
assert.Contains(t, paths.Paths, tt.expectedPath,
|
||||||
|
"Expected path %s not found in generated paths. Got: %v",
|
||||||
|
tt.expectedPath, paths.Paths)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BeforeCommands run before comamnd run to show some migration notes
|
// BeforeCommands run before command run to show some migration notes
|
||||||
func BeforeCommands(dir, style string) error {
|
func BeforeCommands(dir, style string) error {
|
||||||
if err := migrateBefore1_3_4(dir, style); err != nil {
|
if err := migrateBefore1_3_4(dir, style); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ func Install(cacheDir string) (string, error) {
|
|||||||
case vars.OsLinux:
|
case vars.OsLinux:
|
||||||
downloadUrl = url[fmt.Sprintf("%s_%d", vars.OsLinux, bit)]
|
downloadUrl = url[fmt.Sprintf("%s_%d", vars.OsLinux, bit)]
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("unsupport OS: %q", goos)
|
return "", fmt.Errorf("unsupported OS: %q", goos)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := downloader.Download(downloadUrl, tempFile)
|
err := downloader.Download(downloadUrl, tempFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user