From 9d3361217eb5ece5d5052e698e5d46937a05d25f Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Sat, 16 Mar 2024 23:09:30 +0100 Subject: [PATCH] Don't store literal _deleted key in doc --- x/sqlite/get.go | 5 +++-- x/sqlite/get_test.go | 4 +++- x/sqlite/json.go | 16 ++++++++++++---- x/sqlite/json_test.go | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/x/sqlite/get.go b/x/sqlite/get.go index 4a3bd4bb0..bb367e7fb 100644 --- a/x/sqlite/get.go +++ b/x/sqlite/get.go @@ -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 ( diff --git a/x/sqlite/get_test.go b/x/sqlite/get_test.go index a4170cc5a..19ef86c5c 100644 --- a/x/sqlite/get_test.go +++ b/x/sqlite/get_test.go @@ -16,6 +16,7 @@ package sqlite import ( + "bytes" "context" "encoding/json" "io" @@ -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 { diff --git a/x/sqlite/json.go b/x/sqlite/json.go index 1b20359ea..bff7c9a9e 100644 --- a/x/sqlite/json.go +++ b/x/sqlite/json.go @@ -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") } @@ -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 { @@ -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(',') diff --git a/x/sqlite/json_test.go b/x/sqlite/json_test.go index e6745e4a6..772d06c32 100644 --- a/x/sqlite/json_test.go +++ b/x/sqlite/json_test.go @@ -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, }, },