-
Notifications
You must be signed in to change notification settings - Fork 159
/
impl.go
117 lines (101 loc) · 2.52 KB
/
impl.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
106
107
108
109
110
111
112
113
114
115
116
117
package cmd_pb
import (
"encoding/binary"
"google.golang.org/protobuf/proto"
)
func (request *AddPeerRequest) GetContentType() int32 {
return int32(ContentType_AddPeerRequestType)
}
func (request *RemovePeerRequest) GetContentType() int32 {
return int32(ContentType_RemovePeerRequestType)
}
func (request *TransferLeadershipRequest) GetContentType() int32 {
return int32(ContentType_TransferLeadershipRequestType)
}
// TypedMessage instances are protobuf messages which know their command type
type TypedMessage interface {
proto.Message
GetCommandType() int32
}
// EncodeProtobuf returns the encoded message, prefixed with the command type
func EncodeProtobuf(v TypedMessage) ([]byte, error) {
b, err := proto.Marshal(v)
if err != nil {
return nil, err
}
result := make([]byte, len(b)+4)
binary.BigEndian.PutUint32(result, uint32(v.GetCommandType()))
copy(result[4:], b)
return result, nil
}
func (x *CreateEntityCommand) GetCommandType() int32 {
return int32(CommandType_CreateEntityType)
}
func (x *UpdateEntityCommand) GetCommandType() int32 {
return int32(CommandType_UpdateEntityType)
}
func (x *DeleteEntityCommand) GetCommandType() int32 {
return int32(CommandType_DeleteEntityType)
}
func (x *DeleteTerminatorsBatchCommand) GetCommandType() int32 {
return int32(CommandType_DeleteTerminatorsBatchType)
}
func (x *SyncSnapshotCommand) GetCommandType() int32 {
return int32(CommandType_SyncSnapshot)
}
func EncodeTags(tags map[string]interface{}) (map[string]*TagValue, error) {
if len(tags) == 0 {
return nil, nil
}
result := map[string]*TagValue{}
for k, v := range tags {
if v == nil {
result[k] = &TagValue{
Value: &TagValue_NilValue{
NilValue: true,
},
}
} else {
switch val := v.(type) {
case string:
result[k] = &TagValue{
Value: &TagValue_StringValue{
StringValue: val,
},
}
case bool:
result[k] = &TagValue{
Value: &TagValue_BoolValue{
BoolValue: val,
},
}
case float64:
result[k] = &TagValue{
Value: &TagValue_FpValue{
FpValue: val,
},
}
}
}
}
return result, nil
}
func DecodeTags(tags map[string]*TagValue) map[string]interface{} {
if len(tags) == 0 {
return nil
}
result := map[string]interface{}{}
for k, v := range tags {
switch v.Value.(type) {
case *TagValue_NilValue:
result[k] = nil
case *TagValue_BoolValue:
result[k] = v.GetBoolValue()
case *TagValue_StringValue:
result[k] = v.GetStringValue()
case *TagValue_FpValue:
result[k] = v.GetFpValue()
}
}
return result
}