Skip to content

Commit 9929f17

Browse files
committed
Ensure JavaScript and Symbol codecs are registered
Register the JavaScript and Symbol encoder and decoder functions when registering the primitive codecs. Fix bug in Symbol encoder that wrote JavaScript as the type instead of Symbol. Add tests for conversion of bson.D to and from JavaScript and Symbol. Fix tests for symbol encoding. GODRIVER-646 Change-Id: I090e19844031d40ae778c460c4eaa7fc1edbce73
1 parent 3f37fd6 commit 9929f17

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

bson/primitive_codecs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder)
3939
RegisterEncoder(tDocument, bsoncodec.ValueEncoderFunc(pc.x.DocumentEncodeValue)).
4040
RegisterEncoder(tArray, bsoncodec.ValueEncoderFunc(pc.x.ArrayEncodeValue)).
4141
RegisterEncoder(tValue, bsoncodec.ValueEncoderFunc(pc.x.ValueEncodeValue)).
42+
RegisterEncoder(reflect.PtrTo(tJavaScript), bsoncodec.ValueEncoderFunc(pc.JavaScriptEncodeValue)).
43+
RegisterEncoder(reflect.PtrTo(tSymbol), bsoncodec.ValueEncoderFunc(pc.SymbolEncodeValue)).
4244
RegisterEncoder(reflect.PtrTo(tRawValue), bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)).
4345
RegisterEncoder(reflect.PtrTo(tElementSlice), bsoncodec.ValueEncoderFunc(pc.x.ElementSliceEncodeValue)).
4446
RegisterEncoder(reflect.PtrTo(tBinary), bsoncodec.ValueEncoderFunc(pc.BinaryEncodeValue)).
@@ -56,6 +58,8 @@ func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder)
5658
RegisterDecoder(tDocument, bsoncodec.ValueDecoderFunc(pc.x.DocumentDecodeValue)).
5759
RegisterDecoder(tArray, bsoncodec.ValueDecoderFunc(pc.x.ArrayDecodeValue)).
5860
RegisterDecoder(tValue, bsoncodec.ValueDecoderFunc(pc.x.ValueDecodeValue)).
61+
RegisterDecoder(reflect.PtrTo(tJavaScript), bsoncodec.ValueDecoderFunc(pc.JavaScriptDecodeValue)).
62+
RegisterDecoder(reflect.PtrTo(tSymbol), bsoncodec.ValueDecoderFunc(pc.SymbolDecodeValue)).
5963
RegisterDecoder(reflect.PtrTo(tRawValue), bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)).
6064
RegisterDecoder(reflect.PtrTo(tElementSlice), bsoncodec.ValueDecoderFunc(pc.x.ElementSliceDecodeValue)).
6165
RegisterDecoder(reflect.PtrTo(tBinary), bsoncodec.ValueDecoderFunc(pc.BinaryDecodeValue)).
@@ -114,7 +118,7 @@ func (PrimitiveCodecs) SymbolEncodeValue(ectx bsoncodec.EncodeContext, vw bsonrw
114118
}
115119
}
116120

117-
return vw.WriteJavascript(string(symbol))
121+
return vw.WriteSymbol(string(symbol))
118122
}
119123

120124
// JavaScriptDecodeValue is the ValueDecoderFunc for the primitive.JavaScript type.

bson/primitive_codecs_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func TestDefaultValueEncoders(t *testing.T) {
151151
Received: wrong,
152152
},
153153
},
154-
{"Symbol", primitive.Symbol("foobar"), nil, nil, bsonrwtest.WriteJavascript, nil},
155-
{"*Symbol", psymbol, nil, nil, bsonrwtest.WriteJavascript, nil},
154+
{"Symbol", primitive.Symbol("foobar"), nil, nil, bsonrwtest.WriteSymbol, nil},
155+
{"*Symbol", psymbol, nil, nil, bsonrwtest.WriteSymbol, nil},
156156
{"*Symbol/nil", psymbolNil, nil, nil, bsonrwtest.WriteNull, nil},
157157
},
158158
},
@@ -997,6 +997,18 @@ func TestDefaultValueEncoders(t *testing.T) {
997997
b []byte
998998
err error
999999
}{
1000+
{
1001+
"D to JavaScript",
1002+
D{{"a", primitive.JavaScript(`function() { var hello = "world"; }`)}},
1003+
docToBytes(bsonx.Doc{{"a", bsonx.JavaScript(`function() { var hello = "world"; }`)}}),
1004+
nil,
1005+
},
1006+
{
1007+
"D to Symbol",
1008+
D{{"a", primitive.Symbol("foobarbaz")}},
1009+
docToBytes(bsonx.Doc{{"a", bsonx.Symbol("foobarbaz")}}),
1010+
nil,
1011+
},
10001012
{
10011013
"struct{}",
10021014
struct {
@@ -2424,6 +2436,18 @@ func TestDefaultValueDecoders(t *testing.T) {
24242436
docToBytes(bsonx.Doc{{"a", bsonx.String("bar")}}),
24252437
nil,
24262438
},
2439+
{
2440+
"JavaScript to D",
2441+
D{{"a", primitive.JavaScript(`function() { var hello = "world"; }`)}},
2442+
docToBytes(bsonx.Doc{{"a", bsonx.JavaScript(`function() { var hello = "world"; }`)}}),
2443+
nil,
2444+
},
2445+
{
2446+
"Symbol to D",
2447+
D{{"a", primitive.Symbol("foobarbaz")}},
2448+
docToBytes(bsonx.Doc{{"a", bsonx.Symbol("foobarbaz")}}),
2449+
nil,
2450+
},
24272451
{
24282452
"struct{}",
24292453
struct {

0 commit comments

Comments
 (0)