-
Notifications
You must be signed in to change notification settings - Fork 5
/
grpcParser.js
101 lines (89 loc) · 2.52 KB
/
grpcParser.js
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
function loadProtoFile(absolutePath){
var fs = require('fs');
var protoFile = fs.readFileSync(absolutePath, 'utf8');
return protoFile;
}
function removeCommentsInProtoFile(protoFile){
var splittedProtoFile = protoFile.split("\n");
var splittedProtoFileTransformed = [];
splittedProtoFile.forEach(function(eachLine){
var comment = eachLine.match(/^\s*\/\//);
if (comment){
return;
}
comment = eachLine.match(/^([^\/\/]*)\/\/.*/);
if (comment){
splittedProtoFileTransformed.push(comment[1]);
} else {
splittedProtoFileTransformed.push(eachLine);
}
});
return splittedProtoFileTransformed.join("\n");
}
function restructureNestedMessages(protoDesc){
// TODO
}
function restructureMessages(protoDesc){
protoDesc.messages.forEach(function(eachMessage){
eachMessage.nestedMessages = [];
eachMessage.parameters = [];
eachMessage.enums = [];
eachMessage.content.forEach(function(eachContent){
if (eachContent.nestedMessage != undefined){
eachMessage.nestedMessages.push(eachContent.nestedMessage);
}
if (eachContent.parameter != undefined){
eachMessage.parameters.push(eachContent.parameter);
}
if (eachContent.enumDef != undefined){
eachMessage.enums.push(eachContent.enumDef);
}
delete eachMessage["content"];
});
eachMessage.getEnumWithName = getEnumWithName;
});
return protoDesc;
}
function getEnumWithName(enumName){
var result;
this.enums.forEach(function(eachEnum){
if (eachEnum.name === enumName){
result = eachEnum;
}
});
return result;
}
function getMethodWithName(methodName){
var result;
this.services.forEach(function(eachService){
eachService.methods.forEach(function(eachMethod){
if (eachMethod.name === methodName){
result = eachMethod;
}
});
});
return result;
}
function getMessageOfType(messageType){
var result;
this.messages.forEach(function(eachMessage){
if (eachMessage.name === messageType){
result = eachMessage;
}
});
return result;
}
function parse(absolutePathToProtoFile){
var parser = require('./grpcPegParser.js');
var protoFile = loadProtoFile(absolutePathToProtoFile);
protoFile = removeCommentsInProtoFile(protoFile);
var protoDesc = parser.parse(protoFile);
restructureMessages(protoDesc);
restructureNestedMessages(protoDesc);
protoDesc.getMessageOfType = getMessageOfType;
protoDesc.getMethodWithName = getMethodWithName;
return protoDesc;
}
module.exports = {
parse: parse
};