Browse Source

支持字段小驼峰

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

49
main.go

@ -11,6 +11,7 @@ import (
"sync"
"text/template"
"time"
"unicode"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
@ -45,6 +46,7 @@ type options struct {
Package string
GormType bool
ForceTableName bool
Camel bool // 是否json字段驼峰
}
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
func WithGormType() Option {
return func(o *options) {
@ -215,6 +223,29 @@ type tmplField struct {
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) {
importPath := make([]string, 0, 1)
data := tmplData{
@ -309,7 +340,11 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (string, []string, error)
tags = append(tags, "gorm", gormTag.String())
if opt.JsonTag {
tags = append(tags, "json", colName)
if opt.Camel {
tags = append(tags, "json", Case2Camel(colName))
} else {
tags = append(tags, "json", colName)
}
}
if opt.ZhTag {
@ -479,16 +514,22 @@ func main() {
r.POST("/sql", func(c *gin.Context) {
var req struct {
Content string `json:"content"`
Camel bool `json:"camel"`
}
err := c.BindJSON(&req)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
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,
WithGormType(),
WithJsonTag(),
WithZhTag(),
opts...,
)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())

5
paser_test.go

@ -1,6 +1,7 @@
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@ -23,3 +24,7 @@ func TestParseSql(t *testing.T) {
}
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-button type="primary" @click="genCode()">生成代码</el-button>
<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>
<vue-json-editor
v-model="schema"
@ -109,6 +117,7 @@ export default {
components: { vueJsonEditor },
data() {
return {
isUnderScoreCase: true, // 线
schemaKey: "19878878",
renderKey: 1,
activeName: "",
@ -445,7 +454,7 @@ export default {
oReq.open("POST", "/sql", false); //
oReq.setRequestHeader("Content-type", "application/json");
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; //
return result;
},

Loading…
Cancel
Save