Skip to content

Commit 6b3efad

Browse files
GODRIVER-3455 Fix uncommted default encoder tests
1 parent 4bb8442 commit 6b3efad

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

bson/default_value_encoders.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,5 +770,18 @@ func emptyInterfaceValueRF(ec EncodeContext, vw ValueWriter, val any) error {
770770
}
771771

772772
func emptyInterfaceValue(ec EncodeContext, vw ValueWriter, val reflect.Value) error {
773-
return emptyInterfaceValueRF(ec, vw, val.Interface())
773+
if !val.IsValid() || val.Type() != tEmpty {
774+
return ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: val}
775+
}
776+
777+
if val.IsNil() {
778+
return vw.WriteNull()
779+
}
780+
781+
encoder, err := ec.LookupEncoder(val.Elem().Type())
782+
if err != nil {
783+
return err
784+
}
785+
786+
return encoder.EncodeValue(ec, vw, val.Elem())
774787
}

bson/default_value_encoders_test.go

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestDefaultValueEncoders(t *testing.T) {
5151
type myfloat32 float32
5252
type myfloat64 float64
5353

54-
//now := time.Now().Truncate(time.Millisecond)
54+
now := time.Now().Truncate(time.Millisecond)
5555
pjsnum := new(json.Number)
5656
*pjsnum = json.Number("3.14159")
5757
d128 := NewDecimal128(12345, 67890)
@@ -194,21 +194,21 @@ func TestDefaultValueEncoders(t *testing.T) {
194194
{"float64/reflection path", myfloat64(3.14159), nil, nil, writeDouble, nil},
195195
},
196196
},
197-
//{
198-
// "TimeEncodeValue",
199-
// &timeCodec{},
200-
// []subtest{
201-
// {
202-
// "wrong type",
203-
// wrong,
204-
// nil,
205-
// nil,
206-
// nothing,
207-
// ValueEncoderError{Name: "TimeEncodeValue", Types: []reflect.Type{tTime}, Received: reflect.ValueOf(wrong)},
208-
// },
209-
// {"time.Time", now, nil, nil, writeDateTime, nil},
210-
// },
211-
//},
197+
{
198+
"TimeEncodeValue",
199+
ValueEncoderFunc(timeEncodeValue),
200+
[]subtest{
201+
{
202+
"wrong type",
203+
wrong,
204+
nil,
205+
nil,
206+
nothing,
207+
ValueEncoderError{Name: "TimeEncodeValue", Types: []reflect.Type{tTime}, Received: reflect.ValueOf(wrong)},
208+
},
209+
{"time.Time", now, nil, nil, writeDateTime, nil},
210+
},
211+
},
212212
{
213213
"MapEncodeValue",
214214
&mapCodec{},
@@ -531,39 +531,36 @@ func TestDefaultValueEncoders(t *testing.T) {
531531
{"url.URL", url.URL{Scheme: "http", Host: "example.com"}, nil, nil, writeString, nil},
532532
},
533533
},
534-
//{
535-
// "ByteSliceEncodeValue",
536-
// // TODO: Fix this.
537-
// ValueEncoderFunc(func(ec EncodeContext, vw ValueWriter, val reflect.Value) error {
538-
// return (&byteSliceCodec{}).EncodeValue(ec, vw, val)
539-
// }),
540-
// []subtest{
541-
// {
542-
// "wrong type",
543-
// wrong,
544-
// nil,
545-
// nil,
546-
// nothing,
547-
// ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: reflect.ValueOf(wrong)},
548-
// },
549-
// {"[]byte", []byte{0x01, 0x02, 0x03}, nil, nil, writeBinary, nil},
550-
// {"[]byte/nil", []byte(nil), nil, nil, writeNull, nil},
551-
// },
552-
//},
553-
//{
554-
// "EmptyInterfaceEncodeValue",
555-
// &emptyInterfaceCodec{},
556-
// []subtest{
557-
// {
558-
// "wrong type",
559-
// wrong,
560-
// nil,
561-
// nil,
562-
// nothing,
563-
// ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: reflect.ValueOf(wrong)},
564-
// },
565-
// },
566-
//},
534+
{
535+
"ByteSliceEncodeValue",
536+
ValueEncoderFunc(byteSliceEncodeValue(false)),
537+
[]subtest{
538+
{
539+
"wrong type",
540+
wrong,
541+
nil,
542+
nil,
543+
nothing,
544+
ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: reflect.ValueOf(wrong)},
545+
},
546+
{"[]byte", []byte{0x01, 0x02, 0x03}, nil, nil, writeBinary, nil},
547+
{"[]byte/nil", []byte(nil), nil, nil, writeNull, nil},
548+
},
549+
},
550+
{
551+
"EmptyInterfaceEncodeValue",
552+
ValueEncoderFunc(emptyInterfaceValue),
553+
[]subtest{
554+
{
555+
"wrong type",
556+
wrong,
557+
nil,
558+
nil,
559+
nothing,
560+
ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: reflect.ValueOf(wrong)},
561+
},
562+
},
563+
},
567564
{
568565
"ValueMarshalerEncodeValue",
569566
ValueEncoderFunc(valueMarshalerEncodeValue),
@@ -1053,9 +1050,7 @@ func TestDefaultValueEncoders(t *testing.T) {
10531050
},
10541051
{
10551052
"CoreArrayEncodeValue",
1056-
ValueEncoderFunc(func(ec EncodeContext, vw ValueWriter, v reflect.Value) error {
1057-
return coreArrayEncodeValueRF(ec, vw, v.Interface())
1058-
}),
1053+
ValueEncoderFunc(coreArrayEncodeValue),
10591054
[]subtest{
10601055
{
10611056
"wrong type",

0 commit comments

Comments
 (0)