Skip to content

Commit 37fd839

Browse files
authored
Merge pull request #21 from adjoeio/fix-range-key-condition-for-get-items
remove range key condition from GetItems method of repo
2 parents 093e978 + 5055bab commit 37fd839

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

dynamo_global_index_interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type GlobalIndexInterface interface {
1515
// returns true if items are found, returns false and nil if no items found, returns false and error in case of error
1616
GetItemsWithContext(ctx context.Context, key KeyInterface, items interface{}) (bool, error)
1717

18+
// GetItemsWithRangeWithContext same as GetItemsWithContext, but also respects range key
19+
GetItemsWithRangeWithContext(ctx context.Context, key KeyInterface, items interface{}) (bool, error)
20+
1821
// GetItems by key; it accepts a key interface that is used to get the table name, hash key and range key if it exists; the output will be given in items
1922
// returns true if items are found, returns false and nil if no items found, returns false and error in case of error
2023
GetItems(key KeyInterface, items interface{}) (bool, error)

dynamo_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func (repository *Repository) GetItemsWithContext(ctx context.Context, key KeyIn
391391
return false, err
392392
}
393393

394-
err := buildTableKeyCondition(repository.table(key.TableName()), key).AllWithContext(ctx, items)
394+
err := repository.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()).AllWithContext(ctx, items)
395395
if err != nil {
396396
if err == dynamo.ErrNotFound {
397397
repository.log.info(ctx, key.TableName(), ErrNoItemFound.Error())

global_index.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ func (gi GlobalIndex) GetItemsWithContext(ctx context.Context, key KeyInterface,
5656
return false, err
5757
}
5858

59+
err := gi.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()).Index(gi.name).AllWithContext(ctx, items)
60+
if err != nil {
61+
if err == dynamo.ErrNotFound {
62+
gi.log.info(ctx, key.TableName(), ErrNoItemFound.Error())
63+
return false, nil
64+
}
65+
66+
gi.log.error(ctx, key.TableName(), err.Error())
67+
return false, err
68+
}
69+
70+
val := reflect.ValueOf(items)
71+
if val.Kind() == reflect.Ptr {
72+
val = val.Elem()
73+
}
74+
75+
if val.Kind() == reflect.Array || val.Kind() == reflect.Slice {
76+
if val.Len() == 0 {
77+
return false, nil
78+
}
79+
}
80+
81+
return true, nil
82+
}
83+
84+
// GetItemsWithRangeWithContext queries multiple items by key (hash key) and returns it in the slice of items respecting the range key
85+
func (gi GlobalIndex) GetItemsWithRangeWithContext(ctx context.Context, key KeyInterface, items interface{}) (bool, error) {
86+
if err := isValidKey(key); err != nil {
87+
gi.log.error(ctx, key.TableName(), err.Error())
88+
return false, err
89+
}
90+
5991
err := buildTableKeyCondition(gi.table(key.TableName()), key).Index(gi.name).AllWithContext(ctx, items)
6092
if err != nil {
6193
if err == dynamo.ErrNotFound {

repository_global_index_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package djoemo_test
22

33
import (
4+
"context"
45
"errors"
56

67
"go.uber.org/mock/gomock"
@@ -238,7 +239,9 @@ var _ = Describe("Global Index", func() {
238239
It("should return false and nil if item was not found", func() {
239240
key := Key().WithTableName(UserTableName).
240241
WithHashKeyName("UUID").
241-
WithHashKey("uuid")
242+
WithHashKey("uuid").
243+
WithRangeKeyName("AppID").
244+
WithRangeKey("appid")
242245

243246
dMock.Should().
244247
Query(
@@ -276,6 +279,37 @@ var _ = Describe("Global Index", func() {
276279
})
277280

278281
})
282+
Describe("GetItemsWithRangeWithContext", func() {
283+
It("should get items with Hash and Range", func() {
284+
key := Key().WithTableName(UserTableName).
285+
WithHashKeyName("UUID").
286+
WithHashKey("uuid").
287+
WithRangeKeyName("AppID").
288+
WithRangeKey("appid")
289+
290+
userDBOutput := []map[string]interface{}{
291+
{"UUID": "uuid", "UserName": "name1"},
292+
{"UUID": "uuid", "UserName": "name2"},
293+
}
294+
295+
dMock.Should().
296+
Query(
297+
dMock.WithTable(key.TableName()),
298+
dMock.WithIndex(IndexName),
299+
dMock.WithCondition(*key.HashKeyName(), key.HashKey(), "EQ"),
300+
dMock.WithCondition(*key.RangeKeyName(), key.RangeKey(), "EQ"),
301+
dMock.WithQueryOutput(userDBOutput),
302+
).Exec()
303+
304+
users := &[]User{}
305+
found, err := repository.GIndex(IndexName).GetItemsWithRangeWithContext(context.Background(), key, users)
306+
Expect(err).To(BeNil())
307+
Expect(found).To(BeTrue())
308+
result := *users
309+
Expect(len(result)).To(BeEqualTo(2))
310+
Expect(result[0].UUID).To(BeEqualTo(userDBOutput[0]["UUID"]))
311+
})
312+
})
279313
})
280314

281315
Describe("Query Items", func() {

0 commit comments

Comments
 (0)