New API tested

This commit is contained in:
Bel LaPointe
2020-07-23 21:11:36 -06:00
parent d04ced1e33
commit d572cb5c87
6 changed files with 388 additions and 16 deletions

View File

@@ -23,6 +23,14 @@ func (pi PopIf) MarshalBSON() ([]byte, error) {
return opMarshal("$pull", pi.Key, pi.Filter)
}
type SetMany struct {
Value interface{}
}
func (s SetMany) MarshalBSON() ([]byte, error) {
return opMarshal("$set", "", s.Value)
}
type Set struct {
Key string
Value interface{}
@@ -49,20 +57,28 @@ func opMarshal(op, key string, value interface{}) ([]byte, error) {
return bson.Marshal(marshalable)
}
func opMarshalable(op, key string, value interface{}) map[string]map[string]interface{} {
if len(key) == 0 {
func opMarshalable(op, key string, value interface{}) map[string]interface{} {
if len(key) == 0 && value == nil {
return nil
}
m := map[string]map[string]interface{}{
m := map[string]interface{}{
op: map[string]interface{}{
key: value,
},
}
if len(key) == 0 {
m[op] = 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()
switch m["$set"].(type) {
case map[string]interface{}:
m["$set"].(map[string]interface{})[entity.Modified] = time.Now().UnixNano()
case bson.M:
m["$set"].(bson.M)[entity.Modified] = time.Now().UnixNano()
//case primitive.M:
//m["$set"].(primitive.M)[entity.Modified] = time.Now().UnixNano()
}
return m
}