-
Notifications
You must be signed in to change notification settings - Fork 2
/
conv_test.go
105 lines (95 loc) · 1.97 KB
/
conv_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
98
99
100
101
102
103
104
105
//
// conv_test.go
//
// Copyright (c) 2016 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
//
package structpbconv_test
import (
"reflect"
"testing"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/jkawamoto/structpbconv"
)
type ActivityPayload struct {
EventTimestampUs string `structpb:"event_timestamp_us"`
EventType string `structpb:"vent_type"`
TraceID string `structpb:"trace_id"`
Actor struct {
User string
}
Resource struct {
Zone string
Type string
ID string
Name string
}
Version string
EventSubtype string `structpb:"event_subtype"`
Operation struct {
Zone string
Type string
ID string
Name string
}
Items []string
Map map[string]string
}
func TestConvert(t *testing.T) {
src := &structpb.Struct{
Fields: map[string]*structpb.Value{
"items": {
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "Hello World",
},
},
},
},
},
},
"map": {
Kind: &structpb.Value_StructValue{
StructValue: &structpb.Struct{
Fields: map[string]*structpb.Value{
"key1": {
Kind: &structpb.Value_StringValue{
StringValue: "value1",
},
},
},
},
},
},
},
}
var payload ActivityPayload
if err := structpbconv.Convert(src, &payload); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(payload, ActivityPayload{
Items: []string{"Hello World"},
Map: map[string]string{
"key1": "value1",
},
}) {
t.Fatal("failed to convert")
}
}
func ExampleConvert() {
var payload interface{}
var res *ActivityPayload
switch s := payload.(type) {
case *structpb.Struct:
res = &ActivityPayload{}
structpbconv.Convert(s, res)
default:
// Error: Given payload is not an instance of *structpb.Struct.
}
}