@@ -453,6 +453,48 @@ func TestDefaultValueEncoders(t *testing.T) {
453
453
},
454
454
},
455
455
},
456
+ {
457
+ "ProxyEncodeValue" ,
458
+ ValueEncoderFunc (dve .ProxyEncodeValue ),
459
+ []subtest {
460
+ {
461
+ "wrong type" ,
462
+ wrong ,
463
+ nil ,
464
+ nil ,
465
+ bsonrwtest .Nothing ,
466
+ ValueEncoderError {
467
+ Name : "ProxyEncodeValue" ,
468
+ Types : []interface {}{(Proxy )(nil )},
469
+ Received : wrong ,
470
+ },
471
+ },
472
+ {
473
+ "Proxy error" ,
474
+ testProxy {err : errors .New ("proxy error" )},
475
+ nil ,
476
+ nil ,
477
+ bsonrwtest .Nothing ,
478
+ errors .New ("proxy error" ),
479
+ },
480
+ {
481
+ "Lookup error" ,
482
+ testProxy {ret : nil },
483
+ & EncodeContext {Registry : buildDefaultRegistry ()},
484
+ nil ,
485
+ bsonrwtest .Nothing ,
486
+ ErrNilType ,
487
+ },
488
+ {
489
+ "success" ,
490
+ testProxy {ret : int64 (1234567890 )},
491
+ & EncodeContext {Registry : buildDefaultRegistry ()},
492
+ nil ,
493
+ bsonrwtest .WriteInt64 ,
494
+ nil ,
495
+ },
496
+ },
497
+ },
456
498
}
457
499
458
500
for _ , tc := range testCases {
@@ -704,6 +746,8 @@ func TestDefaultValueEncoders(t *testing.T) {
704
746
AC decimal.Decimal128
705
747
AD * time.Time
706
748
AE testValueMarshaler
749
+ AF Proxy
750
+ AG testProxy
707
751
}{
708
752
A : true ,
709
753
B : 123 ,
@@ -729,6 +773,8 @@ func TestDefaultValueEncoders(t *testing.T) {
729
773
AC : decimal128 ,
730
774
AD : & now ,
731
775
AE : testValueMarshaler {t : bsontype .String , buf : bsoncore .AppendString (nil , "hello, world" )},
776
+ AF : testProxy {ret : struct { Hello string }{Hello : "world!" }},
777
+ AG : testProxy {ret : struct { Pi float64 }{Pi : 3.14159 }},
732
778
},
733
779
buildDocument (func (doc []byte ) []byte {
734
780
doc = bsoncore .AppendBooleanElement (doc , "a" , true )
@@ -753,6 +799,8 @@ func TestDefaultValueEncoders(t *testing.T) {
753
799
doc = bsoncore .AppendDecimal128Element (doc , "ac" , decimal128 )
754
800
doc = bsoncore .AppendDateTimeElement (doc , "ad" , now .UnixNano ()/ int64 (time .Millisecond ))
755
801
doc = bsoncore .AppendStringElement (doc , "ae" , "hello, world" )
802
+ doc = bsoncore .AppendDocumentElement (doc , "af" , buildDocument (bsoncore .AppendStringElement (nil , "hello" , "world!" )))
803
+ doc = bsoncore .AppendDocumentElement (doc , "ag" , buildDocument (bsoncore .AppendDoubleElement (nil , "pi" , 3.14159 )))
756
804
return doc
757
805
}(nil )),
758
806
nil ,
@@ -785,6 +833,8 @@ func TestDefaultValueEncoders(t *testing.T) {
785
833
AC []decimal.Decimal128
786
834
AD []* time.Time
787
835
AE []testValueMarshaler
836
+ AF []Proxy
837
+ AG []testProxy
788
838
}{
789
839
A : []bool {true },
790
840
B : []int32 {123 },
@@ -818,6 +868,14 @@ func TestDefaultValueEncoders(t *testing.T) {
818
868
{t : bsontype .String , buf : bsoncore .AppendString (nil , "hello" )},
819
869
{t : bsontype .String , buf : bsoncore .AppendString (nil , "world" )},
820
870
},
871
+ AF : []Proxy {
872
+ testProxy {ret : struct { Hello string }{Hello : "world!" }},
873
+ testProxy {ret : struct { Foo string }{Foo : "bar" }},
874
+ },
875
+ AG : []testProxy {
876
+ {ret : struct { One int64 }{One : 1234567890 }},
877
+ {ret : struct { Pi float64 }{Pi : 3.14159 }},
878
+ },
821
879
},
822
880
buildDocument (func (doc []byte ) []byte {
823
881
doc = appendArrayElement (doc , "a" , bsoncore .AppendBooleanElement (nil , "0" , true ))
@@ -867,6 +925,22 @@ func TestDefaultValueEncoders(t *testing.T) {
867
925
doc = appendArrayElement (doc , "ae" ,
868
926
bsoncore .AppendStringElement (bsoncore .AppendStringElement (nil , "0" , "hello" ), "1" , "world" ),
869
927
)
928
+ doc = appendArrayElement (doc , "af" ,
929
+ bsoncore .AppendDocumentElement (
930
+ bsoncore .AppendDocumentElement (nil , "0" ,
931
+ bsoncore .BuildDocument (nil , bsoncore .AppendStringElement (nil , "hello" , "world!" )),
932
+ ), "1" ,
933
+ bsoncore .BuildDocument (nil , bsoncore .AppendStringElement (nil , "foo" , "bar" )),
934
+ ),
935
+ )
936
+ doc = appendArrayElement (doc , "ag" ,
937
+ bsoncore .AppendDocumentElement (
938
+ bsoncore .AppendDocumentElement (nil , "0" ,
939
+ bsoncore .BuildDocument (nil , bsoncore .AppendInt64Element (nil , "one" , 1234567890 )),
940
+ ), "1" ,
941
+ bsoncore .BuildDocument (nil , bsoncore .AppendDoubleElement (nil , "pi" , 3.14159 )),
942
+ ),
943
+ )
870
944
return doc
871
945
}(nil )),
872
946
nil ,
@@ -888,7 +962,7 @@ func TestDefaultValueEncoders(t *testing.T) {
888
962
if diff := cmp .Diff ([]byte (b ), tc .b ); diff != "" {
889
963
t .Errorf ("Bytes written differ: (-got +want)\n %s" , diff )
890
964
t .Errorf ("Bytes\n got: %v\n want:%v\n " , b , tc .b )
891
- t .Errorf ("Readers\n got: %v\n want:%v\n " , b , tc .b )
965
+ t .Errorf ("Readers\n got: %v\n want:%v\n " , bsoncore . Document ( b ), bsoncore . Document ( tc .b ) )
892
966
}
893
967
})
894
968
}
@@ -904,3 +978,10 @@ type testValueMarshaler struct {
904
978
func (tvm testValueMarshaler ) MarshalBSONValue () (bsontype.Type , []byte , error ) {
905
979
return tvm .t , tvm .buf , tvm .err
906
980
}
981
+
982
+ type testProxy struct {
983
+ ret interface {}
984
+ err error
985
+ }
986
+
987
+ func (tp testProxy ) ProxyBSON () (interface {}, error ) { return tp .ret , tp .err }
0 commit comments