Skip to content

Commit

Permalink
Merge pull request #26 from gobuffalo/expose-callback-interfaces
Browse files Browse the repository at this point in the history
expose the callback interfaces to make it clearer how they work
  • Loading branch information
markbates committed Mar 13, 2018
2 parents cb2f913 + 165f07e commit 8e87bee
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"golang.org/x/sync/errgroup"
)

type afterFindable interface {
// AfterFindable callback will be called after a record, or records,
// has been retrieved from the database.
type AfterFindable interface {
AfterFind(*Connection) error
}

func (m *Model) afterFind(c *Connection) error {
if x, ok := m.Value.(afterFindable); ok {
if x, ok := m.Value.(AfterFindable); ok {
if err := x.AfterFind(c); err != nil {
return errors.WithStack(err)
}
Expand All @@ -33,7 +35,7 @@ func (m *Model) afterFind(c *Connection) error {
wg.Go(func() error {
y := rv.Index(i)
y = y.Addr()
if x, ok := y.Interface().(afterFindable); ok {
if x, ok := y.Interface().(AfterFindable); ok {
return x.AfterFind(c)
}
return nil
Expand All @@ -44,89 +46,105 @@ func (m *Model) afterFind(c *Connection) error {
return wg.Wait()
}

type beforeSaveable interface {
// BeforeSaveable callback will be called before a record is
// either created or updated in the database.
type BeforeSaveable interface {
BeforeSave(*Connection) error
}

func (m *Model) beforeSave(c *Connection) error {
if x, ok := m.Value.(beforeSaveable); ok {
if x, ok := m.Value.(BeforeSaveable); ok {
return x.BeforeSave(c)
}
return nil
}

type beforeCreateable interface {
// BeforeCreateable callback will be called before a record is
// created in the database.
type BeforeCreateable interface {
BeforeCreate(*Connection) error
}

func (m *Model) beforeCreate(c *Connection) error {
if x, ok := m.Value.(beforeCreateable); ok {
if x, ok := m.Value.(BeforeCreateable); ok {
return x.BeforeCreate(c)
}
return nil
}

type beforeUpdateable interface {
// BeforeUpdateable callback will be called before a record is
// updated in the database.
type BeforeUpdateable interface {
BeforeUpdate(*Connection) error
}

func (m *Model) beforeUpdate(c *Connection) error {
if x, ok := m.Value.(beforeUpdateable); ok {
if x, ok := m.Value.(BeforeUpdateable); ok {
return x.BeforeUpdate(c)
}
return nil
}

type beforeDestroyable interface {
// BeforeDestroyable callback will be called before a record is
// destroyed in the database.
type BeforeDestroyable interface {
BeforeDestroy(*Connection) error
}

func (m *Model) beforeDestroy(c *Connection) error {
if x, ok := m.Value.(beforeDestroyable); ok {
if x, ok := m.Value.(BeforeDestroyable); ok {
return x.BeforeDestroy(c)
}
return nil
}

type afterDestroyable interface {
// AfterDestroyable callback will be called after a record is
// destroyed in the database.
type AfterDestroyable interface {
AfterDestroy(*Connection) error
}

func (m *Model) afterDestroy(c *Connection) error {
if x, ok := m.Value.(afterDestroyable); ok {
if x, ok := m.Value.(AfterDestroyable); ok {
return x.AfterDestroy(c)
}
return nil
}

type afterUpdateable interface {
// AfterUpdateable callback will be called after a record is
// updated in the database.
type AfterUpdateable interface {
AfterUpdate(*Connection) error
}

func (m *Model) afterUpdate(c *Connection) error {
if x, ok := m.Value.(afterUpdateable); ok {
if x, ok := m.Value.(AfterUpdateable); ok {
return x.AfterUpdate(c)
}
return nil
}

type afterCreateable interface {
// AfterCreateable callback will be called after a record is
// created in the database.
type AfterCreateable interface {
AfterCreate(*Connection) error
}

func (m *Model) afterCreate(c *Connection) error {
if x, ok := m.Value.(afterCreateable); ok {
if x, ok := m.Value.(AfterCreateable); ok {
return x.AfterCreate(c)
}
return nil
}

type afterSaveable interface {
// AfterSaveable callback will be called after a record is
// either created or updated in the database.
type AfterSaveable interface {
AfterSave(*Connection) error
}

func (m *Model) afterSave(c *Connection) error {
if x, ok := m.Value.(afterSaveable); ok {
if x, ok := m.Value.(AfterSaveable); ok {
return x.AfterSave(c)
}
return nil
Expand Down

0 comments on commit 8e87bee

Please sign in to comment.