Skip to content

Commit

Permalink
add: 辅助数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-home committed Mar 10, 2023
1 parent 2b37116 commit 92a294c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ Has:
Description:
生成依赖注入的声明源代码文件, 使用@Bean注解, 和inject引入
````


# 数据库注释使用
1. 使用注释@type(int), 强制设置生成的go struct 属性 类型
2. 使用注释@index, 强制生成索引赋值函数
8 changes: 8 additions & 0 deletions console/commands/orm/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ func genOrmStruct(table string, columns []tableColumn, conf Conf, relationships
if column.COLUMN_NAME == "deleted_at" {
column.GoType = "gorm.DeletedAt"
}

// 使用注释@Type(int), 强制设置生成的go struct 属性 类型
if i := strings.Index(column.COLUMN_COMMENT, "@type("); i != -1 {
s := column.COLUMN_COMMENT[i+6:]
e := strings.Index(s, ")")
column.GoType = s[:e]
}

hasField[column.COLUMN_NAME] = true
fieldName := parser.StringToHump(column.COLUMN_NAME)
str += fmt.Sprintf("\n\t%v %v%v`%v json:\"%v\"` // %v", fieldName, p, column.GoType, genGormTag(column), column.COLUMN_NAME, strings.ReplaceAll(column.COLUMN_COMMENT, "\n", " "))
Expand Down
19 changes: 13 additions & 6 deletions console/commands/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func rpcToPath(pge string, service parser.ServiceRpc, swagger *openapi.Spec, now
endpoint.Description = service.Doc + option.Doc
endpoint.Summary = service.Doc + option.Doc
endpoint.Tags = strings.Split(pge, ".")
endpoint.Parameters = messageToParameters(service.Param, nowDirProtoc, allProtoc)
endpoint.Parameters = messageToParameters(option.Key, service.Param, nowDirProtoc, allProtoc)
endpoint.Responses = map[string]*openapi.Response{
"200": messageToResponse(service.Return, nowDirProtoc, allProtoc),
}
Expand Down Expand Up @@ -204,12 +204,19 @@ func messageToResponse(message string, nowDirProtoc []parser.ProtocFileParser, a
return got
}

func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser, allProtoc map[string][]parser.ProtocFileParser) openapi.Parameters {
func messageToParameters(method string, message string, nowDirProtoc []parser.ProtocFileParser, allProtoc map[string][]parser.ProtocFileParser) openapi.Parameters {
protocMessage, pge := findMessage(message, nowDirProtoc, allProtoc)
got := openapi.Parameters{}
if protocMessage == nil {
return got
}
in := "query"
switch method {
case "http.Get":
default:
in = "formData"
}

for _, option := range protocMessage.Attr {
doc, isRequired := filterRequired(option.Doc)
doc = getTitle(doc)
Expand All @@ -221,7 +228,7 @@ func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser,
Description: doc,
Enum: nil,
Format: option.Ty,
In: "query",
In: in,
Required: isRequired,
Items: &openapi.Schema{
Description: doc,
Expand All @@ -237,7 +244,7 @@ func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser,
Name: option.Name,
Description: doc,
Type: "array",
In: "query",
In: in,
Required: isRequired,
Items: &openapi.Schema{
Ref: getRef(pge, option.Ty),
Expand All @@ -251,7 +258,7 @@ func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser,
} else if isProtoBaseType(option.Ty) {
attr := &openapi.Parameter{
Name: option.Name,
In: "query",
In: in,
Description: doc,
Type: getProtoToSwagger(option.Ty),
Format: option.Ty,
Expand All @@ -265,7 +272,7 @@ func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser,
Description: doc,
Type: getProtoToSwagger(option.Ty),
Format: option.Ty,
In: "query",
In: in,
Required: isRequired,
Schema: &openapi.Schema{
Type: "object",
Expand Down

0 comments on commit 92a294c

Please sign in to comment.