-
Notifications
You must be signed in to change notification settings - Fork 49
/
bridge.go
180 lines (147 loc) · 3.28 KB
/
bridge.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Used internally for the RPC protocol.
*/
package bridge
import (
"encoding/binary"
"errors"
"io"
"log"
"net"
"github.com/Kong/go-pdk/server/kong_plugin_protocol"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
type PdkBridge struct {
conn net.Conn
}
type StepData struct {
Method string
Args []interface{}
}
func New(conn net.Conn) PdkBridge {
return PdkBridge{
conn: conn,
}
}
func readPbFrame(conn net.Conn) (data []byte, err error) {
var len uint32
err = binary.Read(conn, binary.LittleEndian, &len)
if err != nil {
return
}
data = make([]byte, len)
_, err = io.ReadFull(conn, data)
if err != nil {
return nil, err
}
return
}
func writePbFrame(conn net.Conn, data []byte) (err error) {
var len uint32 = uint32(len(data))
err = binary.Write(conn, binary.LittleEndian, len)
if err != nil {
return
}
if len > 0 {
_, err = conn.Write(data)
}
return
}
func WrapString(s string) *kong_plugin_protocol.String {
return &kong_plugin_protocol.String{V: s}
}
func WrapByteString(s []byte) *kong_plugin_protocol.ByteString {
return &kong_plugin_protocol.ByteString{V: s}
}
func WrapHeaders(h map[string][]string) (*structpb.Struct, error) {
h2 := make(map[string]interface{}, len(h))
for k, v := range h {
l := make([]interface{}, len(v))
for i, v2 := range v {
l[i] = v2
}
h2[k] = l
}
st, err := structpb.NewStruct(h2)
if err != nil {
return nil, err
}
return st, nil
}
func UnwrapHeaders(st *structpb.Struct) map[string][]string {
m := st.AsMap()
m2 := make(map[string][]string)
for k, v := range m {
switch v2 := v.(type) {
case string:
m2[k] = []string{v2}
case []string:
m2[k] = v2
case []interface{}:
m2[k] = make([]string, len(v2))
for i, v3 := range v2 {
if s, ok := v3.(string); ok {
m2[k][i] = s
}
}
default:
log.Printf("unexpected type %T on header %s:%v", v2, k, v2)
}
}
return m2
}
func (b PdkBridge) Ask(method string, args proto.Message, out proto.Message) error {
err := writePbFrame(b.conn, []byte(method))
if err != nil {
return err
}
var args_d []byte
if args != nil {
args_d, err = proto.Marshal(args)
if err != nil {
return err
}
}
err = writePbFrame(b.conn, args_d)
if err != nil {
return err
}
out_d, err := readPbFrame(b.conn)
if err != nil {
return err
}
if out != nil {
err = proto.Unmarshal(out_d, out)
}
return err
}
func (b PdkBridge) AskString(method string, args proto.Message) (string, error) {
out := new(kong_plugin_protocol.String)
err := b.Ask(method, args, out)
return out.V, err
}
func (b PdkBridge) AskInt(method string, args proto.Message) (int, error) {
out := new(kong_plugin_protocol.Int)
err := b.Ask(method, args, out)
return int(out.V), err
}
func (b PdkBridge) AskNumber(method string, args proto.Message) (float64, error) {
out := new(kong_plugin_protocol.Number)
err := b.Ask(method, args, out)
return out.V, err
}
func (b PdkBridge) AskValue(method string, args proto.Message) (interface{}, error) {
out := new(structpb.Value)
err := b.Ask(method, args, out)
if err != nil {
return nil, err
}
return out.AsInterface(), nil
}
func (b PdkBridge) Close() error {
return b.conn.Close()
}
func ReturnTypeError(expected string) error {
return errors.New("expected type: " + expected)
}