timshannon/bolthold

bolthold.Get does not populate ID field with boltholdKey tag

bemasher opened this issue · 2 comments

I may be misunderstanding the documentation for boltholdKey, but the following example demonstrates my confusion. When using a uint64 id field with the boltholdKey struct tag, the id field is not populated when using bolthold.Get

package main

import (
    "os"
    "testing"

    "github.com/timshannon/bolthold"
)

type User struct {
    ID   uint64 `json:"id" boltholdKey:"id"`
    User string `json:"user"`
}

func TestInsertGet(t *testing.T) {
    os.Remove("test.db")

    db, err := bolthold.Open("test.db", 0600, nil)
    if err != nil {
        t.Fatalf("%+v\n", err)
    }

    u1 := User{
        User: "john.doe",
    }

    err = db.Insert(bolthold.NextSequence(), &u1)
    if err != nil {
        t.Fatalf("%+v\n", err)
    }
    t.Logf("u1: %+v\n", u1)

    var u2 User
    err = db.Get(u1.ID, &u2)
    if err != nil {
        t.Fatalf("%+v\n", err)
    }
    t.Logf("u2: %+v\n", u2)
}

This produces the following output:

=== RUN   TestInsertGet
--- PASS: TestInsertGet (0.00s)
    main_test.go:32: u1: {ID:1 User:john.doe}
    main_test.go:39: u2: {ID:0 User:john.doe}
PASS
ok      github.com/bemasher/bolthold    0.282s

Why is the ID field on u2 not being populated by db.Get?

Yeah looks like it only gets set during Find. I bet my thinking was that if they are getting by Key, they already know it, so why set it, but it definitely feels like inconsistent behavior.

I'll fix it. Thanks,

This should be good on the master branch now. Open up a new issue if your run into any problems.

Thanks,