diff options
author | alabid <alabidan@gmail.com> | 2014-10-20 00:09:05 -0400 |
---|---|---|
committer | alabid <alabidan@gmail.com> | 2014-10-20 00:09:05 -0400 |
commit | 38fdc8213a2a6e2fbe51a81f1054075302f01480 (patch) | |
tree | f366d1d895126202fdd9bbbf1c1d4476113e8460 | |
parent | b6d1b7a4151e0fd204c68e901bf6a79c1d0055b3 (diff) | |
download | mongo-38fdc8213a2a6e2fbe51a81f1054075302f01480.tar.gz |
TOOLS-279 fixes: 1. convertKeys method should return marshallable bson.M instead of ordinary Go map. Updated unit tests accordingly.
2. Don't use UnixNano(). It could easily return a number > int64.
3. escape $code value in json.Javascript
Former-commit-id: fd4299000c2dff297b529123bc5f268fa355393b
-rw-r--r-- | common/bsonutil/converter.go | 4 | ||||
-rw-r--r-- | common/bsonutil/converter_test.go | 4 | ||||
-rw-r--r-- | common/json/json_format.go | 2 |
3 files changed, 5 insertions, 5 deletions
diff --git a/common/bsonutil/converter.go b/common/bsonutil/converter.go index 17c44cb5fc2..3be732de332 100644 --- a/common/bsonutil/converter.go +++ b/common/bsonutil/converter.go @@ -96,7 +96,7 @@ func ConvertJSONValueToBSON(x interface{}) (interface{}, error) { } } -func convertKeys(v bson.M) (map[string]interface{}, error) { +func convertKeys(v bson.M) (bson.M, error) { for key, value := range v { jsonValue, err := ConvertBSONValueToJSON(value) if err != nil { @@ -157,7 +157,7 @@ func ConvertBSONValueToJSON(x interface{}) (interface{}, error) { return json.ObjectId(v.Hex()), nil case time.Time: // Date - return json.Date(v.UnixNano() / 1e6), nil + return json.Date(v.Unix()*1000 + int64(v.Nanosecond()/1e6)), nil case int64: // NumberLong return json.NumberLong(v), nil diff --git a/common/bsonutil/converter_test.go b/common/bsonutil/converter_test.go index ad4bed9ebeb..7d789bf38d8 100644 --- a/common/bsonutil/converter_test.go +++ b/common/bsonutil/converter_test.go @@ -59,11 +59,11 @@ func TestArraysBSONToJSON(t *testing.T) { So(err, ShouldBeNil) _jObj, ok := __jObj.([]interface{}) So(ok, ShouldBeTrue) - jObj, ok := _jObj[1].(map[string]interface{}) + jObj, ok := _jObj[1].(bson.M) So(ok, ShouldBeTrue) So(len(jObj), ShouldEqual, 2) So(jObj["a"], ShouldEqual, json.NumberLong(20)) - jjObj, ok := jObj["b"].(map[string]interface{}) + jjObj, ok := jObj["b"].(bson.M) So(ok, ShouldBeTrue) So(jjObj["c"], ShouldResemble, json.RegExp{"hi", "i"}) diff --git a/common/json/json_format.go b/common/json/json_format.go index ad0086fe032..41b362bca23 100644 --- a/common/json/json_format.go +++ b/common/json/json_format.go @@ -14,7 +14,7 @@ func (b BinData) MarshalJSON() ([]byte, error) { } func (js JavaScript) MarshalJSON() ([]byte, error) { - data := []byte(fmt.Sprintf(`{ "$code": "%v"`, js.Code)) + data := []byte(fmt.Sprintf(`{ "$code": %q`, js.Code)) scopeChunk := []byte{} if js.Scope != nil { |