google-cloud-go
google-cloud-go copied to clipboard
datastore: ErrFieldMismatch on sub fields clears out top level array field
Client
datastore
Environment
MacOS
Go Environment
go1.17.2 darwin/amd64 cloud.google.com/do/datastore v.1.16.0
Code
type (
ComplexObject struct {
Field1 string
Field2 []ComplexSubObject
}
ComplexSubObject struct {
SubField1 string
SubField2 struct {
SubSubField1 string
SubSubField2 string
}
}
SimpleObject struct {
Field1 string
Field2 []SimpleSubObject
}
SimpleSubObject struct {
SubField1 string
SubField2 struct {
SubSubField1 string
}
}
)
func TestFieldMismatchLoading() {
ctx := context.Background()
c, err := datastore.NewClient(ctx, "my-project")
if err != nil {
panic(err)
}
o := ComplexObject{
Field1: "field-1",
Field2: []ComplexSubObject{{
SubField1: "sub-field-1",
SubField2: struct {
SubSubField1 string
SubSubField2 string
}{
SubSubField1: "sub-sub-field-1",
SubSubField2: "sub-sub-field-2",
},
}},
}
fmt.Printf("%+v\n", o)
key, err := c.Put(ctx, datastore.IncompleteKey("test", nil), &o)
if err != nil {
panic(err)
}
var s SimpleObject
err = c.Get(ctx, key, &s)
if err != nil {
if _, ok := err.(*datastore.ErrFieldMismatch); !ok {
panic(err)
}
}
fmt.Printf("%+v\n", s)
}
Expected behavior
Field2
of the entity should load with one entry that contains an A
field and a B
that only contains the one field it knows about. The documentation for loading suggests that as many fields are filled out as possible and an error is returned alerting the user that there was a field mismatch.
Actual behavior
Field2
is returned as an empty array.
Additional context
If you change Field2
to not be an array, the sub object loads all possible fields that it can.