For storage, store as JSON rather than BSON
parent
a6f5bc3192
commit
d67654e601
|
|
@ -2,6 +2,7 @@ package driver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"local/dndex/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"local/storage"
|
"local/storage"
|
||||||
|
|
@ -63,7 +64,7 @@ func (s *Storage) Update(ctx context.Context, ns string, filter, operator interf
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v, err = bson.Marshal(n)
|
v, err = json.Marshal(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m := bson.M{}
|
m := bson.M{}
|
||||||
if err := bson.Unmarshal(b, &m); err != nil {
|
if err := json.Unmarshal(b, &m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,11 +123,15 @@ func (s *Storage) forEach(ctx context.Context, ns string, filter interface{}, fo
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
n := bson.M{}
|
n := bson.M{}
|
||||||
if err := bson.Unmarshal(v, &n); err != nil {
|
if err := json.Unmarshal(v, &n); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if matches(n, m) {
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package driver
|
package driver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"local/dndex/storage/entity"
|
"local/dndex/storage/entity"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewStorage(t *testing.T) {
|
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}},
|
Connections: map[string]entity.Connection{p.ID: entity.Connection{p.Name}},
|
||||||
Attachments: map[string]entity.Attachment{"filename": {"/path/to/file"}},
|
Attachments: map[string]entity.Attachment{"filename": {"/path/to/file"}},
|
||||||
}
|
}
|
||||||
b, err := bson.Marshal(o)
|
b, err := json.Marshal(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package entity
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
|
@ -62,6 +61,18 @@ func (o One) Generic() bson.M {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o One) MarshalJSON() ([]byte, error) {
|
||||||
|
b, err := o.MarshalBSON()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
v := bson.M{}
|
||||||
|
if err := bson.Unmarshal(b, &v); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return json.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
func (o One) MarshalBSON() ([]byte, error) {
|
func (o One) MarshalBSON() ([]byte, error) {
|
||||||
isMin := fmt.Sprint(o) == fmt.Sprint(o.Query())
|
isMin := fmt.Sprint(o) == fmt.Sprint(o.Query())
|
||||||
if !isMin {
|
if !isMin {
|
||||||
|
|
@ -73,19 +84,39 @@ func (o One) MarshalBSON() ([]byte, error) {
|
||||||
if o.Attachments == nil {
|
if o.Attachments == nil {
|
||||||
o.Attachments = make(map[string]Attachment)
|
o.Attachments = make(map[string]Attachment)
|
||||||
}
|
}
|
||||||
b, err := json.Marshal(o)
|
var v interface{}
|
||||||
|
b, err := o.toBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m := bson.M{}
|
if err := fromBytes(b, &v); err != nil {
|
||||||
if err := json.Unmarshal(b, &m); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for k, v := range m {
|
return bson.Marshal(v)
|
||||||
switch v.(type) {
|
}
|
||||||
case string:
|
|
||||||
m[k] = strings.TrimSpace(v.(string))
|
func (o One) toBytes() ([]byte, error) {
|
||||||
}
|
return json.Marshal(struct {
|
||||||
}
|
ID string `bson:"_id,omitempty" json:"_id"`
|
||||||
return bson.Marshal(m)
|
Name string `bson:"name,omitempty" json:"name"`
|
||||||
|
Type string `bson:"type,omitempty" json:"type"`
|
||||||
|
Title string `bson:"title,omitempty" json:"title"`
|
||||||
|
Text string `bson:"text,omitempty" json:"text"`
|
||||||
|
Modified int64 `bson:"modified,omitempty" json:"modified"`
|
||||||
|
Connections map[string]Connection `bson:"connections" json:"connections"`
|
||||||
|
Attachments map[string]Attachment `bson:"attachments" json:"attachments"`
|
||||||
|
}{
|
||||||
|
ID: o.ID,
|
||||||
|
Name: o.Name,
|
||||||
|
Type: o.Type,
|
||||||
|
Title: o.Title,
|
||||||
|
Text: o.Text,
|
||||||
|
Modified: o.Modified,
|
||||||
|
Connections: o.Connections,
|
||||||
|
Attachments: o.Attachments,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromBytes(b []byte, v interface{}) error {
|
||||||
|
return json.Unmarshal(b, v)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,3 +57,24 @@ func TestOneMarshalBSON(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToFromBytes(t *testing.T) {
|
||||||
|
o := One{
|
||||||
|
Name: "hello",
|
||||||
|
Connections: map[string]Connection{
|
||||||
|
"world": Connection{Relationship: "!"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := o.toBytes()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var v interface{}
|
||||||
|
if err := fromBytes(b, &v); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log(v)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue