mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-06-12 16:53:35 +00:00
Function update one now considers the protection key using the update operator
This commit is contained in:
@@ -3,6 +3,7 @@ package protection
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/modern-go/reflect2"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,6 +13,22 @@ type Key struct {
|
|||||||
V int64 `bson:"_v" json:"_v"`
|
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
|
// Restate creates a new protection key
|
||||||
func (k *Key) Restate() {
|
func (k *Key) Restate() {
|
||||||
k.X = primitive.NewObjectID()
|
k.X = primitive.NewObjectID()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/modern-go/reflect2"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
|
||||||
@@ -32,13 +33,15 @@ func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err er
|
|||||||
if !protected.X.IsZero() {
|
if !protected.X.IsZero() {
|
||||||
query.Push(composed, protected)
|
query.Push(composed, protected)
|
||||||
}
|
}
|
||||||
//updater = append(updater, primitive.M{
|
|
||||||
// "$set": primitive.M{
|
|
||||||
// "_x": primitive.NewObjectID(),
|
|
||||||
// "_v": time.Now().Unix(),
|
|
||||||
// },
|
|
||||||
//})
|
|
||||||
protected.Restate()
|
protected.Restate()
|
||||||
|
|
||||||
|
setCmd, _ := updaterDoc["$set"].(primitive.M)
|
||||||
|
if reflect2.IsNil(setCmd) {
|
||||||
|
setCmd = primitive.M{}
|
||||||
|
}
|
||||||
|
protected.PutToDocument(setCmd)
|
||||||
|
updaterDoc["$set"] = setCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
+4
-13
@@ -107,28 +107,19 @@ func applySkip(query *Query, filter interface{}) (ok bool) {
|
|||||||
|
|
||||||
func applyProtection(query *Query, filter interface{}) (ok bool) {
|
func applyProtection(query *Query, filter interface{}) (ok bool) {
|
||||||
|
|
||||||
var x *primitive.ObjectID
|
var keyDoc = primitive.M{}
|
||||||
var v *int64
|
|
||||||
|
|
||||||
switch filter := filter.(type) {
|
switch filter := filter.(type) {
|
||||||
case protection.Key:
|
case protection.Key:
|
||||||
x = &filter.X
|
filter.PutToDocument(keyDoc)
|
||||||
v = &filter.V
|
|
||||||
case *protection.Key:
|
case *protection.Key:
|
||||||
x = &filter.X
|
filter.PutToDocument(keyDoc)
|
||||||
v = &filter.V
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if x.IsZero() {
|
query.And(keyDoc)
|
||||||
query.And(primitive.M{"_x": primitive.M{"$exists": false}})
|
|
||||||
query.And(primitive.M{"_v": primitive.M{"$exists": false}})
|
|
||||||
} else {
|
|
||||||
query.And(primitive.M{"_x": *x})
|
|
||||||
query.And(primitive.M{"_v": *v})
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user