dndex/storage/modify.go

52 lines
868 B
Go

package storage
import (
"fmt"
"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) {
if len(key) == 0 {
return nil, fmt.Errorf("no key to %s", op)
}
return bson.Marshal(map[string]interface{}{
op: map[string]interface{}{
key: value,
},
})
}