mikejs / gomongo

Go driver for MongoDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unmarshal doesn't work for maps

sougou opened this issue · comments

Test Program:

package main

import (
  "fmt"
  "github.com/mikejs/gomongo/mongo"
)

func main() {
  in := map[string]string {"foo": "test"}
  doc, err := mongo.Marshal(in)
  if err != nil {
    println(err)
  }
  b := doc.Bytes()
  fmt.Printf("%#v\n", string(b))
  ret := make(map[string]string)
  _ = mongo.Unmarshal(b, &ret)
  fmt.Printf("%v\n", ret)
}

Here's the output:
"\x13\x00\x00\x00\x02foo\x00\x05\x00\x00\x00test\x00\x00"
map[foo:]

I managed to get this working by adding a call to b2.Flush() to the Parse() function in bson.go at the end of the switch statement. Not sure if that's the right thing to do:

499       b2.Int64(int64(ui64))
500     default:
501       err = os.NewError(fmt.Sprintf("don't know how to handle kind %v yet", kind))
502     }
503     b2.Flush()
504 
505     kind, _ = buf.ReadByte()

sougou

I'm not 100% sure, but where I've used mongo before with other languages their equivalent types haven't been about to write to mongo either.

I think this is because a map is bson is ~= to an object, where the keys of the map are properties of the object.

I could be wrong, but I've always assumed this to be the case and now used maps kinds with mongo.