From 5a8cf64951f29b9891611b4e74ab600c94f11af7 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Wed, 11 Sep 2024 16:45:00 +0200 Subject: [PATCH 1/2] x/sqlite: Update core kivik dep, and dedupe SplitKeys function --- x/sqlite/go.mod | 2 +- x/sqlite/go.sum | 4 ++-- x/sqlite/json.go | 33 ++----------------------------- x/sqlite/json_test.go | 45 ------------------------------------------- 4 files changed, 5 insertions(+), 79 deletions(-) diff --git a/x/sqlite/go.mod b/x/sqlite/go.mod index 76467cd19..e47e6373c 100644 --- a/x/sqlite/go.mod +++ b/x/sqlite/go.mod @@ -5,7 +5,7 @@ go 1.22.0 require ( github.com/cenkalti/backoff/v4 v4.3.0 github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 - github.com/go-kivik/kivik/v4 v4.2.4-0.20240716145309-5be038df2f1e + github.com/go-kivik/kivik/v4 v4.3.2-0.20240911141255-ada1fcc539f5 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/mitchellh/mapstructure v1.5.0 diff --git a/x/sqlite/go.sum b/x/sqlite/go.sum index cdc20a019..57b34c360 100644 --- a/x/sqlite/go.sum +++ b/x/sqlite/go.sum @@ -18,8 +18,8 @@ github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/go-kivik/kivik/v4 v4.2.4-0.20240716145309-5be038df2f1e h1:1V2uq+tL0mnfCr01Lg6LLgznVxHXK4+5MmYywxMluAs= -github.com/go-kivik/kivik/v4 v4.2.4-0.20240716145309-5be038df2f1e/go.mod h1:uPonn+OcrDYyZqPXZDTANaWPpmBWAIlpk6gEDnFnDpE= +github.com/go-kivik/kivik/v4 v4.3.2-0.20240911141255-ada1fcc539f5 h1:3BgmgTbGbNIkqiO+g81SCQSjb+cFbUuPbo2dUPBteOo= +github.com/go-kivik/kivik/v4 v4.3.2-0.20240911141255-ada1fcc539f5/go.mod h1:uPonn+OcrDYyZqPXZDTANaWPpmBWAIlpk6gEDnFnDpE= github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= diff --git a/x/sqlite/json.go b/x/sqlite/json.go index 44d7d2e6d..b80627f9d 100644 --- a/x/sqlite/json.go +++ b/x/sqlite/json.go @@ -27,6 +27,7 @@ import ( "strings" internal "github.com/go-kivik/kivik/v4/int/errors" + "github.com/go-kivik/kivik/v4/x/mango" ) type revision struct { @@ -456,7 +457,7 @@ func (d *fullDoc) toRaw(fields ...string) json.RawMessage { if f == "_id" || f == "_rev" { continue } - keys := splitKeys(f) + keys := mango.SplitKeys(f) if v, ok := extractValue(doc, keys...); ok { insertValue(tmp, v, keys) } @@ -495,36 +496,6 @@ func (d *fullDoc) toRaw(fields ...string) json.RawMessage { return result } -// splitKeys splits a field into its component keys. For example, -// `splitKeys("foo.bar")` returns `["foo", "bar"]`. Escaped dots are treated as -// a single key, so `splitKeys("foo\\.bar")` returns `["foo.bar"]`. -func splitKeys(field string) []string { - var escaped bool - result := []string{} - word := make([]byte, 0, len(field)) - for _, ch := range field { - if escaped { - word = append(word, byte(ch)) - escaped = false - continue - } - if ch == '\\' { - escaped = true - continue - } - if ch == '.' { - result = append(result, string(word)) - word = word[:0] - continue - } - word = append(word, byte(ch)) - } - if escaped { - word = append(word, '\\') - } - return append(result, string(word)) -} - func extractValue(obj map[string]interface{}, keys ...string) (interface{}, bool) { for i, key := range keys { if i == len(keys)-1 { diff --git a/x/sqlite/json_test.go b/x/sqlite/json_test.go index e8388dba3..e9f447ece 100644 --- a/x/sqlite/json_test.go +++ b/x/sqlite/json_test.go @@ -323,48 +323,3 @@ func Test_RevID(t *testing.T) { }) } } - -func Test_splitKeys(t *testing.T) { - tests := []struct { - input string - want []string - }{ - { - input: "foo.bar.baz", - want: []string{"foo", "bar", "baz"}, - }, - { - input: "foo", - want: []string{"foo"}, - }, - { - input: "", - want: []string{""}, - }, - { - input: "foo\\.bar", - want: []string{"foo.bar"}, - }, - { - input: "foo\\\\.bar", - want: []string{"foo\\", "bar"}, - }, - { - input: "foo\\", - want: []string{"foo\\"}, - }, - { - input: "foo.", - want: []string{"foo", ""}, - }, - } - - for _, tt := range tests { - t.Run(tt.input, func(t *testing.T) { - got := splitKeys(tt.input) - if d := cmp.Diff(tt.want, got); d != "" { - t.Errorf("unexpected keys (-want, +got): %s", d) - } - }) - } -} From 05336fb2e4f41d7fda7b2bade58192da0e76dff9 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Wed, 11 Sep 2024 16:45:58 +0200 Subject: [PATCH 2/2] minor delinting --- x/sqlite/json_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/sqlite/json_test.go b/x/sqlite/json_test.go index e9f447ece..be5baf4c4 100644 --- a/x/sqlite/json_test.go +++ b/x/sqlite/json_test.go @@ -98,7 +98,7 @@ func Test_prepareDoc(t *testing.T) { t.Errorf("unexpected error = %v, wantErr %v", err, tt.wantErr) } if d := cmp.Diff(tt.want, got); d != "" { - t.Errorf(d) + t.Error(d) } }) } @@ -220,7 +220,7 @@ func Test_revsInfo_revs(t *testing.T) { got = append(got, r.String()) } if d := cmp.Diff(tt.want, got); d != "" { - t.Errorf(d) + t.Error(d) } }) } @@ -262,7 +262,7 @@ func Test_mergeIntoDoc(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, _ := io.ReadAll(tt.doc.toReader()) if d := cmp.Diff(tt.want, string(got)); d != "" { - t.Errorf(d) + t.Error(d) } }) }