Skip to content

Commit

Permalink
Merge pull request #1046 from go-kivik/dedupeSplit
Browse files Browse the repository at this point in the history
x/sqlite: Update core kivik dep, and dedupe SplitKeys function
  • Loading branch information
flimzy authored Sep 11, 2024
2 parents ada1fcc + 05336fb commit 3d1eb2a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 82 deletions.
2 changes: 1 addition & 1 deletion x/sqlite/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
33 changes: 2 additions & 31 deletions x/sqlite/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
51 changes: 3 additions & 48 deletions x/sqlite/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 3d1eb2a

Please sign in to comment.