googleapis / googleapis/google-cloud-go

datastore: flatten option validation not confined to field it is defined on

Open
#5,026 5 comments 0 reactions 1 assignee Assigned to @bhshkh View on GitHub
api: datastore priority: p2 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/go/datastore v1.6.0

**Code**

e.g.
```go
package main

import (
"context"
"fmt"
"time"

"cloud.google.com/go/datastore"
)

type CustomTime struct {
T time.Time
}

type B struct {
IDs []string
}

type S struct {
A CustomTime `datastore:",flatten"`
Bs []B
}

func main() {
ctx := context.Background()
c, err := datastore.NewClient(ctx, "my-project")
if err != nil {
panic(err)
}

key, err := c.Put(ctx, datastore.IncompleteKey("s", nil), &S{})
if err != nil {
panic(err)
}
fmt.Println(key)
}
```

**Expected behavior**

The struct is saved with the CustomTime field flattened to the property "A.T" and Bs as it normally would

**Actual behavior**

I receive the error `datastore: flattening nested structs leads to a slice of slices: field "IDs"`. However this field is not supposed to be flattened. If you switch the order of S's fields to have `Bs` first, then I no longer receive the error. Once the order is changed you can add any other struct fields after `A` that do not contain a slice of slices and they are correctly saved as non-flattened structs. This leads me to believe the validation for the flatten option is in error, not the actual flattening of fields.

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.