From 64d02c04e55ac6685e5f493fc2446f92f3d93aab Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sun, 28 Feb 2021 18:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ddl=E8=BD=ACmarkdown=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/openapi.go | 24 ++++++++++++++++++++++++ main.go | 2 ++ utils/helper.go | 20 ++++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/controller/openapi.go b/controller/openapi.go index c4a463b..e9775e5 100644 --- a/controller/openapi.go +++ b/controller/openapi.go @@ -63,3 +63,27 @@ func (this *OpenApiController) DDL2ORM(c *gin.Context) { resp.Msg = "OK" resp.Status = 0 } + + +func (this *OpenApiController) DDL2Markdown(c *gin.Context) { + var req ReqDDL + resp := RespBase{ + Msg: "err", + } + defer func() { + c.JSON(200, resp) + }() + + e := c.Bind(&req) + if e != nil { + log.Println(e.Error()) + resp.Msg = "ParaErr" + return + } + tbname, fields := utils.DDL2Field(req.Data) + log.Print(tbname, fields) + gocode := utils.CreateMarkdownTable(tbname,fields) + resp.Data = gocode + resp.Msg = "OK" + resp.Status = 0 +} diff --git a/main.go b/main.go index 6d149a3..336ebff 100644 --- a/main.go +++ b/main.go @@ -189,6 +189,8 @@ func main() { { openapi.POST("/diff") openapi.POST("/ddl2orm",openapiController.DDL2ORM) + openapi.POST("/ddl2markdown",openapiController.DDL2Markdown) + } e := r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e { diff --git a/utils/helper.go b/utils/helper.go index 2b8eb13..e9e16df 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -59,7 +59,7 @@ func CreateGoStruct(name string,doc []FieldDesc) string{ if len(doc) == 0{ return "" } - ret := fmt.Sprintf("type %s struct{\r\n",toCamelString(name)) + ret := fmt.Sprintf("golang gorm tag struct 代码:\r\ntype %s struct{\r\n",toCamelString(name)) for _,v := range doc{ filed := "\t\t" camelField := toCamelString(v.FiledName) @@ -72,6 +72,22 @@ func CreateGoStruct(name string,doc []FieldDesc) string{ return ret } + +func CreateMarkdownTable(name string,doc []FieldDesc) string{ + if name == "" { + return "" + } + if len(doc) == 0{ + return "" + } + ret := fmt.Sprintf("数据表 %s markdown 表格字段说明: \r\n",toCamelString(name)) + ret += "|字段名称|字段类型|字段说明|是否主键| \r\n|-|-|-|-| \r\n" + for _,v := range doc{ + ret += fmt.Sprintf("|%s|%s||%t|\r\n",v.FiledName,v.Type,v.Primary) + } + return ret +} + func TrimUnWanted(in string) string { ret := strings.ReplaceAll(in, "\t","") ret = strings.ReplaceAll(ret, "\n","") @@ -97,7 +113,7 @@ func DDL2Field(ddl string) (string, []FieldDesc) { sTableName := strings.Trim(sTbNameUnTrim, " ") sTbDesc := ddl[indexFirstParentheses+1 : indexLastParentheses] - fields := strings.Split(sTbDesc, ",") + fields := strings.Split(sTbDesc, "\n") primarykey := []string{} for _, v := range fields {