Set modified via bson marshal and update operators
parent
710e20d6e0
commit
9338cf86c9
|
|
@ -1,7 +1,16 @@
|
||||||
package entity
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Name = "_id"
|
Name = "_id"
|
||||||
|
JSONName = "name"
|
||||||
Relationship = "relationship"
|
Relationship = "relationship"
|
||||||
Type = "type"
|
Type = "type"
|
||||||
Title = "title"
|
Title = "title"
|
||||||
|
|
@ -33,3 +42,22 @@ func (o One) Peers() []string {
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o One) MarshalBSON() ([]byte, error) {
|
||||||
|
if fmt.Sprint(o) != fmt.Sprint(o.Query()) {
|
||||||
|
o.Modified = time.Now().UnixNano()
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := bson.M{}
|
||||||
|
if err := json.Unmarshal(b, &m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if name, ok := m[JSONName]; ok {
|
||||||
|
m[Name] = name
|
||||||
|
delete(m, JSONName)
|
||||||
|
}
|
||||||
|
return bson.Marshal(m)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package entity
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOne(t *testing.T) {
|
func TestOne(t *testing.T) {
|
||||||
|
|
@ -15,3 +17,46 @@ func TestOne(t *testing.T) {
|
||||||
t.Error(want, q)
|
t.Error(want, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOneMarshalBSON(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
sameAsQuery bool
|
||||||
|
one One
|
||||||
|
}{
|
||||||
|
"query no modified change": {
|
||||||
|
sameAsQuery: true,
|
||||||
|
one: (One{Name: "hello", Type: "world", Modified: 1}).Query(),
|
||||||
|
},
|
||||||
|
"modified changes": {
|
||||||
|
one: (One{Name: "hello", Type: "world", Modified: 1}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
var bm bson.Marshaler = c.one
|
||||||
|
t.Log(bm)
|
||||||
|
b, err := bson.Marshal(c.one)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
one := One{}
|
||||||
|
if err := bson.Unmarshal(b, &one); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if c.sameAsQuery && (fmt.Sprint(one) != fmt.Sprint(one.Query()) || fmt.Sprint(one) != fmt.Sprint(c.one)) {
|
||||||
|
t.Error(c.sameAsQuery, c.one, one)
|
||||||
|
} else if !c.sameAsQuery {
|
||||||
|
if c.one.Modified == one.Modified {
|
||||||
|
t.Error(c.one.Modified, one.Modified)
|
||||||
|
}
|
||||||
|
c.one.Modified = 0
|
||||||
|
one.Modified = 0
|
||||||
|
if fmt.Sprint(c.one) != fmt.Sprint(one) {
|
||||||
|
t.Error(c.one, one)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,8 +85,8 @@ func TestIntegration(t *testing.T) {
|
||||||
if some[0].Name != ones[0].Name {
|
if some[0].Name != ones[0].Name {
|
||||||
t.Fatal(some[0].Name)
|
t.Fatal(some[0].Name)
|
||||||
}
|
}
|
||||||
if len(some[0].Peers()) > 0 {
|
if l := len(some[0].Peers()); l > 0 {
|
||||||
t.Fatal(some[0].Peers())
|
t.Fatalf("%d: %+v", l, some[0])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package operator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"local/whodunit/storage/entity"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
@ -51,9 +53,16 @@ func opMarshalable(op, key string, value interface{}) map[string]map[string]inte
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return map[string]map[string]interface{}{
|
m := map[string]map[string]interface{}{
|
||||||
op: map[string]interface{}{
|
op: map[string]interface{}{
|
||||||
key: value,
|
key: value,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if _, ok := m["$set"]; !ok {
|
||||||
|
m["$set"] = map[string]interface{}{}
|
||||||
|
}
|
||||||
|
if _, ok := m["$set"][entity.Modified]; !ok {
|
||||||
|
m["$set"][entity.Modified] = time.Now().UnixNano()
|
||||||
|
}
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue