Set modified via bson marshal and update operators

This commit is contained in:
Bel LaPointe
2020-07-22 21:13:18 -06:00
parent 710e20d6e0
commit 9338cf86c9
4 changed files with 85 additions and 3 deletions

View File

@@ -1,7 +1,16 @@
package entity
import (
"encoding/json"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
)
const (
Name = "_id"
JSONName = "name"
Relationship = "relationship"
Type = "type"
Title = "title"
@@ -33,3 +42,22 @@ func (o One) Peers() []string {
}
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)
}