Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.

Commit 514eb8b

Browse files
committed
add namespace toString method
1 parent 01650ec commit 514eb8b

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

model/event.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package model
22

33
import (
4+
"fmt"
5+
46
"go.mongodb.org/mongo-driver/bson"
57
"go.mongodb.org/mongo-driver/bson/primitive"
68
)
@@ -28,3 +30,7 @@ type Namespace struct {
2830
Database string `bson:"db,omitempty" json:"db,omitempty"`
2931
Collection string `bson:"coll,omitempty" json:"coll,omitempty"`
3032
}
33+
34+
func (ns Namespace) String() string {
35+
return fmt.Sprintf("%s.%s", ns.Database, ns.Collection)
36+
}

operator/mongo/mongo.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,24 @@ func (m *mongoOperator) Replace(e model.ChangeEvent) error {
4646
}
4747

4848
func (m *mongoOperator) Update(e model.ChangeEvent) error {
49-
if e.FullDocument == nil {
50-
update := bson.M{}
49+
update := bson.M{}
5150

52-
if len(e.UpdateDescription.UpdatedFields) != 0 {
53-
update["$set"] = e.UpdateDescription.UpdatedFields
54-
}
51+
if len(e.UpdateDescription.UpdatedFields) != 0 {
52+
update["$set"] = e.UpdateDescription.UpdatedFields
53+
}
5554

56-
if len(e.UpdateDescription.RemovedFields) != 0 {
57-
unset := bson.M{}
58-
for _, field := range e.UpdateDescription.RemovedFields {
59-
unset[field] = ""
60-
}
61-
update["$unset"] = unset
55+
if len(e.UpdateDescription.RemovedFields) != 0 {
56+
unset := bson.M{}
57+
for _, field := range e.UpdateDescription.RemovedFields {
58+
unset[field] = ""
6259
}
63-
64-
_, err := m.Database(e.Namespace.Database).
65-
Collection(e.Namespace.Collection).
66-
UpdateOne(context.Background(), e.DocumentKey, update)
67-
return err
60+
update["$unset"] = unset
6861
}
69-
return m.Replace(e)
62+
63+
_, err := m.Database(e.Namespace.Database).
64+
Collection(e.Namespace.Collection).
65+
UpdateOne(context.Background(), e.DocumentKey, update)
66+
return err
7067
}
7168

7269
func (m *mongoOperator) Drop(e model.ChangeEvent) error {
@@ -76,8 +73,8 @@ func (m *mongoOperator) Drop(e model.ChangeEvent) error {
7673
}
7774

7875
func (m *mongoOperator) Rename(e model.ChangeEvent) error {
79-
from := e.Namespace.Database + "." + e.Namespace.Collection
80-
to := e.To.Database + "." + e.To.Collection
76+
from := e.Namespace.String()
77+
to := e.To.String()
8178

8279
result := m.Database("admin").
8380
RunCommand(

0 commit comments

Comments
 (0)