mrb/riakpbc — pull request #12 by boj (2013-05-27 07:23:37+00:00)
Title: query index works, large changes to storeobject

Starting to get my head wrapped around the whole Riak PB interface now.

I have modified StoreObject so it's signature is the following:

func (c *Conn) StoreObject(bucket, key string, content *RpbContent) (*RpbPutResp, error)

RpbContent is another one of those things that open a lot of options up to the developer (indexes, links, etc.).

However! It is a real pain to use these interfaces in such a raw fashion. You get code that looks like the following (pulled from query_test.go):

d1, err := json.Marshal(&Farm{Animal: "chicken"})
if err != nil {
t.Error(err.Error())
}
i1 := &RpbPair{
Key: []byte("animal_bin"),
Value: []byte("chicken"),
}
c1 := &RpbContent{
Value: d1,
ContentType: []byte("application/json"),
Indexes: []*RpbPair{
i1,
},
}
if _, err := riak.StoreObject("farm", "chicken", c1); err != nil {
t.Error(err.Error())
}

A couple ideas on how to make things a little more developer friendly.

1. Create an Object struct which contains simple methods to configure RpbContent, and let all the complicated stuff happen behind the scenes.
2. Create our own marshaller so we can do stuff like below.

type Data struct {
Foo string `json:"foo" riak:"index"`
BelongsTo string `json:"belongs_to" riak:"link"`
}

Then if we do something like `riak.Marshall(&Data{Foo: "bar", BelongsTo: "mrb"})` (with the future written Marshall method) it would automatically create the `{"foo":"mrb"}` JSON structure, add the `foo_bin` index automatically, and create the `belongs_to` Riak link for link walking.

The StoreObject signature would then look like:

func (c *Conn) StoreObject(bucket, key string, data interface{}) (*RpbPutResp, error)

Then the user can happily pass in any structure they want to store without worrying about the actual backend implementation. As long as they use the struct annotations they should be good to go.

So the complicated code above would then look more like the following, given the previous Data struct:

content, err := riak.Marshall(&Data{Foo: "bar", BelongsTo: "mrb"})
if err != nil {
t.Error(err.Error())
}
if _, err := riak.StoreObject("mybucket", "mykey", content); err != nil {
t.Error(err.Error())
}

I'm not sure how easy that will be to implement, but I think it would be by far the cleanest way to go about it.

Anyways, this pull request works (the 2i Index search is now functional), but it did require the StoreObject parameter change.