Skip to content

Commit

Permalink
修复table名称转Camel的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
xfali committed Dec 9, 2021
1 parent 1c92075 commit 9a8cbf8
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 97 deletions.
41 changes: 5 additions & 36 deletions pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

package common

import "strings"
import (
"github.com/xfali/gobatis-cmd/pkg/stringutils"
)

func Newline() string {
return "\n"
Expand All @@ -32,43 +34,10 @@ func Column2DynamicName(tableName, column string) string {

// snake string, XxYy to xx_yy , XxYY to xx_yy
func Camel2snake(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data))
return stringutils.Camel2snake(s)
}

// camel string, xx_yy to XxYy
func Snake2camel(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data)
return stringutils.Snake2camel(s)
}
6 changes: 4 additions & 2 deletions pkg/generator/gen_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/xfali/gobatis-cmd/pkg/config"
"github.com/xfali/gobatis-cmd/pkg/io"
"github.com/xfali/gobatis-cmd/pkg/mapping"
"path/filepath"
"strings"
"time"
)
Expand All @@ -36,8 +37,9 @@ func GenModel(config config.Config, tableName string, model []common.ModelInfo)
if fileName == "" {
fileName = strings.ToLower(tableName) + ".go"
}
exist := io.IsPathExists(modelDir + fileName)
modelFile, err := io.OpenAppend(modelDir + fileName)
dst := filepath.Join(modelDir, fileName)
exist := io.IsPathExists(dst)
modelFile, err := io.OpenAppend(dst)
if err == nil {
defer modelFile.Close()

Expand Down
3 changes: 2 additions & 1 deletion pkg/generator/gen_proxy_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/xfali/gobatis-cmd/pkg/common"
"github.com/xfali/gobatis-cmd/pkg/config"
"github.com/xfali/gobatis-cmd/pkg/io"
"path/filepath"
"strings"
"time"
)
Expand All @@ -22,7 +23,7 @@ func GenV2Proxy(config config.Config, tableName string, models []common.ModelInf
if !io.IsPathExists(mapperDir) {
io.Mkdir(mapperDir)
}
mapperFile, err := io.OpenAppend(mapperDir + strings.ToLower(tableName) + "_proxy.go")
mapperFile, err := io.OpenAppend(filepath.Join(mapperDir, strings.ToLower(tableName) + "_proxy.go"))
if err == nil {
defer mapperFile.Close()

Expand Down
25 changes: 13 additions & 12 deletions pkg/generator/gen_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/xfali/gobatis-cmd/pkg/config"
"github.com/xfali/gobatis-cmd/pkg/io"
"github.com/xfali/gobatis-cmd/pkg/mapping"
"path/filepath"
"strings"
"time"
)
Expand All @@ -22,11 +23,11 @@ func GenTemplate(config config.Config, tableName string, model []common.ModelInf
if config.Keyword {
common.SelectKeywordFormatter(config.Driver)
}
targetDir := config.Path + "template/"
targetDir := filepath.Join(config.Path, "template/")
if !io.IsPathExists(targetDir) {
io.Mkdir(targetDir)
}
targetFile, err := io.OpenAppend(targetDir + strings.ToLower(tableName) + "_mapper.tmpl")
targetFile, err := io.OpenAppend(filepath.Join(targetDir, strings.ToLower(tableName)+"_mapper.tmpl"))
if err == nil {
defer targetFile.Close()

Expand Down Expand Up @@ -70,19 +71,19 @@ func buildTmplMapper(builder *strings.Builder, config config.Config, tableName s
builder.WriteString(common.Newline())
builder.WriteString(common.Newline())

//select count
builder.WriteString(fmt.Sprintf(`{{define "select%sCount"}}`, modelName))
builder.WriteString(common.Newline())
//select count
builder.WriteString(fmt.Sprintf(`{{define "select%sCount"}}`, modelName))
builder.WriteString(common.Newline())

builder.WriteString(fmt.Sprintf(`SELECT COUNT(*) FROM %s`, tableName))
builder.WriteString(common.Newline())
builder.WriteString(fmt.Sprintf(`SELECT COUNT(*) FROM %s`, tableName))
builder.WriteString(common.Newline())

builder.WriteString(genTmplWhere(modelName, model))
builder.WriteString(common.Newline())
builder.WriteString(genTmplWhere(modelName, model))
builder.WriteString(common.Newline())

builder.WriteString(`{{end}}`)
builder.WriteString(common.Newline())
builder.WriteString(common.Newline())
builder.WriteString(`{{end}}`)
builder.WriteString(common.Newline())
builder.WriteString(common.Newline())

//insert
builder.WriteString(fmt.Sprintf(`{{define "insert%s"}}`, modelName))
Expand Down
9 changes: 5 additions & 4 deletions pkg/generator/gen_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"github.com/xfali/gobatis-cmd/pkg/config"
"github.com/xfali/gobatis-cmd/pkg/io"
"github.com/xfali/gobatis-cmd/pkg/mapping"
"strings"
"path/filepath"
"strings"
"time"
)

Expand All @@ -23,11 +24,11 @@ func GenXml(config config.Config, tableName string, model []common.ModelInfo) {
common.SelectKeywordFormatter(config.Driver)
}
if config.MapperFile == "xml" {
xmlDir := config.Path + "xml/"
xmlDir := filepath.Join(config.Path, "xml/")
if !io.IsPathExists(xmlDir) {
io.Mkdir(xmlDir)
}
xmlFile, err := io.OpenAppend(xmlDir + strings.ToLower(tableName) + "_mapper.xml")
xmlFile, err := io.OpenAppend(filepath.Join(xmlDir, strings.ToLower(tableName) + "_mapper.xml"))
if err == nil {
defer xmlFile.Close()

Expand All @@ -41,7 +42,7 @@ func GenXml(config config.Config, tableName string, model []common.ModelInfo) {
if !io.IsPathExists(xmlDir) {
io.Mkdir(xmlDir)
}
xmlFile, err := io.OpenAppend(xmlDir + strings.ToLower(tableName) + "_mapper.go")
xmlFile, err := io.OpenAppend(filepath.Join(xmlDir, strings.ToLower(tableName) + "_mapper.go"))
if err == nil {
defer xmlFile.Close()

Expand Down
102 changes: 102 additions & 0 deletions pkg/stringutils/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (C) 2020-2021, Xiongfa Li.
// @author xiongfa.li
// @version V1.0
// Description:

package stringutils

import (
"testing"
)

func TestSnake2Camel(t *testing.T) {
t.Run("compute_node", func(t *testing.T) {
s := Snake2camel("compute_node")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})
t.Run("COMPUTE_NODE", func(t *testing.T) {
s := Snake2camel("COMPUTE_NODE")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("COmpute_NODE", func(t *testing.T) {
s := Snake2camel("COmpute_NODE")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("COmuteNODE", func(t *testing.T) {
s := Snake2camel("COmuteNODE")
if s != "COmuteNODE" {
t.Fatal("expect COmuteNODE got: ", s)
}
})

t.Run("__compute_nOde", func(t *testing.T) {
s := Snake2camel("__compute_nOde")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("__compute__nOde", func(t *testing.T) {
s := Snake2camel("__compute__nOde")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("computeNode", func(t *testing.T) {
s := Snake2camel("computeNode")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("_computeNode_", func(t *testing.T) {
s := Snake2camel("_computeNode_")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("ComputeNode", func(t *testing.T) {
s := Snake2camel("ComputeNode")
if s != "ComputeNode" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("ComputeNode_detail", func(t *testing.T) {
s := Snake2camel("ComputeNode_detail")
if s != "ComputeNodeDetail" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("_computeNode_detail_", func(t *testing.T) {
s := Snake2camel("_computeNode_detail_")
if s != "ComputeNodeDetail" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("_compute_Node_detail_", func(t *testing.T) {
s := Snake2camel("_compute_Node_detail_")
if s != "ComputeNodeDetail" {
t.Fatal("expect ComputeNode got: ", s)
}
})

t.Run("_compute_node_detail_", func(t *testing.T) {
s := Snake2camel("_compute_node_detail_")
if s != "ComputeNodeDetail" {
t.Fatal("expect ComputeNode got: ", s)
}
})
}
104 changes: 104 additions & 0 deletions pkg/stringutils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (C) 2019, Xiongfa Li.
* All right reserved.
* @author xiongfa.li
* @version V1.0
* Description:
*/

package stringutils

import (
"bytes"
"strings"
)

// snake string, XxYy to xx_yy , XxYY to xx_yy
func Camel2snake(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data))
}

// camel string, xx_yy to XxYy
func Snake2camel(s string) string {
s = strings.Trim(s, "_")
if strings.Index(s, "_") != -1 {
return Snake2camel3(s)
} else {
d := []byte(s)
if len(d) > 0 {
if d[0] >= 'a' && d[0] <= 'z' {
d[0] -= 32
return string(d)
}
}
}
return s
}

func Snake2camel2(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data)
}

func Snake2camel3(s string) string {
strs := strings.Split(s, "_")
buf := bytes.Buffer{}
buf.Grow(len(s))
for _, v := range strs {
if v == "" {
continue
}
// 第一个字符必须大写
d := v[0]
if d >= 'a' && d <= 'z' {
d -= 32
}
buf.WriteByte(d)
low := false
for i := 1; i < len(v); i++ {
d := v[i]
// 中间夹杂小写
if d >= 'a' && d <= 'z' {
low = true
} else if d >= 'A' && d <= 'Z' && !low {
// 大写且中间没有经过小写,则表明是连续大写,则改写为小写
buf.WriteByte(d + 32)
continue
}
buf.WriteByte(d)
}
}
return buf.String()
}
Loading

0 comments on commit 9a8cbf8

Please sign in to comment.