Skip to content

Commit

Permalink
datastore: allow flatten tag on struct pointers
Browse files Browse the repository at this point in the history
Previously, if a struct had a field marked with the `flatten` tag the
type could not be a pointer to a struct. It would fail with the
following error when loading:

datastore: cannot load field "x.x" into a "y.y": no such struct field

This CL fixes this by checking for a pointer type in loadOneElement.

Relvant issue: #1296

Change-Id: I6de60b76b46bcbde20a0c2ad8574e7ee18758e56
Reviewed-on: https://code-review.googlesource.com/c/38550
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jean de Klerk <deklerk@google.com>
  • Loading branch information
poy committed Feb 27, 2019
1 parent 3fdd33d commit 2b3814d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ type OuterFlatten struct {
I []Inner1 `datastore:",flatten"`
J Inner2 `datastore:",flatten,noindex"`
Inner3 `datastore:",flatten"`
K Inner4 `datastore:",flatten"`
K Inner4 `datastore:",flatten"`
L *Inner2 `datastore:",flatten"`
}

type OuterEquivalent struct {
Expand Down Expand Up @@ -1116,13 +1117,17 @@ var testCases = []testCase{
WW: 12,
},
},
L: &Inner2{
Y: 2.71,
},
},
&PropertyList{
Property{Name: "A", Value: int64(1), NoIndex: false},
Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
Property{Name: "K.X.WW", Value: int64(12), NoIndex: false},
Property{Name: "L.Y", Value: float64(2.71), NoIndex: false},
Property{Name: "Z", Value: true, NoIndex: false},
},
"",
Expand All @@ -1135,6 +1140,7 @@ var testCases = []testCase{
Property{Name: "I.W", Value: []interface{}{int64(10), int64(20), int64(30)}, NoIndex: false},
Property{Name: "I.X", Value: []interface{}{"ten", "twenty", "thirty"}, NoIndex: false},
Property{Name: "J.Y", Value: float64(3.14), NoIndex: true},
Property{Name: "L.Y", Value: float64(2.71), NoIndex: false},
Property{Name: "Z", Value: true, NoIndex: false},
},
&OuterFlatten{
Expand All @@ -1150,6 +1156,9 @@ var testCases = []testCase{
Inner3: Inner3{
Z: true,
},
L: &Inner2{
Y: 2.71,
},
},
"",
"",
Expand Down
13 changes: 13 additions & 0 deletions datastore/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ func (l *propertyLoader) loadOneElement(codec fields.List, structValue reflect.V
return ""
}

if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
codec, err = structCache.Fields(field.Type.Elem())
if err != nil {
return err.Error()
}

// Init value if its nil
if v.IsNil() {
v.Set(reflect.New(field.Type.Elem()))
}
structValue = v.Elem()
}

if field.Type.Kind() == reflect.Struct {
codec, err = structCache.Fields(field.Type)
if err != nil {
Expand Down

0 comments on commit 2b3814d

Please sign in to comment.