[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: PropertyLoadSaver doesn't store __key__ of embedded entity #10351

Open
Philio opened this issue Jun 9, 2024 · 1 comment
Open
Assignees
Labels
api: datastore Issues related to the Datastore API. triage me I really want to be triaged.

Comments

@Philio
Copy link
Philio commented Jun 9, 2024

Client

Datastore

Environment

Linux

Go Environment

$ go version

go version go1.22.3 linux/amd64

$ go env

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/phil/.cache/go-build'
GOENV='/home/phil/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/phil/go/pkg/mod'
GONOPROXY='github.com/boxes-ltd'
GONOSUMDB='github.com/boxes-ltd'
GOOS='linux'
GOPATH='/home/phil/go'
GOPRIVATE='github.com/boxes-ltd'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/snap/go/10603'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/snap/go/10603/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/phil/GolandProjects/boxes-common/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4098846308=/tmp/go-build -gno-record-gcc-switches'

Code

I have a struct with 2 slices, they are interface types so although they save correctly they require a PropertyLoadSaver implementation to reconstruct them on load

type BoxSpec struct {
	Key  *datastore.Key `datastore:"__key__"`
	Content  []SortableContent
	Criteria []SortableCriteria
	Order    int
}

func (s *BoxSpec) LoadKey(k *datastore.Key) error {
	s.Key = k
	return nil
}

func (s *BoxSpec) Load(ps []datastore.Property) error {
	// TODO: Load properties into struct
	return nil
}

func (s *BoxSpec) Save() ([]datastore.Property, error) {
	return datastore.SaveStruct(s)
}

Expected behavior

The key is returned by datastore.SaveStruct(s) (see debug screenshot below) so I would have expected it to be saved

Actual behavior

BoxSpec is embedded in another entity, if I save without the PropertyLoadSaver implementation the key gets saved:

{
  "values": [
    {
      "entityValue": {
        "key": {
          "partitionId": {
            "projectId": "*******"
          },
          "path": [
            {
              "kind": "Boxes_BoxSpec",
              "name": "018ffd4d-1543-760c-9437-fbc05141f3ab"
            }
          ]
        },
        "properties": {
          "Order": {
            "integerValue": "0"
          },
...

After adding PropertyLoadSaver implementation the key is returned by datastore.SaveStruct(s) but doesn't get saved in the database:

{
  "values": [
    {
      "entityValue": {
        "properties": {
          "Order": {
            "integerValue": "0"
          },
...

Screenshots

Screenshot from 2024-06-09 15-49-44

Additional context

N/A

@Philio Philio added the triage me I really want to be triaged. label Jun 9, 2024
@Philio Philio changed the title packagename: PropertyLoadSaver doesn't store __key__ of embedded entity datastore: PropertyLoadSaver doesn't store __key__ of embedded entity Jun 9, 2024
@Philio
Copy link
Author
Philio commented Jun 9, 2024

Currently the solution I have is to save/load the key as an additional property:

func (s *BoxSpec) Load(ps []datastore.Property) error {
	for _, p := range ps {
		switch p.Name {
		case "Key":
			s.Key = p.Value.(*datastore.Key)
		}
	}
	return nil
}

func (s *BoxSpec) Save() ([]datastore.Property, error) {
	ps, err := datastore.SaveStruct(s)
	if err != nil {
		return ps, err
	}
	ps = append(ps, datastore.Property{
		Name:  "Key",
		Value: s.Key,
	})
	return ps, nil
}

@product-auto-label product-auto-label bot added the api: datastore Issues related to the Datastore API. label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants