dndex/storage/operator/modify.go

69 lines
1.3 KiB
Go

package operator
import (
"fmt"
"local/whodunit/storage/entity"
"time"
"go.mongodb.org/mongo-driver/bson"
)
type Unset string
func (u Unset) MarshalBSON() ([]byte, error) {
return opMarshal("$unset", string(u), "")
}
type PopIf struct {
Key string
Filter interface{}
}
func (pi PopIf) MarshalBSON() ([]byte, error) {
return opMarshal("$pull", pi.Key, pi.Filter)
}
type Set struct {
Key string
Value interface{}
}
func (s Set) MarshalBSON() ([]byte, error) {
return opMarshal("$set", s.Key, s.Value)
}
type Push struct {
Key string
Value interface{}
}
func (p Push) MarshalBSON() ([]byte, error) {
return opMarshal("$push", p.Key, p.Value)
}
func opMarshal(op, key string, value interface{}) ([]byte, error) {
marshalable := opMarshalable(op, key, value)
if len(marshalable) == 0 {
return nil, fmt.Errorf("failed marshalling op")
}
return bson.Marshal(marshalable)
}
func opMarshalable(op, key string, value interface{}) map[string]map[string]interface{} {
if len(key) == 0 {
return nil
}
m := map[string]map[string]interface{}{
op: map[string]interface{}{
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
}