googleapis / googleapis/google-cloud-go

datastore: ErrFieldMismatch on sub fields clears out top level array field

Open
#5,074 3 comments 0 reactions 1 assignee Assigned to @bhshkh View on GitHub
api: datastore type: feature request
Dominant language
Go
Stars
4.5k
Forks
1.6k
Avg merge
1d 13h
Merged PRs (30d)
109

Description

**Client**

datastore

**Environment**

MacOS

**Go Environment**

go1.17.2 darwin/amd64
cloud.google.com/do/datastore v.1.16.0

**Code**

```go
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.