-
Notifications
You must be signed in to change notification settings - Fork 63
/
proto_parser.cc
706 lines (638 loc) · 22.9 KB
/
proto_parser.cc
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//---------------------------------------------------------------------
//---------------------------------------------------------------------
#include <google/protobuf/compiler/importer.h>
#include <lv_interop.h>
#include <sstream>
#include <list>
//---------------------------------------------------------------------
//---------------------------------------------------------------------
using namespace google::protobuf::compiler;
using namespace google::protobuf;
namespace grpc_labview
{
//---------------------------------------------------------------------
//---------------------------------------------------------------------
#ifdef _PS_4
#pragma pack (push, 1)
#endif
struct MessageFieldCluster
{
LStrHandle fieldName;
LStrHandle embeddedMessage;
int32_t protobufIndex;
int32_t type;
char isRepeated;
char isInOneof;
LStrHandle oneofContainerName;
};
struct EnumFieldCluster
{
int32_t version;
LStrHandle name;
LStrHandle typeUrl;
LStrHandle enumValues;
char isAliasAllowed;
};
#ifdef _PS_4
#pragma pack (pop)
#endif
//---------------------------------------------------------------------
//---------------------------------------------------------------------
class ErrorCollector : public MultiFileErrorCollector
{
public:
void AddError(const std::string& filename, int line, int column, const std::string& message) override;
std::string GetLVErrorMessage();
private:
std::list<string> _errors;
};
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void ErrorCollector::AddError(const std::string& filename, int line, int column, const std::string& message)
{
std::string errorMessage = filename + ": " + std::to_string(line) + " - " + message;
_errors.emplace_back(errorMessage);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
std::string ErrorCollector::GetLVErrorMessage()
{
std::stringstream result;
for (auto& s : _errors)
{
result << s << std::endl;
}
return result.str();
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
class LVProtoParser
{
public:
LVProtoParser();
void AddSearchPath(const std::string& searchPath);
void Import(const std::string& filePath, const std::string& searchPath);
public:
DiskSourceTree m_SourceTree;
ErrorCollector m_ErrorCollector;
Importer m_Importer;
std::list<std::string> m_searchPaths;
const FileDescriptor* m_FileDescriptor;
static LVProtoParser* s_Parser;
};
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LVProtoParser* LVProtoParser::s_Parser = nullptr;
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LVProtoParser::LVProtoParser()
: m_Importer(&m_SourceTree, &m_ErrorCollector)
{
s_Parser = this;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void LVProtoParser::AddSearchPath(const std::string& searchPath)
{
std::string path = searchPath;
std::replace(path.begin(), path.end(), '\\', '/');
m_searchPaths.push_back(path);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void LVProtoParser::Import(const std::string& filePath, const std::string& search)
{
std::string searchPath = search;
std::replace(searchPath.begin(), searchPath.end(), '\\', '/');
m_SourceTree.MapPath("", searchPath);
for (auto path : m_searchPaths)
{
m_SourceTree.MapPath("", path);
}
std::string path = filePath;
std::replace(path.begin(), path.end(), '\\', '/');
m_FileDescriptor = m_Importer.Import(path);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
std::string TransformMessageName(const std::string& messageName)
{
std::string result = messageName;
std::replace(result.begin(), result.end(), '.', '_');
return result;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void AddFieldError(FieldDescriptor* field, string message)
{
grpc_labview::LVProtoParser::s_Parser->m_ErrorCollector.AddError("", 0, 0, message);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void AddNestedMessages(const google::protobuf::Descriptor& descriptor, std::set<const google::protobuf::Descriptor*>& messages)
{
auto count = descriptor.nested_type_count();
for (int x = 0; x < count; ++x)
{
auto current = descriptor.nested_type(x);
AddNestedMessages(*current, messages);
messages.emplace(current);
}
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void AddDependentMessages(const google::protobuf::FileDescriptor& descriptor, std::set<const google::protobuf::Descriptor*>& messages)
{
auto imported = descriptor.dependency_count();
for (int x = 0; x < imported; ++x)
{
AddDependentMessages(*descriptor.dependency(x), messages);
}
auto count = descriptor.message_type_count();
for (int x = 0; x < count; ++x)
{
auto current = descriptor.message_type(x);
AddNestedMessages(*current, messages);
messages.emplace(current);
}
}
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetgRPCAPIVersion(int* version)
{
grpc_labview::InitCallbacks();
*version = 2;
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVCreateParser(grpc_labview::LVProtoParser** parser)
{
grpc_labview::InitCallbacks();
*parser = new grpc_labview::LVProtoParser();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVAddParserSearchPath(grpc_labview::LVProtoParser* parser, const char* searchPath)
{
if (parser == nullptr)
{
return -1;
}
parser->AddSearchPath(searchPath);
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVImportProto2(grpc_labview::LVProtoParser* parser, const char* filePath, const char* searchPath)
{
if (parser == nullptr)
{
return -1;
}
parser->Import(filePath, searchPath);
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVImportProto(const char* filePath, const char* searchPath, grpc_labview::LVProtoParser** parser)
{
grpc_labview::InitCallbacks();
*parser = new grpc_labview::LVProtoParser();
(*parser)->Import(filePath, searchPath);
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetErrorString(grpc_labview::LVProtoParser* parser, grpc_labview::LStrHandle* error)
{
if (parser == nullptr)
{
return -1;
}
auto errorMessage = parser->m_ErrorCollector.GetLVErrorMessage();
grpc_labview::SetLVString(error, errorMessage);
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetServices(grpc_labview::LVProtoParser* parser, grpc_labview::LV1DArrayHandle* services)
{
if (parser == nullptr)
{
return -1;
}
if (parser->m_FileDescriptor == nullptr)
{
return -2;
}
auto count = parser->m_FileDescriptor->service_count();
auto elementSize = sizeof(ServiceDescriptor*);
if (NumericArrayResize(grpc_labview::GetTypeCodeForSize(elementSize), 1, services, count * elementSize) != 0)
{
parser->m_ErrorCollector.AddError("", 0, 0, "Failed to resize array");
return -3;
}
(**services)->cnt = count;
const ServiceDescriptor** serviceElements = (**services)->bytes<const ServiceDescriptor*>();
for (int x = 0; x < count; ++x)
{
serviceElements[x] = parser->m_FileDescriptor->service(x);
}
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetMessages(grpc_labview::LVProtoParser* parser, grpc_labview::LV1DArrayHandle* messages)
{
if (parser == nullptr)
{
return -1;
}
if (parser->m_FileDescriptor == nullptr)
{
return -2;
}
std::set<const google::protobuf::Descriptor*> allMessages;
grpc_labview::AddDependentMessages(*parser->m_FileDescriptor, allMessages);
auto count = allMessages.size();
auto elementSize = sizeof(Descriptor*);
if (grpc_labview::NumericArrayResize(grpc_labview::GetTypeCodeForSize(elementSize), 1, messages, count * elementSize) != 0)
{
return -3;
}
(**messages)->cnt = count;
const Descriptor** messageElements = (**messages)->bytes<const Descriptor*>();
int x = 0;
for (auto& it : allMessages)
{
messageElements[x] = it;
x += 1;
}
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
void AddTopLevelEnums(const google::protobuf::FileDescriptor& descriptor, std::set<const google::protobuf::EnumDescriptor*>& allEnums)
{
// Get global enums defined in the proto file and other imported proto files.
// FileDescriptor is being used to fetch these enum descriptors.
auto noOfImportedFiles = descriptor.dependency_count();
for (int x = 0; x < noOfImportedFiles; ++x)
{
AddTopLevelEnums(*descriptor.dependency(x), allEnums);
}
int topLevelEnumCount = descriptor.enum_type_count();
for (int x = 0; x < topLevelEnumCount; ++x)
{
auto current = descriptor.enum_type(x);
allEnums.emplace(current);
}
}
void AddNestedEnums(grpc_labview::LV1DArrayHandle* messages, std::set<const google::protobuf::EnumDescriptor*>& allEnums)
{
// Get enums nested within messages.
// Descriptor is being used to fetch these enum descriptors.
// Read messages sent from LV layer. These are the messages already collected
// by the LV layer during the parsing of the proto file.
std::set<const google::protobuf::Descriptor*> allMessages;
const Descriptor** messageElements = (**messages)->bytes<const Descriptor*>();
for (int i = 0; i < (**messages)->cnt; i++)
{
allMessages.emplace(messageElements[i]);
}
// Get enums nested within each message.
for (auto& message : allMessages)
{
int enumCount = message->enum_type_count();
for (int i = 0; i < enumCount; i++)
{
auto current = message->enum_type(i);
allEnums.emplace(current);
}
}
}
LIBRARY_EXPORT int LVGetEnums(grpc_labview::LVProtoParser* parser, grpc_labview::LV1DArrayHandle* messages, grpc_labview::LV1DArrayHandle* enums)
{
if (parser == nullptr)
{
return -1;
}
if (parser->m_FileDescriptor == nullptr)
{
return -2;
}
std::set<const google::protobuf::EnumDescriptor*> allEnums;
AddTopLevelEnums(*(parser->m_FileDescriptor), allEnums);
AddNestedEnums(messages, allEnums);
auto count = allEnums.size();
auto elementSize = sizeof(EnumDescriptor*);
if (grpc_labview::NumericArrayResize(grpc_labview::GetTypeCodeForSize(elementSize), 1, enums, count * elementSize) != 0)
{
return -3;
}
(**enums)->cnt = count;
const EnumDescriptor** enumElements = (**enums)->bytes<const EnumDescriptor*>();
int x = 0;
for (auto& it : allEnums)
{
enumElements[x] = it;
x += 1;
}
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetServiceName(ServiceDescriptor* service, grpc_labview::LStrHandle* name)
{
if (service == nullptr)
{
return -1;
}
grpc_labview::SetLVString(name, service->name());
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetServiceMethods(ServiceDescriptor* service, grpc_labview::LV1DArrayHandle* methods)
{
if (service == nullptr)
{
return -1;
}
auto count = service->method_count();
auto elementSize = sizeof(ServiceDescriptor*);
if (grpc_labview::NumericArrayResize(grpc_labview::GetTypeCodeForSize(elementSize), 1, methods, count * elementSize) != 0)
{
return -3;
}
(**methods)->cnt = count;
auto methodElements = (**methods)->bytes<const MethodDescriptor*>();
for (int x = 0; x < count; ++x)
{
methodElements[x] = service->method(x);
}
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetMethodName(MethodDescriptor* method, grpc_labview::LStrHandle* name)
{
if (method == nullptr)
{
return -1;
}
SetLVString(name, method->name());
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetMethodFullName(MethodDescriptor* method, grpc_labview::LStrHandle* name)
{
if (method == nullptr)
{
return -1;
}
grpc_labview::SetLVString(name, method->full_name());
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVIsMethodClientStreaming(MethodDescriptor* method, int* clientStreaming)
{
if (method == nullptr)
{
return -1;
}
*clientStreaming = method->client_streaming();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVIsMethodServerStreaming(MethodDescriptor* method, int* serverStreaming)
{
if (method == nullptr)
{
return -1;
}
*serverStreaming = method->server_streaming();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetMethodInput(MethodDescriptor* method, const Descriptor** inputType)
{
if (method == nullptr)
{
return -1;
}
*inputType = method->input_type();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetMethodOutput(MethodDescriptor* method, const Descriptor** outputType)
{
if (method == nullptr)
{
return -1;
}
*outputType = method->output_type();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVMessageName(Descriptor* descriptor, grpc_labview::LStrHandle* name)
{
if (descriptor == nullptr)
{
return -1;
}
SetLVString(name, grpc_labview::TransformMessageName(descriptor->full_name()));
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVMessageTypeUrl(Descriptor* descriptor, grpc_labview::LStrHandle* name)
{
if (descriptor == nullptr)
{
return -1;
}
SetLVString(name, descriptor->full_name());
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVMessageHasOneof(Descriptor* descriptor, int* hasOneof)
{
if (descriptor == nullptr)
{
return -1;
}
*hasOneof = descriptor->real_oneof_decl_count();
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVEnumName(EnumDescriptor* descriptor, grpc_labview::LStrHandle* name)
{
if (descriptor == nullptr)
{
return -1;
}
SetLVString(name, grpc_labview::TransformMessageName(descriptor->full_name()));
return 0;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVEnumTypeUrl(EnumDescriptor* descriptor, grpc_labview::LStrHandle* name)
{
if (descriptor == nullptr)
{
return -1;
}
SetLVString(name, descriptor->full_name());
return 0;
}
LIBRARY_EXPORT void SerializeReflectionInfo(grpc_labview::LVProtoParser* parser, grpc_labview::LStrHandle* outbuffer)
{
FileDescriptorProto fProto;
parser->m_FileDescriptor->CopyTo(&fProto);
std::string output;
fProto.SerializeToString(&output);
grpc_labview::SetLVString(outbuffer, output);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVGetFields(Descriptor* descriptor, grpc_labview::LV1DArrayHandle* fields)
{
if (descriptor == nullptr)
{
return -1;
}
auto count = descriptor->field_count();
auto elementSize = sizeof(FieldDescriptor*);
if (NumericArrayResize(grpc_labview::GetTypeCodeForSize(elementSize), 1, fields, count * elementSize) != 0)
{
return -3;
}
(**fields)->cnt = count;
auto fieldElements = (**fields)->bytes<const FieldDescriptor*>();
for (int x = 0; x < count; ++x)
{
fieldElements[x] = descriptor->field(x);
}
return 0;
}
std::string GetEnumNames(google::protobuf::EnumDescriptor* field);
//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int LVFieldInfo(FieldDescriptor* field, grpc_labview::MessageFieldCluster* info)
{
if (field == nullptr)
{
return -1;
}
int error = 0;
switch (field->type())
{
case FieldDescriptor::TYPE_DOUBLE:
info->type = 2;
break;
case FieldDescriptor::TYPE_FLOAT:
info->type = 1;
break;
case FieldDescriptor::TYPE_INT64:
info->type = 6;
break;
case FieldDescriptor::TYPE_UINT64:
info->type = 8;
break;
case FieldDescriptor::TYPE_INT32:
info->type = 0;
break;
case FieldDescriptor::TYPE_UINT32:
info->type = 7;
break;
case FieldDescriptor::TYPE_ENUM:
info->type = 9;
break;
case FieldDescriptor::TYPE_BOOL:
info->type = 3;
break;
case FieldDescriptor::TYPE_STRING:
info->type = 4;
break;
case FieldDescriptor::TYPE_BYTES:
info->type = 10;
break;
case FieldDescriptor::TYPE_MESSAGE:
info->type = 5;
break;
case FieldDescriptor::TYPE_FIXED64:
info->type = 11;
break;
case FieldDescriptor::TYPE_FIXED32:
info->type = 12;
break;
case FieldDescriptor::TYPE_SFIXED32:
info->type = 14;
break;
case FieldDescriptor::TYPE_SFIXED64:
info->type = 13;
break;
case FieldDescriptor::TYPE_SINT32:
info->type = 16;
break;
case FieldDescriptor::TYPE_SINT64:
info->type = 15;
break;
}
if (field->type() == FieldDescriptor::TYPE_MESSAGE)
{
SetLVString(&info->embeddedMessage, grpc_labview::TransformMessageName(field->message_type()->full_name()));
}
if (field->type() == FieldDescriptor::TYPE_ENUM)
{
SetLVString(&info->embeddedMessage, grpc_labview::TransformMessageName(field->enum_type()->full_name()));
}
SetLVString(&info->fieldName, field->name());
info->protobufIndex = field->number();
info->isRepeated = field->is_repeated();
info->isInOneof = (field->real_containing_oneof() != nullptr);
if (info->isInOneof)
{
SetLVString(&info->oneofContainerName, grpc_labview::TransformMessageName(field->real_containing_oneof()->full_name()));
}
if (info->type == 99)
{
error = -2;
}
return error;
}
LIBRARY_EXPORT int GetEnumInfo(EnumDescriptor* enumDescriptor, grpc_labview::EnumFieldCluster* info)
{
if (enumDescriptor == nullptr)
{
return -1;
}
SetLVString(&info->name, grpc_labview::TransformMessageName(enumDescriptor->full_name()));
SetLVString(&info->typeUrl, enumDescriptor->full_name());
SetLVString(&info->enumValues, GetEnumNames(enumDescriptor));
info->isAliasAllowed = (enumDescriptor->options().has_allow_alias() && enumDescriptor->options().allow_alias());
return 0;
}
std::string GetEnumNames(google::protobuf::EnumDescriptor* enumDescriptor)
{
int enumValueCount = enumDescriptor->value_count();
std::string enumNames = "";
for (int i = 0; i < enumValueCount; i++)
{
std::string enumVal = enumDescriptor->value(i)->name() + "=" + std::to_string(enumDescriptor->value(i)->number());
enumNames += enumVal + ((i < enumValueCount - 1) ? ";" : "");
}
return enumNames;
}