Skip to content

Commit

Permalink
Merge pull request #12 from funvit/feat/iter-pass-fn-err
Browse files Browse the repository at this point in the history
feat: iter* now pass fn error if any.
  • Loading branch information
funvit authored Oct 18, 2021
2 parents b16721b + 83e89eb commit b65f08a
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 87 deletions.
43 changes: 30 additions & 13 deletions cmd/goqu-crud-gen/get_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getTpl = `
func (s *{{ .Repo.Name }}) iter(
ctx context.Context,
filter goqu.Expression,
f func(m {{ .Model.Name }}, stop func()),
fn func(m {{ .Model.Name }}) error,
opt ...Option,
) error {
Expand Down Expand Up @@ -56,19 +56,24 @@ func (s *{{ .Repo.Name }}) iter(
return fmt.Errorf("row scan error: %w", err)
}
f(m, func() { sigCtxCancel() })
err = fn(m)
if err != nil {
sigCtxCancel()
_ = rows.Close()
return fmt.Errorf("fn call: %w", err)
}
}
return nil
}
// iterWithOrder iterates other select with specified filter(s).
// iterWithOrder iterates other select with specified filter(s) and order.
//
// Can be used in your custom query methods.
func (s *{{ .Repo.Name }}) iterWithOrder(
ctx context.Context,
filter goqu.Expression,
f func(m {{ .Model.Name }}, stop func()),
fn func(m {{ .Model.Name }}) error,
order exp.OrderedExpression,
opt ...Option,
) error {
Expand Down Expand Up @@ -120,7 +125,12 @@ func (s *{{ .Repo.Name }}) iterWithOrder(
return fmt.Errorf("row scan error: %w", err)
}
f(m, func() { sigCtxCancel() })
err = fn(m)
if err != nil {
sigCtxCancel()
_ = rows.Close()
return fmt.Errorf("fn call: %w", err)
}
}
return nil
Expand All @@ -132,7 +142,7 @@ func (s *{{ .Repo.Name }}) iterWithOrder(
func (s *{{ .Repo.Name }}) iterPrimaryKeys(
ctx context.Context,
filter goqu.Expression,
f func(pk interface{}, stop func()),
fn func(pk interface{}) error,
opt ...Option,
) error {
Expand Down Expand Up @@ -180,7 +190,12 @@ func (s *{{ .Repo.Name }}) iterPrimaryKeys(
return fmt.Errorf("row scan error: %w", err)
}
f(pk, func() { sigCtxCancel() })
err = fn(pk)
if err != nil {
sigCtxCancel()
_ = rows.Close()
return fmt.Errorf("fn call: %w", err)
}
}
return nil
Expand All @@ -191,13 +206,13 @@ func (s *{{ .Repo.Name }}) iterPrimaryKeys(
// Can be used in your custom query methods, for example in All.
//
// See also: iter.
func (s *{{ .Repo.Name }}) each(ctx context.Context, f func(m {{ .Model.Name }})) error {
func (s *{{ .Repo.Name }}) each(ctx context.Context, fn func(m {{ .Model.Name }}) error) error {
return s.iter(
ctx,
nil,
func(m {{ .Model.Name }}, _ func()) {
f(m)
func(m {{ .Model.Name }}) error {
return fn(m)
},
)
}
Expand All @@ -211,9 +226,10 @@ func (s *{{ .Repo.Name }}) {{"Get"|CRUD}}(ctx context.Context, id {{.Model.GetPr
err := s.iter(
ctx,
s.f.PK().Eq(id),
func(m {{ .Model.Name }}, stop func()) {
func(m {{ .Model.Name }}) error {
// note: expected to be called once.
r = &m
stop()
return nil
},
opt...,
)
Expand All @@ -230,8 +246,9 @@ func (s *{{ .Repo.Name }}) {{"GetManySlice"|CRUD}}(ctx context.Context, ids []{{
err := s.iter(
ctx,
s.f.PK().In(ids),
func(m {{ .Model.Name }}, _ func()) {
func(m {{ .Model.Name }}) error {
items = append(items, m)
return nil
},
opt...,
)
Expand Down
31 changes: 21 additions & 10 deletions examples/example1/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"os"
"time"

"example1/model"
Expand All @@ -16,21 +17,31 @@ const (
table = "user"
)

var (
logInfo = log.New(os.Stdout, "INF ", log.LstdFlags)
logErr = log.New(os.Stderr, "ERR ", log.LstdFlags)
)

func main() {
fmt.Println("Example1")

repo := model.NewUserRepo(dsn)
err := repo.Connect(3 * time.Second)
if err != nil {
log.Fatalf("repo connect error: %s", err)
logErr.Fatalf("repo connect error: %s", err)
}

// migrate
err = migrateUp(dsn, table)
if err != nil {
log.Fatalln(err)
logErr.Fatalln(err)
}
defer migrateDown(dsn, table)
defer func() {
err = migrateDown(dsn, table)
if err != nil {
logErr.Fatalln("migrate down error:", err)
}
}()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand All @@ -48,9 +59,9 @@ func main() {
return repo.Create(ctx, &u)
})
if err != nil {
log.Fatalln("Create error:", err)
logErr.Fatalln("Create error:", err)
}
log.Printf("user record created: %+v", u)
logInfo.Printf("user record created: %+v", u)

userId = u.Id
}
Expand All @@ -63,13 +74,13 @@ func main() {
return err
})
if err != nil {
log.Fatalln("Get error:", err)
logErr.Fatalln("Get error:", err)
}
if u == nil {
log.Fatalln("user not exists")
logErr.Fatalln("user not exists")
}

log.Printf("repo Get user result: %+v", u)
logInfo.Printf("repo Get user result: %+v", u)
}

fmt.Println("End.")
Expand All @@ -80,7 +91,7 @@ func migrateUp(dsn, table string) error {
if err != nil {
return fmt.Errorf("connect error: %w", err)
}
defer c.Close()
defer func() { _ = c.Close() }()

_, err = c.Exec(fmt.Sprintf(`
CREATE TABLE %s
Expand All @@ -106,7 +117,7 @@ func migrateDown(dsn, table string) error {
if err != nil {
return fmt.Errorf("connect error: %w", err)
}
defer c.Close()
defer func() { _ = c.Close() }()

_, err = c.Exec(fmt.Sprintf(`DROP TABLE %s`, table))
if err != nil {
Expand Down
43 changes: 30 additions & 13 deletions examples/example1/model/user_repo.go

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

Loading

0 comments on commit b65f08a

Please sign in to comment.