Skip to content

Commit

Permalink
spring templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ixre committed Nov 17, 2021
1 parent 22d0ab2 commit fe85760
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ generated_code
*-bin
tto_*.conf
*.iml
mac-tto
2 changes: 1 addition & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// BuildVersion 版本号
const BuildVersion = "0.4.3"
const BuildVersion = "0.4.5"

// ReleaseCodeHome 代码页
const ReleaseCodeHome = "https://github.com/ixre/tto"
Expand Down
7 changes: 3 additions & 4 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ var (
dbPrefix = "mm_"
connString = "root:@tcp(127.0.0.1:3306)/baozhang?charset=utf8"
genDir = "generated_code/"
tplDir ="./templates/java/spring/kt"

tplDir = "./templates/java/spring/kt"
)

// 生成数据库所有的代码文件
Expand All @@ -26,7 +25,7 @@ func TestGenAll(t *testing.T) {

// 初始化生成器
conn, _ := db.NewConnector(driver, connString, nil, false)
ds,_ := orm.NewDialectSession(driver,conn.Raw())
ds, _ := orm.NewDialectSession(driver, conn.Raw())

// 生成自定义代码
opt := &Options{
Expand All @@ -35,7 +34,7 @@ func TestGenAll(t *testing.T) {
OutputDir: genDir,
ExcludePatterns: []string{"grid_list.html"},
}
dg := DBCodeGenerator(ds.Driver(),opt)
dg := DBCodeGenerator(ds.Driver(), opt)
list, err := ds.TablesByPrefix(dbName, "", dbPrefix)
if err != nil {
println("[ tto][ error]: not found tables", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions lang/dart.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import "github.com/ixre/gof/db/orm"
*/

var _ Lang = new(dart)
type dart struct{

type dart struct {
}

func (d dart) ParseType(typeId int) string {
Expand Down Expand Up @@ -49,11 +49,11 @@ func (d dart) DefaultValue(typeId int) string {
return "\"\""
case orm.TypeBoolean:
return "false"
case orm.TypeInt16,orm.TypeInt32:
case orm.TypeInt16, orm.TypeInt32:
return "0"
case orm.TypeInt64:
return "BigInt.from(0)"
case orm.TypeFloat32,orm.TypeFloat64, orm.TypeDecimal:
case orm.TypeFloat32, orm.TypeFloat64, orm.TypeDecimal:
return "0.0"
case orm.TypeDateTime:
return "DateTime.now()"
Expand Down
3 changes: 2 additions & 1 deletion lang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var _ Lang = new(GoLang)

var pkgRegex = regexp.MustCompile("/(com|net|io|cn|org|info)/")

type GoLang struct {
}

Expand All @@ -26,7 +27,7 @@ func (g GoLang) SqlMapType(typeId int, len int) string {
}

func (g GoLang) PkgPath(pkg string) string {
return pkgRegex.ReplaceAllString(pkg,".$1/")
return pkgRegex.ReplaceAllString(pkg, ".$1/")
}

func (g GoLang) PkgName(pkg string) string {
Expand Down
31 changes: 29 additions & 2 deletions lang/java.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package lang

/**
* Copyright (C) 2007-2020 56X.NET,All rights reserved.
*
Expand All @@ -13,14 +14,13 @@ package lang
find output/java -name "*.java" | xargs sed -i 's/ int / Integer /g' && \
find output/java -name "*.java" | xargs sed -i 's/ long / Long /g' && \
find output/java -name "*.java" | xargs sed -i 's/ float / Float /g'
*/
*/

import (
"fmt"
"github.com/ixre/gof/db/orm"
)


type JavaLang struct {
}

Expand All @@ -43,6 +43,9 @@ func (j JavaLang) DefaultValue(typeId int) string {
return JavaValues(typeId)
}

func (j JavaLang) ParsePkType(typeId int) string {
return javaPkTypes(typeId)
}

var _ Lang = new(JavaLang)

Expand Down Expand Up @@ -70,6 +73,30 @@ func JavaTypes(typeId int) string {
return fmt.Sprintf("Unknown type id:%d", typeId)
}

func javaPkTypes(typeId int) string {
switch typeId {
case orm.TypeBoolean:
return "Boolean"
case orm.TypeInt64:
return "Long"
case orm.TypeFloat32:
return "Float"
case orm.TypeFloat64:
return "Double"
case orm.TypeInt16, orm.TypeInt32:
return "Integer"
case orm.TypeString:
return "String"
case orm.TypeDecimal:
return "BigDecimal"
case orm.TypeDateTime:
return "Date"
case orm.TypeBytes:
return "Byte[]"
}
return fmt.Sprintf("Unknown type id:%d", typeId)
}

func JavaValues(typeId int) string {
switch typeId {
case orm.TypeBoolean:
Expand Down
2 changes: 1 addition & 1 deletion lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var langMap = map[string]Lang{
"typescript": &Typescript{},
"protobuf": &Protobuf{},
"thrift": &Thrift{},
"dart":&dart{},
"dart": &dart{},
}

func Get(n string) Lang {
Expand Down
4 changes: 2 additions & 2 deletions lang/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ import "testing"
func TestPkgStyleLikeGo(t *testing.T) {
pkg := "github/com/ixre/go2o/core"
//pkg = PkgStyleLikeGo(pkg)
pkg = pkgRegex.ReplaceAllString(pkg,".$1/")
pkg = pkgRegex.ReplaceAllString(pkg, ".$1/")
t.Log(pkg)
}
}
19 changes: 11 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"github.com/pelletier/go-toml"
)



// global registry
var g *RegistryReader

Expand Down Expand Up @@ -62,16 +60,21 @@ func GetRegistry() *RegistryReader {
func initRegistry(r *Registry) *RegistryReader {
mp := map[string]interface{}{}
keys := make([]string, 0)
if n := r.tree.Get("global"); n != nil {
initByPrefix(r, "global", mp, &keys)
initByPrefix(r, "custom", mp, &keys)
return &RegistryReader{
Data: mp,
Keys: keys,
}
}

func initByPrefix(r *Registry, s string, mp map[string]interface{}, keys *[]string) {
if n := r.tree.Get(s); n != nil {
if tree := n.(*toml.Tree); tree != nil {
for _, k := range tree.Keys() {
mp[k] = tree.Get(k)
keys = append(keys, k)
*keys = append(*keys, k)
}
}
}
return &RegistryReader{
Data: mp,
Keys: keys,
}
}
32 changes: 16 additions & 16 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import (
// 获取表结构,userMeta 是否使用用户的元数据
func parseTable(ordinal int, tb *orm.Table, shortUpper bool, userMeta bool) *Table {
n := &Table{
Ordinal: ordinal,
Name: tb.Name,
Prefix: prefix(tb.Name),
Title: title(tb.Name, shortUpper),
Ordinal: ordinal,
Name: tb.Name,
Prefix: prefix(tb.Name),
Title: title(tb.Name, shortUpper),
ShortTitle: shortTitle(tb.Name),
Comment: tb.Comment,
Engine: tb.Engine,
Schema: tb.Schema,
Charset: tb.Charset,
Raw: tb,
Pk: "id",
PkProp: "Id",
PkType: orm.TypeInt32,
Columns: make([]*Column, len(tb.Columns)),
Comment: tb.Comment,
Engine: tb.Engine,
Schema: tb.Schema,
Charset: tb.Charset,
Raw: tb,
Pk: "id",
PkProp: "Id",
PkType: orm.TypeInt32,
Columns: make([]*Column, len(tb.Columns)),
}
if len(n.Comment) == 0 {
n.Comment = n.Title
Expand Down Expand Up @@ -162,15 +162,15 @@ func smartField(c *Column) *config.ColumnMeta {
func smartElement(c *Column) (string, map[string]string) {
name := c.Name
len := c.Length
if strings.HasPrefix(name, "is_"){
if strings.HasPrefix(name, "is_") {
return "checkbox", map[string]string{"是": "1", "否": "0"}
}
if strings.HasSuffix(name, "_time"){
if strings.HasSuffix(name, "_time") {
return "time", map[string]string{}
}
if strings.HasSuffix(name, "state") ||
strings.HasSuffix(name, "status") ||
strings.HasSuffix(name,"enabled"){
strings.HasSuffix(name, "enabled") {
return "radio", map[string]string{}
}
if strings.HasSuffix(name, "_date") {
Expand Down
46 changes: 30 additions & 16 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
Expand All @@ -38,6 +39,8 @@ const (
TIME = "time"
// 版本
VERSION = "version"
// 当前用户
USER = "user"
)

type (
Expand Down Expand Up @@ -127,17 +130,24 @@ func (s *sessionImpl) init() Session {
// predefine default vars
s.Var(BASE_URL, "")
s.Var(BASE_PATH, "")
s.Var(ENTITY_SUFFIX,"Entity")
s.Var(ENTITY_SUFFIX, "Entity")
// load global registry
rd := GetRegistry()
for _, k := range rd.Keys {
s.Var(k, rd.Data[k])
}
// put system vars
s.Package("com/your/pkg")
s.Var("db", s.driver)
s.Var(TIME, time.Now().Format("2006/01/02 15:04:05"))
s.Var(VERSION, BuildVersion)
// setting user variables
if usr, err := user.Current(); err == nil {
s.Var(USER, usr.Username)
} else {
s.Var(USER, "")
}
s.Var("db", s.driver)
s.Var("year", time.Now().Format("2006"))
return s
}

Expand Down Expand Up @@ -232,7 +242,7 @@ func (s *sessionImpl) GenerateCodeByTables(tables []*Table, tpl *CodeTemplate) s
}
groups := s.reduceGroup(tbs)
mp := map[string]interface{}{
"groups":groups,
"groups": groups,
"tables": tbs,
"global": s.codeVars,
}
Expand Down Expand Up @@ -407,11 +417,15 @@ func (s *sessionImpl) reduceGroup(tables []*Table) map[string][]*Table {
return groups
}

// 输出到文件
func (s *sessionImpl) flushToFile(tpl *CodeTemplate, tb *Table, path string, output string, opt *Options) {
// 获取文件的路径和名称
dstPath, _ := s.predefineTargetPath(tpl, tb)
if dstPath == "" {
dstPath = s.defaultTargetPath(path, tb)
}
fileName := dstPath[strings.LastIndexAny(dstPath, "/\\")+1:]
output = strings.Replace(output, "$file_name$", fileName, 1) // 替换文件名占位符
savedPath := filepath.Clean(opt.OutputDir + "/" + dstPath)
if err := SaveFile(output, savedPath); err != nil {
println(fmt.Sprintf("[ tto][ error]: save file failed! %s ,template:%s", err.Error(), tpl.FilePath()))
Expand Down Expand Up @@ -502,20 +516,20 @@ func (s *sessionImpl) copyTable(table *Table, lowerProp bool) *Table {
return s
}
dst := &Table{
Ordinal: table.Ordinal,
Name: table.Name,
Prefix: table.Prefix,
Title: table.Title,
Ordinal: table.Ordinal,
Name: table.Name,
Prefix: table.Prefix,
Title: table.Title,
ShortTitle: shortTitle(table.Name),
Comment: table.Comment,
Engine: table.Engine,
Schema: table.Schema,
Charset: table.Charset,
Raw: table.Raw,
Pk: table.Pk,
PkProp: prop(table.PkProp),
PkType: table.PkType,
Columns: make([]*Column, len(table.Columns)),
Comment: table.Comment,
Engine: table.Engine,
Schema: table.Schema,
Charset: table.Charset,
Raw: table.Raw,
Pk: table.Pk,
PkProp: prop(table.PkProp),
PkType: table.PkType,
Columns: make([]*Column, len(table.Columns)),
}
for i, v := range table.Columns {
dst.Columns[i] = &Column{
Expand Down
Loading

0 comments on commit fe85760

Please sign in to comment.