Update storage to new object format
This commit is contained in:
59
storage/operator/modify.go
Normal file
59
storage/operator/modify.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package operator
|
||||
|
||||
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) {
|
||||
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
|
||||
}
|
||||
return map[string]map[string]interface{}{
|
||||
op: map[string]interface{}{
|
||||
key: value,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user