/
proto3.go
130 lines (120 loc) · 3.57 KB
/
proto3.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
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gorpcvtgatecommon
import (
"github.com/youtube/vitess/go/sqltypes"
vtgatepb "github.com/youtube/vitess/go/vt/proto/vtgate"
"github.com/youtube/vitess/go/vt/tabletserver/querytypes"
)
// This file contains methods to convert the bson rpc structures to and from
// proto3.
// EntityIdsToProto converts an array of EntityId to proto3
func EntityIdsToProto(l []EntityId) []*vtgatepb.ExecuteEntityIdsRequest_EntityId {
if len(l) == 0 {
return nil
}
result := make([]*vtgatepb.ExecuteEntityIdsRequest_EntityId, len(l))
for i, e := range l {
result[i] = &vtgatepb.ExecuteEntityIdsRequest_EntityId{
KeyspaceId: []byte(e.KeyspaceID),
}
v, err := querytypes.BindVariableToValue(e.ExternalID)
if err != nil {
panic(err)
}
result[i].Type = v.Type
result[i].Value = v.Value
}
return result
}
// ProtoToEntityIds converts an array of EntityId from proto3
func ProtoToEntityIds(l []*vtgatepb.ExecuteEntityIdsRequest_EntityId) []EntityId {
if len(l) == 0 {
return nil
}
result := make([]EntityId, len(l))
for i, e := range l {
v, err := sqltypes.ValueFromBytes(e.Type, e.Value)
if err != nil {
panic(err)
}
result[i].KeyspaceID = e.KeyspaceId
result[i].ExternalID = v.ToNative()
}
return result
}
// BoundShardQueriesToProto transforms a list of BoundShardQuery to proto3
func BoundShardQueriesToProto(bsq []BoundShardQuery) ([]*vtgatepb.BoundShardQuery, error) {
if len(bsq) == 0 {
return nil, nil
}
result := make([]*vtgatepb.BoundShardQuery, len(bsq))
for i, q := range bsq {
qq, err := querytypes.BoundQueryToProto3(q.Sql, q.BindVariables)
if err != nil {
return nil, err
}
result[i] = &vtgatepb.BoundShardQuery{
Query: qq,
Keyspace: q.Keyspace,
Shards: q.Shards,
}
}
return result, nil
}
// ProtoToBoundShardQueries transforms a list of BoundShardQuery from proto3
func ProtoToBoundShardQueries(bsq []*vtgatepb.BoundShardQuery) ([]BoundShardQuery, error) {
if len(bsq) == 0 {
return nil, nil
}
result := make([]BoundShardQuery, len(bsq))
for i, q := range bsq {
result[i].Sql = string(q.Query.Sql)
bv, err := querytypes.Proto3ToBindVariables(q.Query.BindVariables)
if err != nil {
return nil, err
}
result[i].BindVariables = bv
result[i].Keyspace = q.Keyspace
result[i].Shards = q.Shards
}
return result, nil
}
// BoundKeyspaceIdQueriesToProto transforms a list of BoundKeyspaceIdQuery to proto3
func BoundKeyspaceIdQueriesToProto(bsq []BoundKeyspaceIdQuery) ([]*vtgatepb.BoundKeyspaceIdQuery, error) {
if len(bsq) == 0 {
return nil, nil
}
result := make([]*vtgatepb.BoundKeyspaceIdQuery, len(bsq))
for i, q := range bsq {
qq, err := querytypes.BoundQueryToProto3(q.Sql, q.BindVariables)
if err != nil {
return nil, err
}
result[i] = &vtgatepb.BoundKeyspaceIdQuery{
Query: qq,
Keyspace: q.Keyspace,
KeyspaceIds: q.KeyspaceIds,
}
}
return result, nil
}
// ProtoToBoundKeyspaceIdQueries transforms a list of BoundKeyspaceIdQuery from proto3
func ProtoToBoundKeyspaceIdQueries(bsq []*vtgatepb.BoundKeyspaceIdQuery) ([]BoundKeyspaceIdQuery, error) {
if len(bsq) == 0 {
return nil, nil
}
result := make([]BoundKeyspaceIdQuery, len(bsq))
for i, q := range bsq {
bv, err := querytypes.Proto3ToBindVariables(q.Query.BindVariables)
if err != nil {
return nil, err
}
result[i].Sql = string(q.Query.Sql)
result[i].BindVariables = bv
result[i].Keyspace = q.Keyspace
result[i].KeyspaceIds = q.KeyspaceIds
}
return result, nil
}