-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
97 lines (88 loc) · 2.39 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package structbson
import (
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/types"
"go.mongodb.org/mongo-driver/bson"
"testing"
)
func Test_Convert(t *testing.T) {
bson.DefaultRegistry = DefaultRegistry
fixture := &types.Struct{Fields: map[string]*types.Value{
"nullValue": nil,
"sliceValue": {
Kind: &types.Value_ListValue{
ListValue: &types.ListValue{
Values: []*types.Value{
{
Kind: &types.Value_ListValue{},
},
{
Kind: &types.Value_ListValue{ListValue: nil},
},
{
Kind: &types.Value_ListValue{ListValue: &types.ListValue{Values: []*types.Value{}}},
},
{
Kind: &types.Value_ListValue{
ListValue: &types.ListValue{
Values: []*types.Value{
{
Kind: &types.Value_StructValue{StructValue: &types.Struct{Fields: nil}},
},
{
Kind: &types.Value_StructValue{StructValue: &types.Struct{Fields: map[string]*types.Value{}}},
},
{
Kind: &types.Value_StructValue{
StructValue: &types.Struct{
Fields: map[string]*types.Value{
"nullValue": nil,
"zeroValue": {
Kind: &types.Value_StructValue{},
},
"emptyValue": {
Kind: &types.Value_StructValue{StructValue: &types.Struct{}},
},
"structValue": {Kind: &types.Value_StructValue{
StructValue: &types.Struct{
Fields: map[string]*types.Value{
"string": {Kind: &types.Value_StringValue{StringValue: "str"}},
"number": {Kind: &types.Value_NumberValue{NumberValue: 1234}},
"boolean": {Kind: &types.Value_BoolValue{BoolValue: true}},
"null": {Kind: &types.Value_NullValue{NullValue: 0}},
}},
}},
},
},
},
},
},
},
},
},
},
},
},
},
}}
bs, err := bson.Marshal(fixture)
if err != nil {
t.Fatal(err)
}
target := &types.Struct{}
if err := bson.Unmarshal(bs, target); err != nil {
t.Fatal(err)
}
jsonMarshaler := jsonpb.Marshaler{}
jb, err := jsonMarshaler.MarshalToString(fixture)
if err != nil {
t.Fatal(err)
}
jt, err := jsonMarshaler.MarshalToString(target)
if err != nil {
t.Fatal(err)
}
if jt != jb {
t.Fail()
}
}