0

The structs look like this:

type Account struct {
  Username         string    // NameKey
  Password         []byte    `datastore:",noindex"`
  RegistrationTime time.Time `datastore:",noindex"`
  AppUser
}

type AppUser struct {
  LoginEntries []LoginEntry `datastore:",noindex"`
}

type LoginEntry struct {
  Timestamp time.Time `datastore:",noindex"`
  UserAgent string    `datastore:",noindex"`
  IP        string    `datastore:",noindex"`
}

I'm also sure I put the data correctly, because other data has no problem being updated, and I tried to fmt.Println the content of account Account right before saving it in datastore (Put(ctx, key, &account) and when I print it then I can see all the AppUser information.. but when I later Get the user then the AppUser info doesn't exist (just shows up as {[]}).

I'm quite certain I have stored nested struct slices before in datastore without any problems, so I'm quite confused as to what might be causing it..


The Put func:

func PutAccount(ctx context.Context, acc Account) (*datastore.Key, error) {
  if isValidUsername(acc.Username) != true {
    return nil, errors.New("Invalid username.")
  }
  var hashedPassword []byte
  if acc.RegistrationTime.IsZero() {
    var err error
    hashedPassword, err = bcrypt.GenerateFromPassword(acc.Password, 12)
    if err != nil {
      return nil, err
    }
  } else {
    hashedPassword = acc.Password
  }
  account := Account{
    Username:         strings.ToLower(acc.Username),
    Password:         hashedPassword,
    RegistrationTime: time.Now(),
    AppUser:          acc.AppUser}
  fmt.Println("PutAccount, account:", account) // before saving it prints the AppUser without problems
  key := datastore.NameKey("Account", account.Username, nil)
  return database.DatastoreClient().Put(ctx, key, &account)
}

the Get func:

func GetAccount(ctx context.Context, key *datastore.Key) (Account, error) {
  var account Account
  err := database.DatastoreClient().Get(ctx, key, &account)
  if err != nil {
    return account, err
  }
  return account, nil
}
9
  • 1
    Please show the code used to get and put your data. You may also want to try a flat struct for your Get to see if that works. Commented Dec 12, 2017 at 14:34
  • You may also want to take a look at stackoverflow.com/questions/14308530/… Commented Dec 12, 2017 at 14:38
  • @Marc please see edit. I also noticed that answer but I found the documentation to be quite confusing and complex Commented Dec 12, 2017 at 14:44
  • 2
    My point was that you need to figure out where this is going wrong. Is it the put or the get? You can narrow down the problem by moving to a flat struct, one where LoginEntries is a field of Account. Using other tools to check that the data is actually there would help too. Commented Dec 12, 2017 at 15:09
  • 1
    Embedded structs are flattened out. For details see: Datastore: Structured Properties Commented Dec 12, 2017 at 17:16

1 Answer 1

1

Using named struct works. eg:

type Account struct {
  Username         string    // NameKey
  Password         []byte    `datastore:",noindex"`
  RegistrationTime time.Time `datastore:",noindex"`
  AppUser          AppUser
}

As to why anonymous embedded struct do not, this is probably worthy of an issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, really appreciate it <3
It's not really "embedded" if it's named, now is it? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.