How to convert bason to json(with src)?

76 views
Skip to first unread message

Alex Luya

unread,
Aug 13, 2012, 10:34:22 PM8/13/12
to mgo

What I really want to do is hide mongdb connections stuffs(creation,clone..close) and error handling stuff like this:

func ExcuteQuery(collection string, query interface{}) []byte {
ses := ensureMongoConnection("ExcuteQuery")
defer ses.Close()

var result []interface{}
err := ses.DB(databaseName).C(collection).Find(query).All(&result)
if err != nil {
  panic(err)
}

// check err
rawjson, err := json.Marshal(result)
// return result
if err != nil {
  panic(err)
}
return rawjson
}

And test caller will like this:

func (s *MySuite) TestGetModel(c *C) {
res := mgd.ExcuteQuery(collection, nil)
//************************************here something get printed out***********************************
fmt.Println(res)
var list []MySuite
json.Unmarshal(res, list)
//************************************but why here is length of list 0?***********************************
c.Check(list[0].Value, Equals, value)
}

Gustavo Niemeyer

unread,
Aug 14, 2012, 6:40:06 AM8/14/12
to mgo-...@googlegroups.com
On Mon, Aug 13, 2012 at 11:34 PM, Alex Luya <alexand...@gmail.com> wrote:
> What I really want to do is hide mongdb connections
> stuffs(creation,clone..close) and error handling stuff like this:

Alex, please use a pastebin in the future, such as gist.github.com,
rather than pasting a lot of code inline.

Also, sample files should be self contained so we can run them, since
otherwise we'll have to do guess work just to get your stuff running.

> json.Unmarshal(res, list)

This should be &list. Check the error result.


gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages