-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
381 lines (325 loc) · 9.34 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// nolint: govet, golint
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/alecthomas/kong"
"github.com/alecthomas/repr"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Trpc struct {
Pos lexer.Position
Entries []*Entry `( @@ ";"* )*`
}
type Entry struct {
Pos lexer.Position
TestName string ` "test" @String`
Description string ` "desc" @String`
TrpcVersion string ` "trpc" @String`
MaxTime float64 ` ("maxtime" @Float)?`
Timeout float64 ` ("timeout" @Float)?`
VerboseLevel int ` ("verbose" @Int)?`
ImportPath string `| "importpath" @String`
ImportProto string `| "import" "protofile" @String`
ImportProtoSet string `| "import" "protoset" @String`
Endpoint *Endpoint `| @@`
Invoke *Invoke `| @@`
Lines *[]string
Warnings int
Ignores int
}
type Endpoint struct {
Pos lexer.Position
Name string `"endpoint" @Ident`
Tls bool `(@"tls")?`
IPDomain string `@String`
//IPDomain string `( @(Ident ( "." Ident )*) | @([0-9]{1,3} ( "." [0-9]{1,3})*) )`
Port int `"port" @Int`
PerfixPath string `("path" @("/" Ident ( "/" Ident )*))?`
ReflectionPerfixPath string `("reflectPath" @("/" Ident ( "/" Ident )*))?`
IgnoreTrailers bool `(@"ignTrailer")?`
}
type Invoke struct {
Pos lexer.Position
Name string `"invoke" @Ident`
EndPoint string `@Ident`
Service string `@Ident @( "." Ident )*`
RPC string `@Ident`
Goal string `( "goal" @String )?`
Headers []*Header `("headers" "{" @@* "}")?`
Data []*Data `("data" "{" @@* "}")?`
SourceExpects []*Expect `("expects" "{" @@* "}")?`
// These ones are runtime extracted values
Entry *Entry
ContaineReferences bool
RequestData map[string]any
RequestHeaders []string
Expects []Expect
Response *map[string]any
ResponseJson string
ResponseHeaders []string
Conditions []InvokeCondition
}
type NamedInvokes = map[string]*Invoke
type InvokeConditionStatus = int
const (
InvokeDoneWithIgnores InvokeConditionStatus = iota
InvokeDoneWithWarnings
InvokeFailed
)
type InvokeCondition struct {
Condition InvokeConditionStatus
Expect *Expect
Msg string
}
func (i InvokeCondition) String() string {
switch i.Condition {
case InvokeDoneWithWarnings:
return "Warn"
case InvokeDoneWithIgnores:
return "Ignore"
case InvokeFailed:
return "Panic"
default:
panic("Invalid invoke status")
}
}
func NewInvokeCondition(conditionName string, expect *Expect, msg string) InvokeCondition {
var condition = InvokeFailed
switch conditionName {
case "Ignore":
condition = InvokeDoneWithIgnores
case "Warn":
condition = InvokeDoneWithWarnings
case "Panic":
condition = InvokeFailed
}
invokeCondition := InvokeCondition{
Condition: condition,
Expect: expect,
Msg: msg,
}
expect.Invoke.Conditions = append(expect.Invoke.Conditions, invokeCondition)
return invokeCondition
}
type Data struct {
Pos lexer.Position
Key string `(@Ident ":"`
Value Value ` @@ (",")?) `
}
type Value struct {
Pos lexer.Position
String *string ` @String`
Reference *PathExpr `| @@`
RawString *string `| @Ident`
Float *float64 `| @Float`
Int *int64 `| @Int`
Bool *bool `| (@"true" | "false")`
Map *Map `| @@`
Array *Array `| @@`
}
type PathExpr struct {
Parts []Part `@@ ( "." @@ )*`
}
func (p PathExpr) String() string {
parts := []string{}
for _, part := range p.Parts {
parts = append(parts, part.String())
}
return strings.Join(parts, ".")
}
type Part struct {
Obj string `@Ident`
Acc []Acc `("[" @@ "]")*`
//Param []Value `| @("(" @@ ")")`
}
func (p Part) String() string {
str := p.Obj
if len(p.Acc) > 0 {
for _, acc := range p.Acc {
str = str + fmt.Sprintf("[%s]", acc.String())
}
}
return str
}
type Acc struct {
StrIndex *string `@(String|Char|RawString)`
IntIndex *int `| @Int`
}
func (a Acc) String() string {
if a.StrIndex != nil {
return *a.StrIndex
} else {
return fmt.Sprintf("%d", *a.IntIndex)
}
}
type Header struct {
Pos lexer.Position
Key string `(@String ":"`
Value string ` @String )(",")? `
}
type Array struct {
Pos lexer.Position
Elements []*Value `"[" ( @@ ( ","? @@ )* )? "]"`
}
type Map struct {
Pos lexer.Position
Entries []*MapEntry `"{" ( @@ ( ( "," )? @@ )* )? "}"`
}
type MapEntry struct {
Pos lexer.Position
Key string `( @Ident`
Value Value `":" @@)`
}
type Expect struct {
Pos lexer.Position
Path *PathExpr `@@`
Function *Function `@@`
OnFail *string `( "onFail" @("Panic"|"Warn"|"Ignore") )?`
//Code []string
Invoke *Invoke
}
type Function struct {
Pos lexer.Position
Name string `( @Ident`
Arg Value ` "(" (@@)? ")" )`
}
var (
parser = participle.MustBuild(&Trpc{}, participle.UseLookahead(2))
cli struct {
Files []string `required existing file arg help:"TRPC(Test RPC) file(s).\n trpc file or trpc file1 file2 or trpc *.trpc"`
}
)
func (value Value) value(lines *[]string, namedInvokes *NamedInvokes, withReference bool) (interface{}, bool) {
var haveReference bool = false
if value.Map != nil {
result := make(map[string]interface{})
for _, entry := range value.Map.Entries {
result[entry.Key], haveReference = entry.Value.value(lines, namedInvokes, withReference)
}
return result, haveReference
} else if value.Array != nil {
result := make([]interface{}, len(value.Array.Elements))
for i, element := range value.Array.Elements {
result[i], haveReference = element.value(lines, namedInvokes, withReference)
}
return result, haveReference
} else if value.String != nil {
val, _ := strconv.Unquote(*value.String)
return val, haveReference
} else if value.RawString != nil {
return *value.RawString, false
} else if value.Float != nil {
return *value.Float, haveReference
} else if value.Int != nil {
return *value.Int, haveReference
} else if value.Bool != nil {
return *value.Bool, haveReference
} else if value.Reference != nil {
parts := value.Reference.Parts
if parts[1].Obj != "response" && parts[1].Obj != "data" {
syntaxError(lines, value.Pos, 0, "Invalid reference: %s", value.Reference)
}
if invoke, ok := (*namedInvokes)[parts[0].Obj]; ok {
if withReference {
if invoke.Response == nil {
syntaxError(lines, value.Pos, 0, "Reference error %v, invoke must be called before use!", value.Reference)
return nil, false
}
var reference map[string]any
if parts[1].Obj == "response" {
reference = (*invoke.Response)
} else {
reference = invoke.RequestData
}
parts = parts[2:]
for {
if len(parts) == 1 {
//fmt.Printf("ref 1 part %s : %v\n", parts[0].Obj, response[parts[0].Obj])
return reference[parts[0].Obj], true
}
//fmt.Printf("ref 2 %v -> %v\n", parts[0].Obj, parts[0].Acc)
if len(parts[0].Acc) == 0 {
reference = reference[parts[0].Obj].(map[string]interface{})
} else {
if parts[0].Acc[0].StrIndex != nil {
reference = reference[parts[0].Obj].(map[string]interface{})[*parts[0].Acc[0].StrIndex].(map[string]interface{})
} else {
reference = reference[parts[0].Obj].(map[string]interface{})[strconv.Itoa(*parts[0].Acc[0].IntIndex)].(map[string]interface{})
}
}
parts = parts[1:]
}
}
} else {
syntaxError(lines, value.Pos, 0, "Reference not found %v", value.Reference)
return nil, false
}
return value.Reference, true
}
return nil, false
}
func (invoke *Invoke) Parse(lines *[]string, namedInvokes *NamedInvokes, withReference bool, parseHeaders bool) error {
if invoke.Data == nil {
return nil
}
invoke.RequestData = make(map[string]interface{})
haveReference := false
for _, data := range invoke.Data {
var valueHaveReference bool
invoke.RequestData[data.Key], valueHaveReference = data.Value.value(lines, namedInvokes, withReference)
haveReference = haveReference || valueHaveReference
}
invoke.ContaineReferences = haveReference
//parsing Headers must run once on the parsing file
if parseHeaders && invoke.Headers != nil {
invoke.RequestHeaders = make([]string, len(invoke.Headers))
for i, header := range invoke.Headers {
key, _ := strconv.Unquote(header.Key)
value, _ := strconv.Unquote(header.Value)
invoke.RequestHeaders[i] = fmt.Sprintf("%s=%s", key, value)
}
}
return nil
}
func (invoke *Invoke) ParsedDataWithReference(namedInvokes *map[string]Invoke) {
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func main() {
ctx := kong.Parse(&cli)
for _, file := range cli.Files {
trpc := &Trpc{}
trpcFile, err := os.Open(file)
lines, _ := readLines(file)
ctx.FatalIfErrorf(err, "")
err = parser.Parse("", trpcFile, trpc)
ctx.FatalIfErrorf(err, "")
if false {
repr.Println(trpc /*, repr.Hide(&lexer.Position{})*/)
}
if len(trpc.Entries) > 0 {
trpc.Entries[0].Lines = &lines
TasteAndRun(trpc)
}
}
}
/*
replace github.com/jhump/protoreflect => github.com/nima-dvlp/protoreflect v1.12.1
replace github.com/fullstorydev/grpcurl => github.com/nima-dvlp/grpcurl v1.8.8
*/