couchdb-golang
couchdb-golang copied to clipboard
golang couchdb endkey param not fetching results
I have a couchdb view returning a resultset based on startkey and endkey params. The view works fine when using the couchdb api and using startkey and endkey params
?include_docs=true&startkey=["nwhire:user:[email protected]"]&endkey=["nwhire:user:[email protected]",{}]
function (doc) {
if (!doc._id.match(/^nwhire:/)) return
var manager_id = doc.managerid
emit([doc._id,"manager"],{_id:manager_id})
emit([doc._id,"newhire"], doc)
}
But when I use the couchdb "github.com/leesper/couchdb-golang" View method and pass the params as options, it is not working.
param := map[string]interface{}{}
param["startkey"] = "[" + strconv.Quote(id) + "]"
param["endkey"] = "[" + strconv.Quote(id) + ",{}]"
param["include_docs"] = true
results, err := c.DB.View(view, nil, param)
fmt.Println(results)
Because, func viewLikeResourceRequest does a JSON.marshal on the values and it add to the params. It appends additional double quotes and escape sequence characters that makes the url bad.
//design.go
func viewLikeResourceRequest(res *Resource, opts map[string]interface{}) (http.Header, []byte, error) {
params := url.Values{}
body := map[string]interface{}{}
for key, val := range opts {
switch key {
case "keys": // json-array, put in body and send POST request
body[key] = val
case "key", "startkey", "start_key", "endkey", "end_key":
data, err := json.Marshal(val)
if err != nil {
return nil, nil, err
}
params.Add(key, string(data))
...
...
But CouchDB expects the parameter to be
endkey=["nwhire:user:[email protected]",{}]
whereas the func makes the parameters as
endkey=""["nwhire:user:[email protected]",{}]""
I think we need to make a new case for these kind of strings to not marshal and directly add as string. Like this
case "startkey_string", "endkey_string":
params.Add(strings.Split(key,"_")[0], val.(string))
Is there any other solution or workaround for this other than adding a new case?
Thank you for your PR
Thank you for merging @leesper