For storage, store as JSON rather than BSON
This commit is contained in:
@@ -2,6 +2,7 @@ package driver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"local/dndex/storage/entity"
|
||||
"local/storage"
|
||||
@@ -63,7 +64,7 @@ func (s *Storage) Update(ctx context.Context, ns string, filter, operator interf
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err = bson.Marshal(n)
|
||||
v, err = json.Marshal(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -72,12 +73,12 @@ func (s *Storage) Update(ctx context.Context, ns string, filter, operator interf
|
||||
}
|
||||
|
||||
func (s *Storage) Insert(ctx context.Context, ns string, doc interface{}) error {
|
||||
b, err := bson.Marshal(doc)
|
||||
b, err := json.Marshal(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -122,11 +123,15 @@ func (s *Storage) forEach(ctx context.Context, ns string, filter interface{}, fo
|
||||
return err
|
||||
} else {
|
||||
n := bson.M{}
|
||||
if err := bson.Unmarshal(v, &n); err != nil {
|
||||
if err := json.Unmarshal(v, &n); err != nil {
|
||||
return err
|
||||
}
|
||||
if matches(n, m) {
|
||||
if err := foo(id, append(bson.Raw{}, bson.Raw(v)...)); err != nil {
|
||||
b, err := bson.Marshal(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := foo(id, b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"local/dndex/storage/entity"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func TestNewStorage(t *testing.T) {
|
||||
@@ -37,7 +37,7 @@ func fillStorage(t *testing.T, s *Storage) {
|
||||
Connections: map[string]entity.Connection{p.ID: entity.Connection{p.Name}},
|
||||
Attachments: map[string]entity.Attachment{"filename": {"/path/to/file"}},
|
||||
}
|
||||
b, err := bson.Marshal(o)
|
||||
b, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user