Skip to content

Commit 1625591

Browse files
committed
dstore: Where
1 parent e4a4d3a commit 1625591

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

dstore/document.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func marshal(v interface{}) ([]byte, error) {
5050
return buf.Bytes(), nil
5151
}
5252

53+
// Values map.
54+
func (d *Document) Values() map[string]interface{} {
55+
return d.values
56+
}
57+
5358
// unmarshal uses msgpack with fallback to json tags.
5459
func unmarshal(b []byte, i interface{}) error {
5560
dec := msgpack.NewDecoder(bytes.NewReader(b))

dstore/documents.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ type Documents interface {
2323
//
2424
Create(ctx context.Context, path string, values map[string]interface{}) error
2525

26-
// Set (or create) document at path.
26+
// Set (or create or update) document at path.
2727
// This will overwrite any existing document data, unless you specify MergeAll() option.
2828
//
2929
// To marshal a value, use dstore.From(v) to convert to a map (using msgpack or json tags).
3030
// If merging and using dstore.From(v), fields with omitempty will no overwrite existing values.
3131
//
32+
// To update a document:
33+
//
34+
// update := map[string]interface{}{
35+
// "property1": value1,
36+
// }
37+
// err := fi.Set(ctx, path, update, dstore.MergeAll())
38+
//
3239
// Paths can be nested as long as they are even length components.
3340
// For example,
3441
//
@@ -56,7 +63,7 @@ type Documents interface {
5663

5764
// Delete at path.
5865
Delete(ctx context.Context, path string) (bool, error)
59-
// If a path is not found, it is ignored.
66+
// DeleteAll paths. If a path is not found, it is ignored.
6067
DeleteAll(ctx context.Context, paths []string) error
6168

6269
// DocumentIterator.

dstore/mem.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/google/go-cmp/cmp"
1213
"github.com/keys-pub/keys/dstore/events"
1314
"github.com/keys-pub/keys/encoding"
1415
"github.com/keys-pub/keys/tsutil"
@@ -219,6 +220,18 @@ func (m *Mem) list(ctx context.Context, parent string, opt ...Option) ([]*Docume
219220
if doc == nil {
220221
return nil, errors.Errorf("missing document in List")
221222
}
223+
if opts.Where != nil {
224+
if opts.Where.Op != "==" {
225+
return nil, errors.Errorf("unsupported op")
226+
}
227+
v, ok := doc.Get(opts.Where.Name)
228+
if !ok {
229+
continue
230+
}
231+
if !cmp.Equal(v, opts.Where.Value) {
232+
continue
233+
}
234+
}
222235
if opts.NoData {
223236
doc = &Document{Path: doc.Path, CreatedAt: doc.CreatedAt, UpdatedAt: doc.UpdatedAt}
224237
}

dstore/mem_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ func testDocumentsListOptions(t *testing.T, ds dstore.Documents) {
161161
require.NoError(t, err)
162162
err = ds.Create(ctx, "/test/3", dstore.Data([]byte("val3")))
163163
require.NoError(t, err)
164+
err = ds.Create(ctx, "/where/4", map[string]interface{}{"name": "val4"})
165+
require.NoError(t, err)
164166

165167
for i := 1; i < 3; i++ {
166168
err := ds.Create(ctx, dstore.Path("a", fmt.Sprintf("e%d", i)), dstore.Data([]byte("🤓")))
@@ -210,6 +212,10 @@ func testDocumentsListOptions(t *testing.T, ds dstore.Documents) {
210212
}
211213
iter.Release()
212214
require.Equal(t, []string{"/b/eb1", "/b/eb2"}, paths)
215+
216+
docs, err := ds.Documents(ctx, "/where", dstore.Where("name", "==", "val4"))
217+
require.NoError(t, err)
218+
require.Equal(t, []string{"/where/4"}, dstore.Paths(docs))
213219
}
214220

215221
func testMetadata(t *testing.T, ds dstore.Documents) {

dstore/options.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ type Options struct {
1010
Limit int
1111
// NoData to only include only path in Document (no data).
1212
NoData bool
13+
// Where
14+
Where *where
15+
}
16+
17+
type where struct {
18+
Name string
19+
Op string
20+
Value interface{}
1321
}
1422

1523
// Option ...
@@ -31,6 +39,13 @@ func Prefix(prefix string) Option {
3139
}
3240
}
3341

42+
// Where name op value.
43+
func Where(name string, op string, value interface{}) Option {
44+
return func(o *Options) {
45+
o.Where = &where{Name: name, Op: op, Value: value}
46+
}
47+
}
48+
3449
// Index to start at.
3550
func Index(index int) Option {
3651
return func(o *Options) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6
1111
github.com/godbus/dbus v4.1.0+incompatible
1212
github.com/golang/protobuf v1.4.2 // indirect
13+
github.com/google/go-cmp v0.5.5 // indirect
1314
github.com/keybase/go-keychain v0.0.0-20200502122510-cda31fe0c86d
1415
github.com/keybase/saltpack v0.0.0-20200430135328-e19b1910c0c5
1516
github.com/keys-pub/secretservice v0.0.0-20200519003656-26e44b8df47f

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
2929
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
3030
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
3131
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
32+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
33+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3234
github.com/keybase/go-codec v0.0.0-20180928230036-164397562123 h1:yg56lYPqh9suJepqxOMd/liFgU/x+maRPiB30JNYykM=
3335
github.com/keybase/go-codec v0.0.0-20180928230036-164397562123/go.mod h1:r/eVVWCngg6TsFV/3HuS9sWhDkAzGG8mXhiuYA+Z/20=
3436
github.com/keybase/go-keychain v0.0.0-20200502122510-cda31fe0c86d h1:gVjhBCfVGl32RIBooOANzfw+0UqX8HU+yPlMv8vypcg=

0 commit comments

Comments
 (0)