Compare commits

...

5 Commits

Author SHA1 Message Date
kevin
55c9c3f3dd replace clickhouse driver to the official one 2020-09-03 16:58:31 +08:00
kevin
8dd93d59a0 refactor code 2020-09-03 14:00:09 +08:00
Keson
3a4e1cbb33 fix bug: miss time import (#36)
* add execute files

* add protoc-osx

* add rpc generation

* add rpc generation

* add: rpc template generation

* optimize gomod cache

* add README.md

* format error

* reactor templatex.go

* update project.go & README.md

* fix bug: miss time import
2020-09-03 13:57:28 +08:00
kevin
d1129e3974 refactor 2020-09-03 10:15:14 +08:00
Leonard Wang
1e85f74fd8 fix shorturl example code (#35) 2020-09-03 08:34:11 +08:00
15 changed files with 80 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
package clickhouse
import (
_ "github.com/kshvakov/clickhouse"
_ "github.com/ClickHouse/clickhouse-go"
"github.com/tal-tech/go-zero/core/stores/sqlx"
)

View File

@@ -10,14 +10,16 @@ import (
)
type ExpandLogic struct {
ctx context.Context
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic {
return ExpandLogic{
ctx: ctx,
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

View File

@@ -10,14 +10,16 @@ import (
)
type ShortenLogic struct {
ctx context.Context
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLogic {
return ShortenLogic{
ctx: ctx,
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

View File

@@ -10,16 +10,16 @@ import (
)
type ExpandLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandLogic {
return &ExpandLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}

View File

@@ -12,16 +12,16 @@ import (
)
type ShortenLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenLogic {
return &ShortenLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}

4
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/tal-tech/go-zero
go 1.13
require (
github.com/ClickHouse/clickhouse-go v1.4.3
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6 // indirect
github.com/alicebob/miniredis v2.5.0+incompatible
@@ -29,7 +30,6 @@ require (
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/kshvakov/clickhouse v1.3.11
github.com/lib/pq v1.3.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-colorable v0.1.6 // indirect
@@ -56,7 +56,7 @@ require (
golang.org/x/tools v0.0.0-20200410132612-ae9902aceb98 // indirect
google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f // indirect
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.25.0
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.28
gopkg.in/yaml.v2 v2.2.8
honnef.co/go/tools v0.0.1-2020.1.4 // indirect

8
go.sum
View File

@@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ClickHouse/clickhouse-go v1.4.3 h1:iAFMa2UrQdR5bHJ2/yaSLffZkxpcOYQMCUuKeNXGdqc=
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1MRDJM=
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/StackExchange/wmi v0.0.0-20170410192909-ea383cf3ba6e/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
@@ -74,6 +76,7 @@ github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U=
github.com/go-redis/redis v6.15.7+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
@@ -143,6 +146,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -171,8 +175,7 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kshvakov/clickhouse v1.3.11 h1:dtzTJY0fCA+MWkLyuKZaNPkmSwdX4gh8+Klic9NB1Lw=
github.com/kshvakov/clickhouse v1.3.11/go.mod h1:/SVBAcqF3u7rxQ9sTWCZwf8jzzvxiZGeQvtmSF2BBEc=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
@@ -192,6 +195,7 @@ github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+tw
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

View File

@@ -20,16 +20,16 @@ import (
)
type {{.logic}} struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} {
return {{.logic}}{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
@@ -57,6 +57,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
if !ok {
return fmt.Errorf("missing handler annotation for %q", route.Path)
}
handler = strings.TrimSuffix(handler, "handler")
handler = strings.TrimSuffix(handler, "Handler")
filename := strings.ToLower(handler)
@@ -65,6 +66,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
if err != nil {
return err
}
if !created {
return nil
}
@@ -74,11 +76,11 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
if err != nil {
return err
}
imports := genLogicImports(route, parentPkg)
responseString := ""
returnString := ""
requestString := ""
imports := genLogicImports(route, parentPkg)
var responseString string
var returnString string
var requestString string
if len(route.ResponseType.Name) > 0 {
resp := strings.Title(route.ResponseType.Name)
responseString = "(*types." + resp + ", error)"
@@ -104,6 +106,7 @@ func genLogicByRoute(dir string, group spec.Group, route spec.Route) error {
if err != nil {
return err
}
formatCode := formatCode(buffer.String())
_, err = fp.WriteString(formatCode)
return err

View File

@@ -9,7 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
)
// struct匹配
// struct match
const typeRegex = `(?m)(?m)(^ *type\s+[a-zA-Z][a-zA-Z0-9_-]+\s+(((struct)\s*?\{[\w\W]*?[^\{]\})|([a-zA-Z][a-zA-Z0-9_-]+)))|(^ *type\s*?\([\w\W]+\}\s*\))`
var (

View File

@@ -22,7 +22,7 @@ func genDelete(table Table, withCache bool) (string, error) {
}
var containsIndexCache = false
for _, item := range table.Fields {
if item.IsKey {
if item.IsKey && !item.IsPrimaryKey {
containsIndexCache = true
break
}

View File

@@ -126,7 +126,12 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
if err != nil {
return "", err
}
importsCode := genImports(withCache)
importsCode, err := genImports(withCache, in.ContainsTime())
if err != nil {
return "", err
}
var table Table
table.Table = in
table.CacheKey = m
@@ -135,36 +140,44 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
if err != nil {
return "", err
}
typesCode, err := genTypes(table, withCache)
if err != nil {
return "", err
}
newCode, err := genNew(table, withCache)
if err != nil {
return "", err
}
insertCode, err := genInsert(table, withCache)
if err != nil {
return "", err
}
var findCode = make([]string, 0)
findOneCode, err := genFindOne(table, withCache)
if err != nil {
return "", err
}
findOneByFieldCode, err := genFineOneByField(table, withCache)
if err != nil {
return "", err
}
findCode = append(findCode, findOneCode, findOneByFieldCode)
updateCode, err := genUpdate(table, withCache)
if err != nil {
return "", err
}
deleteCode, err := genDelete(table, withCache)
if err != nil {
return "", err
}
output, err := t.Execute(map[string]interface{}{
"imports": importsCode,
"vars": varsCode,
@@ -178,5 +191,6 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
if err != nil {
return "", err
}
return output.String(), nil
}

View File

@@ -2,12 +2,25 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
)
func genImports(withCache bool) string {
func genImports(withCache, timeImport bool) (string, error) {
if withCache {
return template.Imports
buffer, err := util.With("import").Parse(template.Imports).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {
return "", err
}
return buffer.String(), nil
} else {
return template.ImportsNoCache
buffer, err := util.With("import").Parse(template.ImportsNoCache).Execute(map[string]interface{}{
"time": timeImport,
})
if err != nil {
return "", err
}
return buffer.String(), nil
}
}

View File

@@ -16,16 +16,20 @@ const (
spatial
)
const timeImport = "time.Time"
type (
Table struct {
Name stringx.String
PrimaryKey Primary
Fields []Field
}
Primary struct {
Field
AutoIncrement bool
}
Field struct {
Name stringx.String
DataBaseType string
@@ -34,6 +38,7 @@ type (
IsPrimaryKey bool
Comment string
}
KeyType int
)
@@ -135,3 +140,12 @@ func Parse(ddl string) (*Table, error) {
Fields: fields,
}, nil
}
func (t *Table) ContainsTime() bool {
for _, item := range t.Fields {
if item.DataType == timeImport {
return true
}
}
return false
}

View File

@@ -2,7 +2,7 @@ package template
var Delete = `
func (m *{{.upperStartCamelObject}}Model) Delete({{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
{{if .withCache}}{{if .containsIndexCache}}_, err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne({{.lowerStartCamelPrimaryKey}})
if err!=nil{
return err
}{{end}}

View File

@@ -5,6 +5,7 @@ var (
"database/sql"
"fmt"
"strings"
{{if .time}}"time"{{end}}
"github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/sqlc"
@@ -16,7 +17,7 @@ var (
ImportsNoCache = `import (
"database/sql"
"strings"
"time"
{{if .time}}"time"{{end}}
"github.com/tal-tech/go-zero/core/stores/sqlc"
"github.com/tal-tech/go-zero/core/stores/sqlx"