Skip to content

Commit

Permalink
修复截断长字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
azhai committed May 18, 2022
1 parent 57d97bc commit 8172aeb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
3 changes: 1 addition & 2 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *Reverser) ReverseTables(pkgName string, tableSchemas []*schemas.Table)
tables := make(map[string]*schemas.Table)
tablePrefixes := r.target.GetTablePrefixes()
for _, table := range tableSchemas {
// fmt.Println(pkgName, table.Name)
fmt.Println("-", pkgName, table.Name)
tableName := table.Name
if len(tablePrefixes) > 0 {
table.Name = trimAnyPrefix(table.Name, tablePrefixes)
Expand Down Expand Up @@ -290,7 +290,6 @@ func ApplyDirMixins(currDir string, verbose bool) error {
files, _ := rewrite.FindFiles(currDir, ".go")
var err error
for filename := range files {
// fmt.Println("mixin --", filename)
_err := rewrite.ParseAndMixinFile(cps, filename, verbose)
if _err != nil {
err = _err
Expand Down
3 changes: 2 additions & 1 deletion language.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/azhai/xgen/rewrite"
"github.com/azhai/xgen/utils"

"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
Expand Down Expand Up @@ -258,7 +259,7 @@ func tagXorm(table *schemas.Table, col *schemas.Column) string {
}
}

if comm := rewrite.ReduceComment(col.Comment); comm != "" {
if comm := utils.TruncateText(col.Comment, 50); comm != "" {
res = append(res, fmt.Sprintf("comment('%s')", comm))
}

Expand Down
3 changes: 2 additions & 1 deletion rewrite/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strings"

"github.com/azhai/xgen/utils"
"github.com/azhai/xgen/utils/enums"
)

Expand Down Expand Up @@ -186,7 +187,7 @@ func (s *ModelSummary) ParseFields(cp *CodeParser, node *DeclNode) int {
}
comm := cp.GetComment(f.Comment, true)
if len(comm) > 0 {
code += " //" + ReduceComment(comm)
code += " //" + utils.TruncateText(comm, 50)
}
s.FieldLines[i] = code
}
Expand Down
12 changes: 1 addition & 11 deletions rewrite/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,14 @@ import (
"strings"
)

// 获取多个标识的名称列表
// GetNameList 获取多个标识的名称列表
func GetNameList(ids []*ast.Ident) (names []string) {
for _, id := range ids {
names = append(names, id.Name)
}
return
}

// ReduceComment
func ReduceComment(comm string) string {
if comm == "" || len(comm) > 200 {
return ""
}
comm = strings.ReplaceAll(comm, "\r", "")
comm = strings.ReplaceAll(comm, "\n", " ")
return comm
}

// TrimComment 去掉注释两边的空白
func TrimComment(c string) string {
c = strings.TrimSpace(c)
Expand Down
12 changes: 12 additions & 0 deletions utils/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ func ReduceSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}

// TruncateText 截断长文本
func TruncateText(s string, size int) string {
if s == "" {
return ""
}
text := []rune(s) // 防止将中文从中间截断
if size > 0 && len(text) > size {
s = string(text[:size-3]) + "..."
}
return ReduceSpaces(s)
}

// 用:号连接两个部分,如果后一部分也存在的话
func ConcatWith(master, slave string) string {
if slave != "" {
Expand Down

0 comments on commit 8172aeb

Please sign in to comment.