Browse Source

支持字段小驼峰

main
cobb 4 years ago
parent
commit
be49ecf489
  1. 47
      main.go
  2. 5
      paser_test.go
  3. 11
      src/components/HelloWorld.vue

47
main.go

@ -11,6 +11,7 @@ import (
"sync" "sync"
"text/template" "text/template"
"time" "time"
"unicode"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -45,6 +46,7 @@ type options struct {
Package string Package string
GormType bool GormType bool
ForceTableName bool ForceTableName bool
Camel bool // 是否json字段驼峰
} }
var defaultOptions = options{ var defaultOptions = options{
@ -106,6 +108,12 @@ func WithPackage(pkg string) Option {
} }
} }
func WithCamel() Option {
return func(o *options) {
o.Camel = true
}
}
// WithGormType will write type in gorm tag // WithGormType will write type in gorm tag
func WithGormType() Option { func WithGormType() Option {
return func(o *options) { return func(o *options) {
@ -215,6 +223,29 @@ type tmplField struct {
Comment string Comment string
} }
// 下划线写法转为小驼峰写法
func Case2Camel(name string) string {
name = strings.Replace(name, "_", " ", -1)
name = strings.Title(name)
return Lcfirst(strings.Replace(name, " ", "", -1))
}
// 首字母大写
func Ucfirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// 首字母小写
func Lcfirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
func makeCode(stmt *ast.CreateTableStmt, opt options) (string, []string, error) { func makeCode(stmt *ast.CreateTableStmt, opt options) (string, []string, error) {
importPath := make([]string, 0, 1) importPath := make([]string, 0, 1)
data := tmplData{ data := tmplData{
@ -309,8 +340,12 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (string, []string, error)
tags = append(tags, "gorm", gormTag.String()) tags = append(tags, "gorm", gormTag.String())
if opt.JsonTag { if opt.JsonTag {
if opt.Camel {
tags = append(tags, "json", Case2Camel(colName))
} else {
tags = append(tags, "json", colName) tags = append(tags, "json", colName)
} }
}
if opt.ZhTag { if opt.ZhTag {
tags = append(tags, "zh-cn", field.Comment) tags = append(tags, "zh-cn", field.Comment)
@ -479,16 +514,22 @@ func main() {
r.POST("/sql", func(c *gin.Context) { r.POST("/sql", func(c *gin.Context) {
var req struct { var req struct {
Content string `json:"content"` Content string `json:"content"`
Camel bool `json:"camel"`
} }
err := c.BindJSON(&req) err := c.BindJSON(&req)
if err != nil { if err != nil {
c.String(http.StatusBadRequest, err.Error()) c.String(http.StatusBadRequest, err.Error())
return return
} }
opts := []Option{}
opts = append(opts, WithGormType())
opts = append(opts, WithJsonTag())
opts = append(opts, WithZhTag())
if req.Camel {
opts = append(opts, WithCamel())
}
res, err := ParseSqlFormat(req.Content, res, err := ParseSqlFormat(req.Content,
WithGormType(),
WithJsonTag(),
WithZhTag(),
opts...,
) )
if err != nil { if err != nil {
c.String(http.StatusInternalServerError, err.Error()) c.String(http.StatusInternalServerError, err.Error())

5
paser_test.go

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -23,3 +24,7 @@ func TestParseSql(t *testing.T) {
} }
t.Log(data.ImportPath) t.Log(data.ImportPath)
} }
func Test2Camel(t *testing.T) {
fmt.Println("data:", Case2Camel("w_z_f"))
}

11
src/components/HelloWorld.vue

@ -24,6 +24,14 @@
</el-row> </el-row>
<el-button type="primary" @click="genCode()">生成代码</el-button> <el-button type="primary" @click="genCode()">生成代码</el-button>
<div style="margin: 20px 0"></div> <div style="margin: 20px 0"></div>
是否JSON字段下划线命名
<el-switch
v-model="isUnderScoreCase"
active-color="#13ce66"
inactive-color="#ff4949"
>
</el-switch>
<div style="margin: 20px 0"></div>
<el-row> <el-row>
<vue-json-editor <vue-json-editor
v-model="schema" v-model="schema"
@ -109,6 +117,7 @@ export default {
components: { vueJsonEditor }, components: { vueJsonEditor },
data() { data() {
return { return {
isUnderScoreCase: true, // 线
schemaKey: "19878878", schemaKey: "19878878",
renderKey: 1, renderKey: 1,
activeName: "", activeName: "",
@ -445,7 +454,7 @@ export default {
oReq.open("POST", "/sql", false); // oReq.open("POST", "/sql", false); //
oReq.setRequestHeader("Content-type", "application/json"); oReq.setRequestHeader("Content-type", "application/json");
console.log(this.sqlinput); console.log(this.sqlinput);
oReq.send(JSON.stringify({ content: this.sqlinput })); //JSON
oReq.send(JSON.stringify({ content: this.sqlinput, camel: !this.isUnderScoreCase })); //JSON
var result = oReq.responseText; // var result = oReq.responseText; //
return result; return result;
}, },

Loading…
Cancel
Save