添加ddl转markdown后端接口
parent
214b3203e7
commit
64d02c04e5
|
@ -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
|
||||
}
|
||||
|
|
2
main.go
2
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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue