christopherhesse/rethinkgo

Customizability of Document Output

Closed this issue · 6 comments

The proximal problem is that there is apparently no way of inserting structures with application-generated ids. A struct like this:

type Test struct {
Id string
Foo string
}

will insert exactly as is, with capitalization retained. The server will therefore always generate the id, preventing application-generated ids.

Additionally, it would be "nice to have" to be able to customize struct field names.

I suggest implementing the encoding/json method of field tagging, e.g.

type Test struct {
Id string rethink: "id"
Foo string rethink: "bar"
}

https://github.com/christopherhesse/rethinkgo

type MyStruct struct {
    MyField int `json:"my_field"` // (will appear in json as my_field)
    OtherField int // (will appear in json as OtherField)
}

Does this work for you?

No, and it was the first thing we tried.

I will look at this later tonight then, thanks for the bug report!

Could you provide a test script that reproduces this bug? Here's the one I tried, but I couldn't get it to work:

package main

import (
    r "github.com/christopherhesse/rethinkgo"
    "log"
)

type Object struct {
    Id string `json:"id"`
    Val int
}

func main() {
    sess, err := r.Connect("localhost:28015", "test")
    if err != nil {
        log.Fatal("rethink connection error:", err)
    }
    r.TableCreate("test").Run(sess).Exec()
    r.Table("test").Delete().Run(sess).Exec()

    var objects = r.List{Object{Id:"1", Val:1}, Object{Id:"2", Val:2}}
    var response r.WriteResponse
    err = r.Table("test").Insert(objects).Run(sess).One(&response)
    if err != nil {
        log.Fatal("insert error:", err)
    }
    log.Printf("%#v", response)

    products := []Object{}
    err = r.Table("test").Run(sess).All(&products)
    if err != nil {
        log.Fatal("query error:", err)
    }
    log.Printf("%#v", products)
}

Ok, we feel like idiots :)

The issue was that the tags json: "id" and json:"id" are interpreted/read differently. After removing the space, it works as expected. Sorry about this.

I had no idea that happened either, so maybe the guys who make the json module should feel like idiots.