-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
69 lines (57 loc) · 1.73 KB
/
message.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
59
60
61
62
63
64
65
66
67
68
69
package cdc
import (
"time"
"github.com/elastic/go-elasticsearch/v7"
"github.com/Trendyol/go-pq-cdc/pq/message/format"
)
type Message struct {
ElasticsearchClient *elasticsearch.Client
EventTime time.Time
TableName string
TableNamespace string
OldData map[string]any
NewData map[string]any
Type MessageType
}
func NewInsertMessage(esClient *elasticsearch.Client, m *format.Insert) Message {
return Message{
ElasticsearchClient: esClient,
EventTime: m.MessageTime,
TableName: m.TableName,
TableNamespace: m.TableNamespace,
OldData: nil,
NewData: m.Decoded,
Type: InsertMessage,
}
}
func NewUpdateMessage(esClient *elasticsearch.Client, m *format.Update) Message {
return Message{
ElasticsearchClient: esClient,
EventTime: m.MessageTime,
TableName: m.TableName,
TableNamespace: m.TableNamespace,
OldData: m.OldDecoded,
NewData: m.NewDecoded,
Type: UpdateMessage,
}
}
func NewDeleteMessage(esClient *elasticsearch.Client, m *format.Delete) Message {
return Message{
ElasticsearchClient: esClient,
EventTime: m.MessageTime,
TableName: m.TableName,
TableNamespace: m.TableNamespace,
OldData: m.OldDecoded,
NewData: nil,
Type: DeleteMessage,
}
}
type MessageType string
const (
InsertMessage MessageType = "INSERT"
UpdateMessage MessageType = "UPDATE"
DeleteMessage MessageType = "DELETE"
)
func (m MessageType) IsInsert() bool { return m == InsertMessage }
func (m MessageType) IsUpdate() bool { return m == UpdateMessage }
func (m MessageType) IsDelete() bool { return m == DeleteMessage }