-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotoc-gen-bsoa-java.py
More file actions
187 lines (148 loc) · 7.11 KB
/
protoc-gen-bsoa-java.py
File metadata and controls
187 lines (148 loc) · 7.11 KB
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
#!/usr/bin/env python
import sys
import itertools
from google.protobuf.descriptor_pb2 import DescriptorProto, EnumDescriptorProto, ServiceDescriptorProto, \
MethodDescriptorProto
from google.protobuf.compiler import plugin_pb2 as plugin
def generate_code(request, response):
for proto_file in request.proto_file:
# print >> log, ">>>options", type(proto_file.options)
# print >> log, ">>>options", proto_file.options
java_package = proto_file.options.java_package
java_outer_classname = proto_file.options.java_outer_classname
java_multiple_files = proto_file.options.java_multiple_files
if java_package is None or java_package == "":
java_package = proto_file.package
if java_outer_classname is None or java_outer_classname == "":
java_outer_classname = proto_file.name[0:proto_file.name.rindex(".")].capitalize()
if java_multiple_files is None:
java_multiple_files = False
# print >> log, ">>>java_package ", type(java_package), java_package
# print >> log, ">>>java_outer_classname ", type(java_outer_classname), java_outer_classname
# print >> log, ">>>java_multiple_files ", type(java_multiple_files), java_multiple_files
# Parse request
for item, package in traverse(proto_file):
if isinstance(item, ServiceDescriptorProto):
# print >> log, "=========Found Service==========", item
java_file_name, java_file_content \
= build_service(proto_file, proto_file.package, java_package, java_outer_classname, java_multiple_files, item)
# print >> log, "==start gen file", java_file_name
service_file = response.file.add()
service_file.name = java_file_name
service_file.content = java_file_content
# elif isinstance(item, DescriptorProto):
# print >> log, "=========desc==========", item
# data.update({
# 'type': 'Message',
# 'properties': [{'name': f.name, 'type': int(f.type)}
# for f in item.field]
# })
#
# elif isinstance(item, EnumDescriptorProto):
# print >> log, "=========enum==========", item
# data.update({
# 'type': 'Enum',
# 'values': [{'name': v.name, 'value': v.number}
# for v in item.value]
# })
def build_service(proto_file, proto_package, java_package, java_outer_classname, java_multiple_files, sdp_item):
"""
proto_file:
proto_package : str
java_package: str
java_outer_classname: str
java_multiple_files: boolean
sdp_item: ServiceDescriptorProtoItem
"""
content_lines = []
imports = []
body = []
content_lines.append("// Generated by the protocol buffer compiler. DO NOT EDIT!")
content_lines.append("// source: " + proto_file.name)
content_lines.append("")
content_lines.append("package " + java_package + ";")
content_lines.append("")
body.append("public interface " + sdp_item.name + " {")
for rpc in sdp_item.method:
if isinstance(rpc, MethodDescriptorProto):
# print >> log, "====Found method", rpc
# print >> log, ""
ridx = rpc.input_type.rindex('.')
sidx = rpc.output_type.rindex('.')
request_package = rpc.input_type[1:ridx]
response_package = rpc.output_type[1:sidx]
request_class_name = rpc.input_type[ridx+1:]
response_class_name = rpc.output_type[ridx+1:]
# print >> log, "=========request==========", rpc.input_type
# print >> log, "=========rresponse==========", rpc.output_type
# print >> log, "=========request_package==========", request_package
# print >> log, "=========response_package==========", response_package
# print >> log, "=========request_class_name==========", request_class_name
# print >> log, "=========response_class_name==========", response_class_name
# print >> log, "=========proto_package==========", proto_package
# print >> log, "=========java_package==========", java_package
if request_package == proto_package:
request_package = ""
else:
request_package = java_package + "."
if response_package == proto_package:
response_package = ""
else:
response_package = java_package + "."
if(java_multiple_files) :
request = request_package + request_class_name
response = response_package + response_class_name
body.append(" public " + response + " " + rpc.name + "(" + request + " req);")
else:
request = request_package + java_outer_classname + "." + request_class_name
response = response_package + java_outer_classname + "." + response_class_name
body.append(" public " + response + " " + rpc.name + "(" + request + " req);")
body.append("}")
content_lines.extend(imports)
content_lines.extend(body)
java_file_name = java_package.replace(".", "/") + "/" + sdp_item.name + ".java"
java_file_content = ""
for s in content_lines:
java_file_content += s + "\n"
# print >> log, s
return java_file_name, java_file_content
def traverse(proto_file):
def _traverse(package, items):
for item in items:
# yield item, package
if isinstance(item, ServiceDescriptorProto):
# print >> log, "=======find ServiceDescriptorProto=======", item
yield item, package
# if isinstance(item, DescriptorProto):
# for enum in item.enum_type:
# yield enum, package
#
# for nested in item.nested_type:
# nested_package = package + item.name
#
# for nested_item in _traverse(nested, nested_package):
# yield nested_item, nested_package
return itertools.chain(
_traverse(proto_file.package, proto_file.service),
# _traverse(proto_file.package, proto_file.enum_type),
# _traverse(proto_file.package, proto_file.message_type),
)
if __name__ == '__main__':
# log = open("../build/pb.log", 'w+')
# Read request message from stdin
data = sys.stdin.read()
# print >> log, "=======data=======", data
# Parse request
request = plugin.CodeGeneratorRequest()
request.ParseFromString(data)
# print >> log, "=======request=======", request
# Create response
response = plugin.CodeGeneratorResponse()
# Generate code
generate_code(request, response)
# print >> log, "=======response=======", response
# Serialise response message
output = response.SerializeToString()
# print >> log, "=======output=======", output
# Write to stdout
sys.stdout.write(output)