Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle embedded unexported structs in Go 1.6. #54

Merged
merged 1 commit into from
Dec 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func generateFieldInfoAndMetadata(t reflect.Type, namePrefix string, indexPrefix
numFields := t.NumField()
for i := 0; i < numFields; i++ {
tf := t.Field(i)
if tf.PkgPath != "" {
if tf.PkgPath != "" && !tf.Anonymous {
continue
}

Expand Down
50 changes: 50 additions & 0 deletions goon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2090,3 +2090,53 @@ func TestParents(t *testing.T) {
t.Fatalf("parent of key not equal '%s' v '%s'! ", dk, rootKey)
}
}

type ContainerStruct struct {
Id string `datastore:"-" goon:"id"`
embeddedStruct
}

type embeddedStruct struct {
X int
y int
}

func TestEmbeddedStruct(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatalf("Could not start aetest - %v", err)
}
defer done()
g := FromContext(c)

// Store some data with an embedded unexported struct
pcs := &ContainerStruct{Id: "foo"}
pcs.X = 1
pcs.y = 2
_, err = g.Put(pcs)
if err != nil {
t.Errorf("Unexpected error on put - %v", err)
}

// First run fetches from the datastore (as Put() only caches to the local cache)
// Second run fetches from memcache (as our first run here called Get() which caches into memcache)
for i := 1; i <= 2; i++ {
// Clear the local cache
g.FlushLocalCache()

// Fetch it and confirm the values
gcs := &ContainerStruct{Id: pcs.Id}
err = g.Get(gcs)
if err != nil {
t.Errorf("#%v - Unexpected error on get - %v", i, err)
}
// The exported field must have the correct value
if gcs.X != pcs.X {
t.Errorf("#%v - Expected - %v, got %v", i, pcs.X, gcs.X)
}
// The unexported field must be zero-valued
if gcs.y != 0 {
t.Errorf("#%v - Expected - %v, got %v", i, 0, gcs.y)
}
}
}