0
votes

It seems the old Appengine Datastore library used to flatten out the embedded structs as mentioned in the section Structured Properties.

And as per this announcement: The cloud Datastore client library by default stores the embedded structs as entity values unless explicitly flattened.

However, it seems like anonymous structs are flattened out even in the cloud Datastore client library.

The package fields section in the Old Datastore API docs indicates this behavior:

These rules apply in the absence of tags: Anonymous struct fields are treated as if their inner exported fields were fields in the outer struct (embedding). The result includes all fields that aren't shadowed by fields at higher level of embedding.

If more than one field with the same name exists at the same level of embedding, it is excluded. An anonymous field that is not of struct type is treated as having its type as its name.

Looking at the 'Structured Properties' in the package datastore API docs, in the last example the Inner3 anonymous struct has an explicit tag to flatten but it seems like the behavior is the same even without the explicit tag.

So, can it be assumed that having an anonymous struct without any struct will always be flattening or do we explicitly need to have a tag to flatten?

1

1 Answers

0
votes

No. The flag should always be added if you want to flat your entities.

Let's take a look the first example of the Structured Properties:

type Inner struct {
    W int32
    X string
    Y bool
}

type Outer struct {
    I Inner
}

Outer would have one property, Inner, encoded as an Entity value. Even if Inner only has one field inside.

On the other hand, if you added the flatten flag, like this:

type Outer struct {
    I Inner   `datastore:",flatten"`
}

Outer would look like this:

type OuterEquivalent struct {
    IDotW      int32  `datastore:"I.W"`
    IDotX      string `datastore:"I.X"`
    IDotY      bool   `datastore:"I.Y"`     
}

So no, the Datastore API does not flat automatically your Entities.