goctl added
This commit is contained in:
33
tools/goctl/model/mongomodel/gen/genmethod.go
Normal file
33
tools/goctl/model/mongomodel/gen/genmethod.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"zero/tools/goctl/model/mongomodel/utils"
|
||||
)
|
||||
|
||||
func genMethodTemplate(funcDesc FunctionDesc, needCache bool) (template string) {
|
||||
var tmp string
|
||||
switch funcDesc.Type {
|
||||
case functionTypeGet:
|
||||
if needCache {
|
||||
tmp = getTemplate
|
||||
} else {
|
||||
tmp = noCacheGetTemplate
|
||||
}
|
||||
case functionTypeFind:
|
||||
tmp = findTemplate
|
||||
case functionTypeSet:
|
||||
if needCache {
|
||||
tmp = ""
|
||||
} else {
|
||||
tmp = noCacheSetFieldtemplate
|
||||
}
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
tmp = strings.ReplaceAll(tmp, "{{.Name}}", funcDesc.FieldName)
|
||||
tmp = strings.ReplaceAll(tmp, "{{.name}}", utils.UpperCamelToLower(funcDesc.FieldName))
|
||||
tmp = strings.ReplaceAll(tmp, "{{.type}}", funcDesc.FieldType)
|
||||
return tmp
|
||||
}
|
||||
156
tools/goctl/model/mongomodel/gen/genmongomodel.go
Normal file
156
tools/goctl/model/mongomodel/gen/genmongomodel.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"zero/tools/goctl/api/spec"
|
||||
"zero/tools/goctl/api/util"
|
||||
"zero/tools/goctl/model/mongomodel/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
functionTypeGet = "get" //GetByField return single model
|
||||
functionTypeFind = "find" // findByField return many model
|
||||
functionTypeSet = "set" // SetField only set specified field
|
||||
|
||||
TagOperate = "o" //字段函数的tag
|
||||
TagComment = "c" //字段注释的tag
|
||||
)
|
||||
|
||||
type (
|
||||
FunctionDesc struct {
|
||||
Type string // get,find,set
|
||||
FieldName string // 字段名字 eg:Age
|
||||
FieldType string // 字段类型 eg: string,int64 等
|
||||
}
|
||||
)
|
||||
|
||||
func GenMongoModel(goFilePath string, needCache bool) error {
|
||||
structs, imports, err := utils.ParseGoFile(goFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(structs) != 1 {
|
||||
return fmt.Errorf("only 1 struct should be provided")
|
||||
}
|
||||
structStr, err := genStructs(structs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fp, err := util.ClearAndOpenFile(goFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
var myTemplate string
|
||||
if needCache {
|
||||
myTemplate = cacheTemplate
|
||||
} else {
|
||||
myTemplate = noCacheTemplate
|
||||
}
|
||||
structName := getStructName(structs)
|
||||
functionList := getFunctionList(structs)
|
||||
|
||||
for _, fun := range functionList {
|
||||
funTmp := genMethodTemplate(fun, needCache)
|
||||
if funTmp == "" {
|
||||
continue
|
||||
}
|
||||
myTemplate += "\n"
|
||||
myTemplate += funTmp
|
||||
myTemplate += "\n"
|
||||
}
|
||||
|
||||
t := template.Must(template.New("mongoTemplate").Parse(myTemplate))
|
||||
return t.Execute(fp, map[string]string{
|
||||
"modelName": structName,
|
||||
"importArray": getImports(imports, needCache),
|
||||
"modelFields": structStr,
|
||||
})
|
||||
}
|
||||
|
||||
func getFunctionList(structs []utils.Struct) []FunctionDesc {
|
||||
var list []FunctionDesc
|
||||
for _, field := range structs[0].Fields {
|
||||
tagMap := parseTag(field.Tag)
|
||||
if fun, ok := tagMap[TagOperate]; ok {
|
||||
funList := strings.Split(fun, ",")
|
||||
for _, o := range funList {
|
||||
var f FunctionDesc
|
||||
f.FieldType = field.Type
|
||||
f.FieldName = field.Name
|
||||
f.Type = o
|
||||
list = append(list, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func getStructName(structs []utils.Struct) string {
|
||||
for _, structItem := range structs {
|
||||
return structItem.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func genStructs(structs []utils.Struct) (string, error) {
|
||||
if len(structs) > 1 {
|
||||
return "", fmt.Errorf("input .go file must only one struct")
|
||||
}
|
||||
|
||||
modelFields := `Id bson.ObjectId ` + quotationMark + `bson:"_id" json:"id,omitempty"` + quotationMark + "\n\t"
|
||||
for _, structItem := range structs {
|
||||
for _, field := range structItem.Fields {
|
||||
modelFields += getFieldLine(field)
|
||||
}
|
||||
}
|
||||
modelFields += "\t" + `CreateTime time.Time ` + quotationMark + `json:"createTime,omitempty" bson:"createTime"` + quotationMark + "\n\t"
|
||||
modelFields += "\t" + `UpdateTime time.Time ` + quotationMark + `json:"updateTime,omitempty" bson:"updateTime"` + quotationMark
|
||||
return modelFields, nil
|
||||
}
|
||||
|
||||
func getFieldLine(member spec.Member) string {
|
||||
if member.Name == "CreateTime" || member.Name == "UpdateTime" || member.Name == "Id" {
|
||||
return ""
|
||||
}
|
||||
jsonName := utils.UpperCamelToLower(member.Name)
|
||||
result := "\t" + member.Name + ` ` + member.Type + ` ` + quotationMark + `json:"` + jsonName + `,omitempty"` + ` bson:"` + jsonName + `"` + quotationMark
|
||||
tagMap := parseTag(member.Tag)
|
||||
if comment, ok := tagMap[TagComment]; ok {
|
||||
result += ` //` + comment + "\n\t"
|
||||
} else {
|
||||
result += "\n\t"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// tag like `o:"find,get,update" c:"姓名"`
|
||||
func parseTag(tag string) map[string]string {
|
||||
var result = make(map[string]string, 0)
|
||||
tags := strings.Split(tag, " ")
|
||||
for _, kv := range tags {
|
||||
temp := strings.Split(kv, ":")
|
||||
if len(temp) > 1 {
|
||||
key := strings.ReplaceAll(strings.ReplaceAll(temp[0], "\"", ""), "`", "")
|
||||
value := strings.ReplaceAll(strings.ReplaceAll(temp[1], "\"", ""), "`", "")
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getImports(imports []string, needCache bool) string {
|
||||
|
||||
importStr := strings.Join(imports, "\n\t")
|
||||
importStr += "\"errors\"\n\t"
|
||||
importStr += "\"time\"\n\t"
|
||||
importStr += "\n\t\"zero/core/stores/cache\"\n\t"
|
||||
importStr += "\"zero/core/stores/mongoc\"\n\t"
|
||||
importStr += "\n\t\"github.com/globalsign/mgo/bson\""
|
||||
return importStr
|
||||
}
|
||||
67
tools/goctl/model/mongomodel/gen/genmongomodelbynetwork.go
Normal file
67
tools/goctl/model/mongomodel/gen/genmongomodelbynetwork.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"zero/tools/goctl/model/mongomodel/utils"
|
||||
)
|
||||
|
||||
func GenMongoModelByNetwork(input string, needCache bool) (string, error) {
|
||||
if strings.TrimSpace(input) == "" {
|
||||
return "", errors.New("struct不能为空")
|
||||
}
|
||||
if strings.Index(strings.TrimSpace(input), "type") != 0 {
|
||||
input = "type " + input
|
||||
}
|
||||
|
||||
if strings.Index(strings.TrimSpace(input), "package") != 0 {
|
||||
input = "package model\r\n" + input
|
||||
}
|
||||
|
||||
structs, imports, err := utils.ParseGoFileByNetwork(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(structs) != 1 {
|
||||
return "", fmt.Errorf("only 1 struct should be provided")
|
||||
}
|
||||
structStr, err := genStructs(structs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var myTemplate string
|
||||
if needCache {
|
||||
myTemplate = cacheTemplate
|
||||
} else {
|
||||
myTemplate = noCacheTemplate
|
||||
}
|
||||
structName := getStructName(structs)
|
||||
functionList := getFunctionList(structs)
|
||||
|
||||
for _, fun := range functionList {
|
||||
funTmp := genMethodTemplate(fun, needCache)
|
||||
if funTmp == "" {
|
||||
continue
|
||||
}
|
||||
myTemplate += "\n"
|
||||
myTemplate += funTmp
|
||||
myTemplate += "\n"
|
||||
}
|
||||
|
||||
t := template.Must(template.New("mongoTemplate").Parse(myTemplate))
|
||||
var result bytes.Buffer
|
||||
err = t.Execute(&result, map[string]string{
|
||||
"modelName": structName,
|
||||
"importArray": getImports(imports, needCache),
|
||||
"modelFields": structStr,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.String(), nil
|
||||
}
|
||||
238
tools/goctl/model/mongomodel/gen/templatemodel.go
Normal file
238
tools/goctl/model/mongomodel/gen/templatemodel.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package gen
|
||||
|
||||
const (
|
||||
quotationMark = "`"
|
||||
//templates that do not use caching
|
||||
noCacheTemplate = `package model
|
||||
|
||||
import (
|
||||
{{.importArray}}
|
||||
)
|
||||
|
||||
var ErrNotFound = mongoc.ErrNotFound
|
||||
|
||||
type (
|
||||
{{.modelName}}Model struct {
|
||||
*mongoc.Model
|
||||
}
|
||||
|
||||
{{.modelName}} struct {
|
||||
{{.modelFields}}
|
||||
}
|
||||
)
|
||||
|
||||
func New{{.modelName}}Model(url, database, collection string, c cache.CacheConf, opts ...cache.Option) *{{.modelName}}Model {
|
||||
return &{{.modelName}}Model{mongoc.MustNewModel(url, database, collection, c, opts...)}
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) FindOne(id string) (*{{.modelName}}, error) {
|
||||
session, err := m.Model.TakeSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer m.Model.PutSession(session)
|
||||
|
||||
var result {{.modelName}}
|
||||
err = m.GetCollection(session).FindOneIdNoCache(&result,bson.ObjectIdHex(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Delete(id string) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
return m.GetCollection(session).RemoveIdNoCache(bson.ObjectIdHex(id))
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Insert(data *{{.modelName}}) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
return m.GetCollection(session).Insert(data)
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Update(data *{{.modelName}}) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
data.UpdateTime = time.Now()
|
||||
return m.GetCollection(session).UpdateIdNoCache(data.Id, data)
|
||||
}
|
||||
`
|
||||
|
||||
//use cache template
|
||||
cacheTemplate = `package model
|
||||
|
||||
import (
|
||||
{{.importArray}}
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
|
||||
const (
|
||||
Prefix{{.modelName}}CacheKey = "#{{.modelName}}#cache" //todo please modify this prefix
|
||||
)
|
||||
|
||||
type (
|
||||
{{.modelName}}Model struct {
|
||||
*mongoc.Model
|
||||
}
|
||||
|
||||
{{.modelName}} struct {
|
||||
{{.modelFields}}
|
||||
}
|
||||
)
|
||||
|
||||
func New{{.modelName}}Model(url, database, collection string, c cache.CacheConf, opts ...cache.Option) *{{.modelName}}Model {
|
||||
return &{{.modelName}}Model{mongoc.MustNewModel(url, database, collection, c, opts...)}
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) FindOne(id string) (*{{.modelName}}, error) {
|
||||
key := Prefix{{.modelName}}CacheKey + id
|
||||
session, err := m.Model.TakeSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer m.Model.PutSession(session)
|
||||
|
||||
var result {{.modelName}}
|
||||
err = m.GetCollection(session).FindOneId(&result, key, bson.ObjectIdHex(id))
|
||||
switch err {
|
||||
case nil:
|
||||
return &result, nil
|
||||
case mongoc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Delete(id string) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
key := Prefix{{.modelName}}CacheKey + id
|
||||
return m.GetCollection(session).RemoveId(bson.ObjectIdHex(id), key)
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Insert(data *{{.modelName}}) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
return m.GetCollection(session).Insert(data)
|
||||
}
|
||||
|
||||
func (m *{{.modelName}}Model) Update(data *{{.modelName}}) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
data.UpdateTime = time.Now()
|
||||
key := Prefix{{.modelName}}CacheKey + data.Id.Hex()
|
||||
return m.GetCollection(session).UpdateId(data.Id, data, key)
|
||||
}
|
||||
`
|
||||
cacheSetFieldtemplate = `func (m *{{.modelName}}Model) Set{{.Name}}(id string, {{.name}} {{.type}}) error {
|
||||
_, err := m.cache.Del(Prefix{{.modelName}}CacheKey + id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
update := bson.M{"$set": bson.M{"{{.name}}": {{.name}}, "updateTime": time.Now()}}
|
||||
return m.GetCollection(session).UpdateId(bson.ObjectIdHex(id), update)
|
||||
}`
|
||||
|
||||
noCacheSetFieldtemplate = `func (m *{{.modelName}}Model) Set{{.Name}}(id string, {{.name}} {{.type}}) error {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
update := bson.M{"$set": bson.M{"{{.name}}": {{.name}}, "updateTime": time.Now()}}
|
||||
return m.GetCollection(session).UpdateId(bson.ObjectIdHex(id), update)
|
||||
}`
|
||||
|
||||
noCacheGetTemplate = `func (m *{{.modelName}}Model) GetBy{{.Name}}({{.name}} {{.type}}) (*{{.modelName}},error) {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
var result {{.modelName}}
|
||||
query := bson.M{"{{.name}}":{{.name}}}
|
||||
err = m.GetCollection(session).FindOneNoCache(&result, query)
|
||||
if err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return nil,ErrNotFound
|
||||
}
|
||||
return nil,err
|
||||
}
|
||||
return &result,nil
|
||||
}`
|
||||
// GetByField return single model
|
||||
getTemplate = `func (m *{{.modelName}}Model) GetBy{{.Name}}({{.name}} {{.type}}) (*{{.modelName}},error) {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
var result {{.modelName}}
|
||||
query := bson.M{"{{.name}}":{{.name}}}
|
||||
key := getCachePrimaryKeyBy{{.Name}}({{.name}})
|
||||
err = m.GetCollection(session).FindOne(&result,key,query)
|
||||
if err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return nil,ErrNotFound
|
||||
}
|
||||
return nil,err
|
||||
}
|
||||
return &result,nil
|
||||
}
|
||||
|
||||
func getCachePrimaryKeyBy{{.Name}}({{.name}} {{.type}}) string {
|
||||
return "" //todo 请补全这里
|
||||
}
|
||||
`
|
||||
|
||||
findTemplate = `func (m *{{.modelName}}Model) FindBy{{.Name}}({{.name}} string) ([]{{.modelName}},error) {
|
||||
session, err := m.TakeSession()
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
defer m.PutSession(session)
|
||||
|
||||
query := bson.M{"{{.name}}":{{.name}}}
|
||||
var result []{{.modelName}}
|
||||
err = m.GetCollection(session).FindAllNoCache(&result,query)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
return result,nil
|
||||
}`
|
||||
)
|
||||
Reference in New Issue
Block a user