Skip to content

Commit

Permalink
migrate: Add CallbackRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Dec 3, 2020
1 parent fbe7bc4 commit 9dd79fc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions migrate/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package migrate

import (
"context"
"errors"

"github.com/scylladb/gocqlx/v2"
)
Expand All @@ -31,3 +32,30 @@ type CallbackFunc func(ctx context.Context, session gocqlx.Session, ev CallbackE
// Use this variable to register a global callback dispatching function.
// See CallbackFunc for details.
var Callback CallbackFunc

type nameEvent struct {
name string
event CallbackEvent
}

// CallbackRegister allows to register a handlers for an event type and a name.
// It dispatches calls to the registered handlers.
// If there is no handler registered for CallComment an error is returned.
type CallbackRegister map[nameEvent]CallbackFunc

// Add registers a callback handler.
func (r CallbackRegister) Add(ev CallbackEvent, name string, f CallbackFunc) {
r[nameEvent{name, ev}] = f
}

// Callback is CallbackFunc.
func (r CallbackRegister) Callback(ctx context.Context, session gocqlx.Session, ev CallbackEvent, name string) error {
f, ok := r[nameEvent{name, ev}]
if !ok {
if ev == CallComment {
return errors.New("missing handler")
}
return nil
}
return f(ctx, session, ev, name)
}

0 comments on commit 9dd79fc

Please sign in to comment.