-
-
Notifications
You must be signed in to change notification settings - Fork 177
Description
I'm hitting an issue where structs with an anonymous time.Time field lose their value when encoded and inserted in the DB. Here is an example illustrating the problem. The code below assumes the following:
- There is a DB called
test - The
testDB has an empty table calledtime_test
package main
import (
"log"
"time"
r "github.com/dancannon/gorethink"
)
type TimeHolder struct {
time.Time
}
type Post struct {
Title string
When TimeHolder
}
func main() {
p := Post{
Title: "A test",
When: TimeHolder{time.Now()},
}
log.Printf("%+v\n", p)
session, err := r.Connect(r.ConnectOpts{
Address: "localhost:28015",
Database: "test",
})
if err != nil {
log.Fatal(err)
}
response, err := r.DB("test").Table("time_test").Insert(p).RunWrite(session)
if err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", response)
cursor, err := r.DB("test").Table("time_test").Run(session)
if err != nil {
log.Fatal(err)
}
if err := cursor.One(&p); err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", p)
}The output of this program is:
2016/04/11 04:03:11 {Title:A test When:2016-04-11 04:03:11.156167866 -0500 CDT}
2016/04/11 04:03:11 {Errors:0 Inserted:1 Updated:0 Unchanged:0 Replaced:0 Renamed:0 Skipped:0 Deleted:0 Created:0 DBsCreated:0 TablesCreated:0 Dropped:0 DBsDropped:0 TablesDropped:0 GeneratedKeys:[e766b90f-46fc-4f97-9dbf-84c214fddd5e] FirstError: ConfigChanges:[] Changes:[]}
2016/04/11 04:03:11 {Title:A test When:0001-01-01 00:00:00 +0000 UTC}
As you can see, p.Post.When (a TimeHolder type that just holds an anonymous time.Time field) is retrieved with the zero time value despite having been initialized with the current time. In the DB's web UI, or via the Python driver, the Post.When field can be seen with an empty object:
{
"Title": "A test" ,
"When": { } ,
"id": "2e0c7937-db70-40ae-8547-4bd9d5bc6cb5"
}
I haven't tested whether anonymous struct fields in general are an issue, or whether the problem is just limited to time.Time values (which I presume may be a bit special). It's getting late (or early) over here :-) ...maybe I can explore further later.
Let me know if I'm doing something wrong, or if this is a real issue.
Thanks, Eddy