Skip to content

Commit

Permalink
Don't store literal _deleted key in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Mar 17, 2024
1 parent 37cf52f commit 9d33612
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions x/sqlite/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func (d *db) Get(ctx context.Context, id string, options driver.Options) (*drive
}

toMerge := fullDoc{
ID: id,
Rev: r.String(),
ID: id,
Rev: r.String(),
Deleted: deleted,
}

var (
Expand Down
4 changes: 3 additions & 1 deletion x/sqlite/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package sqlite

import (
"bytes"
"context"
"encoding/json"
"io"
Expand Down Expand Up @@ -1175,8 +1176,9 @@ func TestDBGet(t *testing.T) {
if err != nil {
return
}
body, _ := io.ReadAll(doc.Body)
var gotDoc interface{}
if err := json.NewDecoder(doc.Body).Decode(&gotDoc); err != nil {
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&gotDoc); err != nil {
t.Fatal(err)
}
if d := testy.DiffAsJSON(tt.wantDoc, gotDoc); d != nil {
Expand Down
16 changes: 12 additions & 4 deletions x/sqlite/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func prepareDoc(docID string, doc interface{}) (*docData, error) {
if err := json.Unmarshal(tmpJSON, &data); err != nil {
return nil, &internal.Error{Status: http.StatusBadRequest, Err: err}
}
for key := range tmp {
if strings.HasPrefix(key, "_") {
delete(tmp, key)
}
}
if !data.Deleted {
delete(tmp, "_deleted")
}
Expand Down Expand Up @@ -191,6 +196,7 @@ type fullDoc struct {
Revisions *revsInfo `json:"_revisions,omitempty"`
LocalSeq int `json:"_local_seq,omitempty"`
Attachments map[string]attachment `json:"_attachments,omitempty"`
Deleted bool `json:"_deleted,omitempty"`
}

func mergeIntoDoc(doc fullDoc) io.ReadCloser {
Expand All @@ -207,11 +213,13 @@ func mergeIntoDoc(doc fullDoc) io.ReadCloser {
_ = buf.WriteByte(',')
}

// The main doc
_, _ = buf.Write(doc.Doc[1 : len(doc.Doc)-1]) // Omit opening and closing braces
_ = buf.WriteByte(',')

const minJSONObjectLen = 2
if len(doc.Doc) > minJSONObjectLen {
// The main doc
_, _ = buf.Write(doc.Doc[1 : len(doc.Doc)-1]) // Omit opening and closing braces
_ = buf.WriteByte(',')
}

if tmp, _ := json.Marshal(doc); len(tmp) > minJSONObjectLen {
_, _ = buf.Write(tmp[1 : len(tmp)-1])
_ = buf.WriteByte(',')
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func Test_prepareDoc(t *testing.T) {
"foo": "bar",
},
want: &docData{
RevID: "6872a0fc474ada5c46ce054b92897063",
Doc: []byte(`{"_deleted":true,"foo":"bar"}`),
RevID: "9bb58f26192e4ba00f01e2e7b136bbd8",
Doc: []byte(`{"foo":"bar"}`),
Deleted: true,
},
},
Expand Down

0 comments on commit 9d33612

Please sign in to comment.