zemirco / couchdb

CouchDB client in Go
MIT License
44 stars 14 forks source link

Array as QueryParameters.Key value #2

Closed leledumbo closed 8 years ago

leledumbo commented 8 years ago

I have a design document with map function that use array as key for easy grouping. When queried directly, e.g. in browser / through curl, it works correctly, e.g.:

curl http://localhost:5984/db/_design/doc/_view/func?key=["a",1]

However, querying from this library, using View.Get doesn't work. This is due to QueryParameters (and therefore its Key member) being URL encoded so if I do:

// assume view is of type View and correctly retrieved from Database
key = `["a",1]`
params := couchdb.QueryParameters{
  Key: &key,
}
resp, err := view.Get("func", params)

It will instead query for http://localhost:5984/db/_design/doc/_view/func?key=%5B%22a%22%2C1%5D which CouchDB doesn't consider as array value.

Is there any way to overcome this?

zemirco commented 8 years ago

Are you using the latest version of the couchdb package? I have some tests where an array is used as StartKey and EndKey and they work fine.

params := QueryParameters{
  StartKey: pointer.String(fmt.Sprintf("[%q,%d]", "foo2", 20)),
  EndKey:   pointer.String(fmt.Sprintf("[%q,%d]", "foo2", 20)),
}

The same should work for the Key field.

leledumbo commented 8 years ago

OK, using fmt.Sprintf("[%q,%q]") seems working, no idea why fmt.Sprintf("[%q,%d]") causes invalid JSON.