-
Notifications
You must be signed in to change notification settings - Fork 13
/
message.go
118 lines (94 loc) · 2.72 KB
/
message.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
118
package mc
import (
"fmt"
p2p_peer "github.com/libp2p/go-libp2p-peer"
p2p_pstore "github.com/libp2p/go-libp2p-peerstore"
pb "github.com/mediachain/concat/proto"
multiaddr "github.com/multiformats/go-multiaddr"
)
const MaxMessageSize = 2 << 20 // 1 MB
func PBFromPeerInfo(pbpi *pb.PeerInfo, pinfo p2p_pstore.PeerInfo) {
pbpi.Id = pinfo.ID.Pretty()
pbpi.Addr = make([][]byte, len(pinfo.Addrs))
for x, addr := range pinfo.Addrs {
pbpi.Addr[x] = addr.Bytes()
}
}
func PBToPeerInfo(pbpi *pb.PeerInfo) (empty p2p_pstore.PeerInfo, err error) {
pid, err := p2p_peer.IDB58Decode(pbpi.Id)
if err != nil {
return empty, err
}
addrs := make([]multiaddr.Multiaddr, len(pbpi.Addr))
for x, bytes := range pbpi.Addr {
addr, err := multiaddr.NewMultiaddrBytes(bytes)
if err != nil {
return empty, err
}
addrs[x] = addr
}
return p2p_pstore.PeerInfo{pid, addrs}, nil
}
type ValueError string
func (v ValueError) Error() string {
return string(v)
}
func SimpleValue(val interface{}) (*pb.SimpleValue, error) {
switch val := val.(type) {
case int:
return &pb.SimpleValue{&pb.SimpleValue_IntValue{int64(val)}}, nil
case int64:
return &pb.SimpleValue{&pb.SimpleValue_IntValue{val}}, nil
case string:
return &pb.SimpleValue{&pb.SimpleValue_StringValue{val}}, nil
case *pb.Statement:
return &pb.SimpleValue{&pb.SimpleValue_Stmt{val}}, nil
case *pb.StatementBody:
return &pb.SimpleValue{&pb.SimpleValue_StmtBody{val}}, nil
default:
return nil, ValueError(fmt.Sprintf("Unexpected value type: %T", val))
}
}
func CompoundValue(val map[string]interface{}) (*pb.CompoundValue, error) {
kvpairs := make([]*pb.KeyValuePair, 0, len(val))
for k, v := range val {
sv, err := SimpleValue(v)
if err != nil {
return nil, err
}
kvpairs = append(kvpairs, &pb.KeyValuePair{k, sv})
}
return &pb.CompoundValue{kvpairs}, nil
}
func ValueOf(res *pb.QueryResultValue) (interface{}, error) {
switch val := res.Value.(type) {
case *pb.QueryResultValue_Simple:
return SimpleValueOf(val.Simple)
case *pb.QueryResultValue_Compound:
obj := make(map[string]interface{})
for _, kv := range val.Compound.Body {
sv, err := SimpleValueOf(kv.Value)
if err != nil {
return nil, err
}
obj[kv.Key] = sv
}
return obj, nil
default:
return nil, ValueError(fmt.Sprintf("Unexpected value type: %T", val))
}
}
func SimpleValueOf(sv *pb.SimpleValue) (interface{}, error) {
switch val := sv.Value.(type) {
case *pb.SimpleValue_IntValue:
return val.IntValue, nil
case *pb.SimpleValue_StringValue:
return val.StringValue, nil
case *pb.SimpleValue_Stmt:
return val.Stmt, nil
case *pb.SimpleValue_StmtBody:
return val.StmtBody, nil
default:
return nil, ValueError(fmt.Sprintf("Unexpected value type: %T", val))
}
}