-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc.go
58 lines (47 loc) · 1.05 KB
/
abc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package watcher
import (
"context"
"github.com/pubgo/x/jsonx"
"github.com/pubgo/x/typex"
"github.com/pubgo/xerror"
)
type Factory func(cfg typex.M) (Watcher, error)
type CallBack func(name string, event *Response) error
// Watcher ...
type Watcher interface {
Watch(ctx context.Context, key string, opts ...Opt) <-chan *Response
Name() string
}
type Opt func(*Opts)
type Opts struct{}
type Response struct {
Event string
Key string
Value []byte
Revision int64
}
func (t *Response) OnPut(fn func()) {
xerror.Panic(t.checkEventType())
if t.Event == "PUT" {
fn()
}
}
func (t *Response) OnDelete(fn func()) {
xerror.Panic(t.checkEventType())
if t.Event == "DELETE" {
fn()
}
}
func (t *Response) Decode(val interface{}) (gErr error) {
defer xerror.RespErr(&gErr)
var err = jsonx.Unmarshal(t.Value, val)
return xerror.WrapF(err, "input: %s, output: %#v", t.Value, val)
}
func (t *Response) checkEventType() error {
switch t.Event {
case "DELETE", "PUT":
return nil
default:
return xerror.Fmt("unknown type: %s", t.Event)
}
}