Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion tools/goctl/api/swagger/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package swagger
import (
"strconv"

"github.com/zeromicro/go-zero/tools/goctl/util"
"google.golang.org/grpc/metadata"

"github.com/zeromicro/go-zero/tools/goctl/util"
)

func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
Expand Down Expand Up @@ -40,6 +41,14 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri
return str
}

func isExist(properties map[string]string, key string) bool {
if len(properties) == 0 {
return false
}
_, ok := properties[key]
return ok
}

func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
if len(properties) == 0 {
return def
Expand Down
4 changes: 3 additions & 1 deletion tools/goctl/api/swagger/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ const (
propertyKeyHost = "host"
propertyKeyBasePath = "basePath"
propertyKeyWrapCodeMsg = "wrapCodeMsg"
propertyKeyWrapCodeMsgMapping = "wrapCodeMsgMapping"
propertyKeyBizCodeEnumDescription = "bizCodeEnumDescription"
)

const (
defaultValueOfPropertyUseDefinition = false
defaultValueOfPropertyUseDefinition = false
defaultValueOfPropertyWrapCodeMsgMapping = `{"code":"code", "data":"data", "msg":"msg"}`
)
12 changes: 11 additions & 1 deletion tools/goctl/api/swagger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Context struct {
UseDefinitions bool
WrapCodeMsg bool
WrapCodeMsgMapping string
BizCodeEnumDescription string
}

Expand All @@ -20,9 +21,18 @@ func contextFromApi(info spec.Info) Context {
if len(info.Properties) == 0 {
return Context{}
}
return Context{

ctx := Context{
UseDefinitions: getBoolFromKVOrDefault(info.Properties, propertyKeyUseDefinitions, defaultValueOfPropertyUseDefinition),
WrapCodeMsg: getBoolFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsg, false),
BizCodeEnumDescription: getStringFromKVOrDefault(info.Properties, propertyKeyBizCodeEnumDescription, "business code"),
}

if !isExist(info.Properties, propertyKeyWrapCodeMsgMapping) {
ctx.WrapCodeMsgMapping = defaultValueOfPropertyWrapCodeMsgMapping
} else {
ctx.WrapCodeMsgMapping = getStringFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsgMapping, "")
}

return ctx
}
66 changes: 46 additions & 20 deletions tools/goctl/api/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/go-openapi/spec"

apiSpec "github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
)
Expand Down Expand Up @@ -259,34 +260,59 @@ func wrapCodeMsgProps(ctx Context, properties spec.SchemaProps, atDoc apiSpec.At
if !ctx.WrapCodeMsg {
return properties
}

type WrapCodeMsgMapping struct {
Code string `json:"code"`
Data string `json:"data"`
Msg string `json:"msg"`
}

var wrapCodeMsgMapping WrapCodeMsgMapping
if err := json.Unmarshal([]byte(ctx.WrapCodeMsgMapping), &wrapCodeMsgMapping); err != nil {
return properties
}

if len(wrapCodeMsgMapping.Data) == 0 {
return properties
}

globalCodeDesc := ctx.BizCodeEnumDescription
methodCodeDesc := getStringFromKVOrDefault(atDoc.Properties, propertyKeyBizCodeEnumDescription, globalCodeDesc)
return spec.SchemaProps{

schemaProps := spec.SchemaProps{
Type: []string{swaggerTypeObject},
Properties: spec.SchemaProperties{
"code": {
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: 0,
},
SchemaProps: spec.SchemaProps{
Type: []string{swaggerTypeInteger},
Description: methodCodeDesc,
},
},
"msg": {
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: "ok",
},
SchemaProps: spec.SchemaProps{
Type: []string{swaggerTypeString},
Description: "business message",
},
},
"data": {
wrapCodeMsgMapping.Data: {
SchemaProps: properties,
},
},
}

if len(wrapCodeMsgMapping.Code) > 0 {
schemaProps.Properties[wrapCodeMsgMapping.Code] = spec.Schema{
SchemaProps: spec.SchemaProps{
Description: methodCodeDesc,
Type: []string{swaggerTypeInteger},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: 0,
},
}
}

if len(wrapCodeMsgMapping.Msg) > 0 {
schemaProps.Properties[wrapCodeMsgMapping.Msg] = spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "business message",
Type: []string{swaggerTypeString},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: "ok",
},
}
}

return schemaProps
}

func specExtensions(api apiSpec.Info) (spec.Extensions, *spec.Info) {
Expand Down
Loading