diff --git a/storage/driver/storage.go b/storage/driver/storage.go index 5874798..2629a1c 100644 --- a/storage/driver/storage.go +++ b/storage/driver/storage.go @@ -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 } } diff --git a/storage/driver/storage_test.go b/storage/driver/storage_test.go index 4e0eb8b..11db4cf 100644 --- a/storage/driver/storage_test.go +++ b/storage/driver/storage_test.go @@ -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) } diff --git a/storage/entity/one.go b/storage/entity/one.go index 5b34230..c13ea5a 100644 --- a/storage/entity/one.go +++ b/storage/entity/one.go @@ -3,7 +3,6 @@ package entity import ( "encoding/json" "fmt" - "strings" "time" "go.mongodb.org/mongo-driver/bson" @@ -62,6 +61,18 @@ func (o One) Generic() bson.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) { isMin := fmt.Sprint(o) == fmt.Sprint(o.Query()) if !isMin { @@ -73,19 +84,39 @@ func (o One) MarshalBSON() ([]byte, error) { if o.Attachments == nil { o.Attachments = make(map[string]Attachment) } - b, err := json.Marshal(o) + var v interface{} + b, err := o.toBytes() if err != nil { return nil, err } - m := bson.M{} - if err := json.Unmarshal(b, &m); err != nil { + if err := fromBytes(b, &v); err != nil { return nil, err } - for k, v := range m { - switch v.(type) { - case string: - m[k] = strings.TrimSpace(v.(string)) - } - } - return bson.Marshal(m) + return bson.Marshal(v) +} + +func (o One) toBytes() ([]byte, error) { + return json.Marshal(struct { + ID string `bson:"_id,omitempty" json:"_id"` + 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) } diff --git a/storage/entity/one_test.go b/storage/entity/one_test.go index 236041b..005375c 100644 --- a/storage/entity/one_test.go +++ b/storage/entity/one_test.go @@ -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) +}