Skip to content

Commit

Permalink
View supports keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lancecarlson committed Jan 18, 2013
1 parent 1ed2408 commit 6310ef1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fmt.Println(lazyCat)
lc.Delete(res.ID, res.Rev)

params := url.Values{"limit": []string{"5"}}
results, err := c.View("myapp", "all", &params)
results, err := c.View("myapp", "all", &params, nil)
if err != nil {
// Do whatever
}
Expand Down
39 changes: 32 additions & 7 deletions couch.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,42 @@ type Row struct {
Value interface{}
}

func (c *Client) View(design string, name string, options *url.Values) (*MultiDocResponse, error) {
res, _, err := c.execRead("GET", c.DBPath() + "/_design/" + design + "/_view/" + name, nil, options, nil)
type KeysRequest struct {
Keys []string `json:"keys"`
}

func (c *Client) View(design string, name string, options *url.Values, keys *[]string) (multiDocResponse *MultiDocResponse, err error) {
url := c.UrlString(c.DBPath() + "/_design/" + design + "/_view/" + name, options)

method := ""
body := new(bytes.Buffer)
if keys != nil {
reqJson, _ := json.Marshal(KeysRequest{Keys: *keys})
body = bytes.NewBuffer(reqJson)
method = "POST"
} else {
method = "GET"
}

req, err := c.NewRequest(method, url, body, nil)
if err != nil {
return nil, err
return
}

multiDocResponse := &MultiDocResponse{}
if err = json.Unmarshal(res, multiDocResponse); err != nil {
return nil, err
httpResp, err := http.DefaultClient.Do(req)
if err != nil {
return
}

respBody, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return
}
return multiDocResponse, nil

if err = json.Unmarshal(respBody, &multiDocResponse); err != nil {
return
}
return
}

func (c *Client) Copy(src string, dest string, destRev *string) (resp *Response, code int, err error) {
Expand Down
15 changes: 14 additions & 1 deletion couch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestView(t *testing.T) {
c.Save(&Dog{Name: "Savannah", Type: "dog"})

params := url.Values{"limit": []string{"5"}}
res, err := c.View("dog", "dog", &params)
res, err := c.View("dog", "dog", &params, nil)
if err != nil {
t.Error(err)
}
Expand All @@ -160,6 +160,19 @@ func TestView(t *testing.T) {
fmt.Println("View")
fmt.Println(res)
fmt.Println("View")

dog1 := Dog{ID: "dog1", Type: "dog"}
dog2 := Dog{ID: "dog2", Type: "dog"}
c.BulkSave(dog1, dog2)

resp, err := c.View("dog", "dog", nil, &[]string{"dog1", "dog2"})
if err != nil {
t.Error(err)
}

fmt.Println("ViewWithKeys")
fmt.Println(resp)
fmt.Println("ViewWithKeys")
}

func TestCopy(t *testing.T) {
Expand Down

0 comments on commit 6310ef1

Please sign in to comment.