Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
move err check to object
Browse files Browse the repository at this point in the history
  • Loading branch information
devilsm authored and devilsm committed Feb 2, 2018
1 parent c129d73 commit d01f57e
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 162 deletions.
9 changes: 0 additions & 9 deletions cmd/fn.code.go
Expand Up @@ -41,7 +41,6 @@ func GenerateCode() {
confTpls := map[string]bool{
"orm": true,
}
utilTpls := make(map[string]bool)
i := 0
for _, yaml := range yamls {
objs, err := parser.ReadYaml(packageName, yaml)
Expand All @@ -60,7 +59,6 @@ func GenerateCode() {
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
utilTpls[db] = true
}
goto GeneratePoint
}
Expand All @@ -71,7 +69,6 @@ func GenerateCode() {
metaObjs[obj.Name] = obj
for _, db := range obj.Dbs {
confTpls[db] = true
utilTpls[db] = true
}
}
}
Expand All @@ -91,11 +88,5 @@ GeneratePoint:
panic(err.Error())
}
}
for db := range utilTpls {
err = fs.ExecuteUtilTemplate(outputDir, db, packageName)
if err != nil {
panic(err.Error())
}
}

}
5 changes: 5 additions & 0 deletions example/model/gen.object.blog.go
Expand Up @@ -473,6 +473,11 @@ func (m *_BlogDBMgr) Fetch(pk PrimaryKey) (*Blog, error) {
return nil, fmt.Errorf("Blog fetch record not found")
}

// err not found check
func (m *_BlogDBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_BlogDBMgr) FetchByPrimaryKey(id int32, userId int32) (*Blog, error) {
obj := BlogMgr.NewBlog()
Expand Down
5 changes: 5 additions & 0 deletions example/model/gen.object.office.go
Expand Up @@ -399,6 +399,11 @@ func (m *_OfficeDBMgr) Fetch(pk PrimaryKey) (*Office, error) {
return nil, fmt.Errorf("Office fetch record not found")
}

// err not found check
func (m *_OfficeDBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_OfficeDBMgr) FetchByPrimaryKey(officeId int32) (*Office, error) {
obj := OfficeMgr.NewOffice()
Expand Down
5 changes: 5 additions & 0 deletions example/model/gen.object.user.blogs.go
Expand Up @@ -376,6 +376,11 @@ func (m *_UserBlogsDBMgr) Fetch(pk PrimaryKey) (*UserBlogs, error) {
return nil, fmt.Errorf("UserBlogs fetch record not found")
}

// err not found check
func (m *_UserBlogsDBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_UserBlogsDBMgr) FetchByPrimaryKey(userId int32, blogId int32) (*UserBlogs, error) {
obj := UserBlogsMgr.NewUserBlogs()
Expand Down
5 changes: 5 additions & 0 deletions example/model/gen.object.user.go
Expand Up @@ -683,6 +683,11 @@ func (m *_UserDBMgr) Fetch(pk PrimaryKey) (*User, error) {
return nil, fmt.Errorf("User fetch record not found")
}

// err not found check
func (m *_UserDBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_UserDBMgr) FetchByPrimaryKey(id int32) (*User, error) {
obj := UserMgr.NewUser()
Expand Down
1 change: 0 additions & 1 deletion example/model/gen.util.elastic.go

This file was deleted.

1 change: 0 additions & 1 deletion example/model/gen.util.mssql.go

This file was deleted.

10 changes: 0 additions & 10 deletions example/model/gen.util.mysql.go

This file was deleted.

1 change: 0 additions & 1 deletion example/model/gen.util.redis.go

This file was deleted.

5 changes: 5 additions & 0 deletions example/model/gen.view.user.base.info.go
Expand Up @@ -474,6 +474,11 @@ func (m *_UserBaseInfoDBMgr) Fetch(pk PrimaryKey) (*UserBaseInfo, error) {
return nil, fmt.Errorf("UserBaseInfo fetch record not found")
}

// err not found check
func (m *_UserBaseInfoDBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_UserBaseInfoDBMgr) FetchByPrimaryKey(id int32) (*UserBaseInfo, error) {
obj := UserBaseInfoMgr.NewUserBaseInfo()
Expand Down
3 changes: 0 additions & 3 deletions example/model/model_test.go
Expand Up @@ -153,9 +153,6 @@ var _ = Describe("redis-orm.mysql", func() {
_, err = mgr.Fetch(user.GetPrimaryKey())
Ω(err).Should(HaveOccurred())

_, err = mgr.FetchByPrimaryKey(user.Id)
Ω(IsErrNotFound(err)).To(BeTrue())

//! save

user.HeadUrl = "ccc.png"
Expand Down
23 changes: 0 additions & 23 deletions fs/template.go
Expand Up @@ -86,25 +86,6 @@ func ExecuteConfigTemplate(output, db string, packageName string) error {
return nil
}

func ExecuteUtilTemplate(output, db, packageName string) error {
filename := filepath.Join(output, strings.Join([]string{"gen", "util", db, "go"}, "."))
fd, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fd.Close()
if err := RedisOrmTemplate.ExecuteTemplate(fd, strings.Join([]string{"util", db}, "."), map[string]interface{}{
"GoPackage": packageName,
}); err != nil {
return err
}

oscmd := exec.Command("gofmt", "-w", filename)
oscmd.Run()
return nil

}

func init() {
funcMap := template.FuncMap{
"add": Add,
Expand All @@ -121,10 +102,6 @@ func init() {
"tpl/conf.mongo.gogo",
"tpl/conf.mssql.gogo",
"tpl/conf.mysql.gogo",
"tpl/util.mysql.gogo",
"tpl/util.mssql.gogo",
"tpl/util.elastic.gogo",
"tpl/util.redis.gogo",
"tpl/conf.orm.gogo",
"tpl/conf.redis.gogo",
"tpl/object.db.gogo",
Expand Down
92 changes: 0 additions & 92 deletions tpl/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tpl/object.db.read.gogo
Expand Up @@ -146,6 +146,11 @@ func (m *_{{$obj.Name}}DBMgr) Fetch(pk PrimaryKey) (*{{$obj.Name}}, error) {
return nil, fmt.Errorf("{{$obj.Name}} fetch record not found")
}

// err not found check
func (m *_{{$obj.Name}}DBMgr) IsErrNotFound(err error) bool {
return strings.Contains(err.Error(), "not found") || err == sql.ErrNoRows
}

// primary key
func (m *_{{$obj.Name}}DBMgr) FetchByPrimaryKey({{$primary.GetFuncParam}}) (*{{$obj.Name}}, error) {
obj := {{$obj.Name}}Mgr.New{{$obj.Name}}()
Expand Down
3 changes: 0 additions & 3 deletions tpl/util.elastic.gogo

This file was deleted.

2 changes: 0 additions & 2 deletions tpl/util.mssql.gogo

This file was deleted.

13 changes: 0 additions & 13 deletions tpl/util.mysql.gogo

This file was deleted.

4 changes: 0 additions & 4 deletions tpl/util.redis.gogo

This file was deleted.

0 comments on commit d01f57e

Please sign in to comment.