Skip to content

Commit

Permalink
redis: ignoring unexported fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Osuri committed Oct 19, 2015
1 parent 07ddd65 commit 3c456dd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
# 0.0.4 (Oct 18, 2015)

IMPROVEMENTS:

- redis: Ignoring unexported fields

# 0.0.3 (Oct 8, 2015)

IMPROVEMENTS:
Expand Down
10 changes: 7 additions & 3 deletions redis/redis.go
Expand Up @@ -280,7 +280,7 @@ func (s *Redis) Write(i store.Item) error {
i.SetKey(ri.key)

// convert the item to redis item
if err := marshall(i, value, ri); err != nil {
if err := marshall(value, ri); err != nil {
return err
}

Expand Down Expand Up @@ -435,14 +435,18 @@ func (s *Redis) typeName(value reflect.Value) string {
return s.nameInNamespace(value.Type().Name())
}

// marshall is a helper function that copies the store.Item to redis.item and converts
// marshall is a helper function that copies the value to redis.item and converts
// the struct field types to driver supported types
func marshall(item store.Item, value reflect.Value, rItem *item) error {
func marshall(value reflect.Value, rItem *item) error {
// Ideally use the driver default marshalling redis.ConverAssignBytes
for i := 0; i < value.NumField(); i++ {
// key for data map
k := value.Type().Field(i).Name
field := value.Field(i)
// ignore unexported fields
if !field.CanSet() {
continue
}
switch field.Kind() {
case reflect.String:
rItem.data[k] = field.String()
Expand Down
100 changes: 39 additions & 61 deletions redis/redis_test.go
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/gosuri/go-store/store"
)

var testNs = uuid.New()
var (
testNs = uuid.New()
testRedisURL = "redis://@127.0.0.1:6379/2"
)

type TestR struct {
ID string
Expand All @@ -22,6 +25,8 @@ type TestR struct {
FieldInt int
FieldBool bool
FieldUint uint

fieldPrivate string
}

type TestRs []TestR
Expand All @@ -44,10 +49,7 @@ func TestWrite(t *testing.T) {
FieldUint: 1,
}

db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)

if err := db.Write(s); err != nil {
t.Fatal("err", err)
Expand All @@ -57,7 +59,7 @@ func TestWrite(t *testing.T) {
t.Fatalf("key is emtpy %#v", s)
}

cfg, err := NewConfig("")
cfg, err := NewConfig(testRedisURL)
if err != nil {
t.Fatal(err)
}
Expand All @@ -82,10 +84,7 @@ func TestWrite(t *testing.T) {
}

func BenchmarkRedisWrite(b *testing.B) {
db, err := NewStore("", testNs)
if err != nil {
b.Fatal("err", err)
}
db := testStoreB(b)
for i := 0; i < b.N; i++ {
db.Write(&TestR{Field: "BenchmarkWrite"})
}
Expand All @@ -96,10 +95,7 @@ func TestRead(t *testing.T) {
ID: uuid.New(),
Field: "value",
}
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)

if err := db.Write(s); err != nil {
t.Fatal("err", err)
Expand All @@ -114,10 +110,7 @@ func TestRead(t *testing.T) {
}

func TestReadNotFound(t *testing.T) {
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)
got := &TestR{ID: "invalid"}
if err := db.Read(got); err != store.ErrKeyNotFound {
t.Fatal("expected ErrNotFound, got: ", err)
Expand All @@ -129,10 +122,7 @@ func TestDelete(t *testing.T) {
ID: uuid.New(),
Field: "value",
}
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)

if err := db.Write(s); err != nil {
t.Fatal("err", err)
Expand All @@ -158,10 +148,7 @@ func TestDeleteMultiple(t *testing.T) {
Field: "value1",
}

db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)

if err := db.Write(s); err != nil {
t.Fatal("err", err)
Expand All @@ -182,6 +169,7 @@ func TestDeleteMultiple(t *testing.T) {
}

func TestPartialDeleteMultiple(t *testing.T) {
db := testStore(t)
s := &TestR{
ID: uuid.New(),
Field: "value",
Expand All @@ -197,11 +185,6 @@ func TestPartialDeleteMultiple(t *testing.T) {
Field: "value2",
}

db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}

if err := db.Write(s); err != nil {
t.Fatal("err", err)
}
Expand All @@ -222,32 +205,23 @@ func TestPartialDeleteMultiple(t *testing.T) {
}

func TestDeleteNotFound(t *testing.T) {
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)
got := &TestR{ID: "invalid"}
if err := db.Delete(got); err != store.ErrKeyNotFound {
t.Fatal("expected ErrKeyNotFound, got: ", err)
}
}

func TestDeleteNoKey(t *testing.T) {
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)
got := &TestR{}
if err := db.Delete(got); err != store.ErrEmptyKey {
t.Fatal("expected ErrEmptyKey, got: ", err)
}
}

func benchmarkRead(n int, b *testing.B) {
db, err := NewStore("", testNs)
if err != nil {
b.Fatal(err)
}
db := testStoreB(b)
items := make([]TestR, n, n)
for i := 0; i < n; i++ {
item := TestR{Field: "..."}
Expand All @@ -267,13 +241,10 @@ func BenchmarkRead(b *testing.B) { benchmarkRead(1, b) }
func BenchmarkRead1k(b *testing.B) { benchmarkRead(1000, b) }

func TestList(t *testing.T) {
db := testStore(t)
flushRedisDB()
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
noItems := 1001

noItems := 1001
for i := 0; i < noItems; i++ {
db.Write(&TestR{Field: "..."})
}
Expand All @@ -295,10 +266,7 @@ func TestList(t *testing.T) {
}

func benchmarkList(n int, b *testing.B) {
db, err := NewStore("", testNs)
if err != nil {
b.Fatal(err)
}
db := testStoreB(b)
for i := 0; i < n; i++ {
db.Write(&TestR{Field: "..."})
}
Expand All @@ -313,10 +281,7 @@ func BenchmarkRedisList1k(b *testing.B) { benchmarkList(1000, b) }
func BenchmarkRedisList10k(b *testing.B) { benchmarkList(10000, b) }

func TestReadMultiple(t *testing.T) {
db, err := NewStore("", testNs)
if err != nil {
t.Fatal(err)
}
db := testStore(t)
i := TestR{Field: "field1"}
db.Write(&i)
i2 := TestR{Field: "field1"}
Expand All @@ -334,10 +299,7 @@ func TestReadMultiple(t *testing.T) {
}

func benchmarkReadMultiple(n int, b *testing.B) {
db, err := NewStore("", testNs)
if err != nil {
b.Fatal(err)
}
db := testStoreB(b)
items := make([]TestR, n, n)
for i := 0; i < n; i++ {
item := TestR{Field: "..."}
Expand All @@ -352,8 +314,24 @@ func benchmarkReadMultiple(n int, b *testing.B) {

func BenchmarkReadMultiple1k(b *testing.B) { benchmarkReadMultiple(1000, b) }

func testStoreB(b *testing.B) store.Store {
db, err := NewStore(testRedisURL, testNs)
if err != nil {
b.Fatal(err)
}
return db
}

func testStore(t *testing.T) store.Store {
db, err := NewStore(testRedisURL, testNs)
if err != nil {
t.Fatal(err)
}
return db
}

func flushRedisDB() {
cfg, err := NewConfig("")
cfg, err := NewConfig(testRedisURL)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 3c456dd

Please sign in to comment.