Skip to content

Commit

Permalink
Merge branch release-0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Jan 23, 2016
2 parents 770a45c + 30fb711 commit 8079803
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Zoom
====

[![Version](https://img.shields.io/badge/version-0.14.2-5272B4.svg)](https://github.com/albrow/zoom/releases)
[![Version](https://img.shields.io/badge/version-0.15.0-5272B4.svg)](https://github.com/albrow/zoom/releases)
[![Circle CI](https://img.shields.io/circleci/project/albrow/zoom/master.svg)](https://circleci.com/gh/albrow/zoom/tree/master)
[![GoDoc](https://godoc.org/github.com/albrow/zoom?status.svg)](https://godoc.org/github.com/albrow/zoom)

Expand Down Expand Up @@ -204,7 +204,10 @@ type Person struct {
}
```

Because of the way Zoom uses reflection, all the fields you want to save need to be public. Almost
Because of the way Zoom uses reflection, all the fields you want to save need to be exported.
Unexported fields (including unexported embedded structs with exported fields) will not
be saved. This is a departure from how the encoding/json and encoding/xml packages
behave. See [issue #25](https://github.com/albrow/zoom/issues/25) for discussion. Almost
any type of field is supported, including custom types, slices, maps, complex types, and embedded
structs. The only things that are not supported are recursive data structures and functions.

Expand Down
10 changes: 5 additions & 5 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestJSONFallback(t *testing.T) {
testConvertType(t, jsonModels, model)
}

type embeddable struct {
type Embeddable struct {
Int int
String string
Bool bool
Expand All @@ -97,15 +97,15 @@ func TestConvertEmbeddedStruct(t *testing.T) {
defer testingTearDown()

type embeddedStructModel struct {
embeddable
Embeddable
RandomId
}
embededStructModels, err := testPool.NewCollection(&embeddedStructModel{}, nil)
if err != nil {
t.Errorf("Unexpected error in testPool.NewCollection: %s", err.Error())
}
model := &embeddedStructModel{
embeddable: embeddable{
Embeddable: Embeddable{
Int: randomInt(),
String: randomString(),
Bool: randomBool(),
Expand All @@ -119,15 +119,15 @@ func TestEmbeddedPointerToStruct(t *testing.T) {
defer testingTearDown()

type embeddedPointerToStructModel struct {
*embeddable
*Embeddable
RandomId
}
embededPointerToStructModels, err := testPool.NewCollection(&embeddedPointerToStructModel{}, nil)
if err != nil {
t.Errorf("Unexpected error in testPool.NewCollection: %s", err.Error())
}
model := &embeddedPointerToStructModel{
embeddable: &embeddable{
Embeddable: &Embeddable{
Int: randomInt(),
String: randomString(),
Bool: randomBool(),
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// atomic transactions, lua scripts, and running Redis commands
// directly if needed.
//
// Version 0.14.2
// Version 0.15.0
//
// For installation instructions, examples, and more information
// visit https://github.com/albrow/zoom.
Expand Down
8 changes: 6 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ func compileModelSpec(typ reflect.Type) (*modelSpec, error) {
numFields := elem.NumField()
for i := 0; i < numFields; i++ {
field := elem.Field(i)
// Skip unexported fields
if field.PkgPath != "" {
// Skip unexported fields. Prior to go 1.6, field.PkgPath won't give us
// the behavior we want. Unlike packages such as encoding/json and
// encoding/gob, Zoom does not save unexported embedded structs with
// exported fields. So instead, we check if the first character of the
// field name is lowercase.
if strings.ToLower(field.Name[0:1]) == field.Name[0:1] {
continue
}

Expand Down
42 changes: 42 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func TestCompileModelSpec(t *testing.T) {
String string `redis:"myString"`
Bool bool `redis:"myBool"`
}
type Embedded struct {
Primative
}
type private struct {
Int int
}
type EmbeddedPrivate struct {
private
}
testCases := []struct {
model interface{}
expectedSpec *modelSpec
Expand Down Expand Up @@ -276,6 +285,39 @@ func TestCompileModelSpec(t *testing.T) {
},
},
},
{
model: &Embedded{},
expectedSpec: &modelSpec{
typ: reflect.TypeOf(&Embedded{}),
name: "Embedded",
fieldsByName: map[string]*fieldSpec{
"Primative": {
kind: inconvertibleField,
name: "Primative",
redisName: "Primative",
typ: reflect.TypeOf(Primative{}),
indexKind: noIndex,
},
},
fields: []*fieldSpec{
{
kind: inconvertibleField,
name: "Primative",
redisName: "Primative",
typ: reflect.TypeOf(Primative{}),
indexKind: noIndex,
},
},
},
},
{
model: &EmbeddedPrivate{},
expectedSpec: &modelSpec{
typ: reflect.TypeOf(&EmbeddedPrivate{}),
name: "EmbeddedPrivate",
fieldsByName: map[string]*fieldSpec{},
},
},
}
for _, tc := range testCases {
gotSpec, err := compileModelSpec(reflect.TypeOf(tc.model))
Expand Down

0 comments on commit 8079803

Please sign in to comment.