...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package protostruct
17
18 import (
19 "testing"
20
21 "cloud.google.com/go/internal/testutil"
22 pb "google.golang.org/protobuf/types/known/structpb"
23 )
24
25 func TestDecodeToMap(t *testing.T) {
26 if got := DecodeToMap(nil); !testutil.Equal(got, map[string]interface{}(nil)) {
27 t.Errorf("DecodeToMap(nil) = %v, want nil", got)
28 }
29 nullv := &pb.Value{Kind: &pb.Value_NullValue{}}
30 stringv := &pb.Value{Kind: &pb.Value_StringValue{"x"}}
31 boolv := &pb.Value{Kind: &pb.Value_BoolValue{true}}
32 numberv := &pb.Value{Kind: &pb.Value_NumberValue{2.7}}
33 in := &pb.Struct{Fields: map[string]*pb.Value{
34 "n": nullv,
35 "s": stringv,
36 "b": boolv,
37 "f": numberv,
38 "l": {Kind: &pb.Value_ListValue{&pb.ListValue{
39 Values: []*pb.Value{nullv, stringv, boolv, numberv},
40 }}},
41 "S": {Kind: &pb.Value_StructValue{&pb.Struct{Fields: map[string]*pb.Value{
42 "n1": nullv,
43 "b1": boolv,
44 }}}},
45 }}
46 want := map[string]interface{}{
47 "n": nil,
48 "s": "x",
49 "b": true,
50 "f": 2.7,
51 "l": []interface{}{nil, "x", true, 2.7},
52 "S": map[string]interface{}{"n1": nil, "b1": true},
53 }
54 got := DecodeToMap(in)
55 if diff := testutil.Diff(got, want); diff != "" {
56 t.Error(diff)
57 }
58 }
59
View as plain text