Function update one now considers the protection key using the update operator

This commit is contained in:
2020-11-18 22:33:50 +01:00
parent c1ad7aea7d
commit 86397885e2
3 changed files with 30 additions and 19 deletions
+17
View File
@@ -3,6 +3,7 @@ package protection
import (
"time"
"github.com/modern-go/reflect2"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@@ -12,6 +13,22 @@ type Key struct {
V int64 `bson:"_v" json:"_v"`
}
// PutToDocument extends the doc with protection key values
func (k *Key) PutToDocument(doc primitive.M) {
if reflect2.IsNil(doc) {
return
}
if k.X.IsZero() {
doc["_x"] = primitive.M{"$exists": false}
doc["_v"] = primitive.M{"$exists": false}
} else {
doc["_x"] = k.X
doc["_v"] = k.V
}
}
// Restate creates a new protection key
func (k *Key) Restate() {
k.X = primitive.NewObjectID()