Skip to content

Commit

Permalink
Merge pull request #8 from Yasara123/grpc-support
Browse files Browse the repository at this point in the history
Add property file for standard proto files
  • Loading branch information
daneshk authored Feb 27, 2018
2 parents 08ba8db + dc98314 commit f95af7b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import org.slf4j.LoggerFactory;
import org.wso2.ballerinalang.grpc.tool.exception.BalGenToolException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -118,18 +121,23 @@ public void execute() {
}
StringBuilder msg = new StringBuilder("Successfully generated initial files." + NEW_LINE_CHARACTER);
ClassLoader classLoader = this.getClass().getClassLoader();
// TODO: 2/27/18 get to property file
String[] protoFiles = {"any.proto", "api.proto", "descriptor.proto", "duration.proto", "empty.proto",
"field_mask.proto", "source_context.proto", "struct.proto", "timestamp.proto", "type.proto",
"wrappers.proto", "compiler/plugin.proto"};
List<String> protoFiles = readProperties(classLoader);
try {
File targetFile = new File("google/protobuf/compiler/plugin.proto");
File parent = targetFile.getParentFile().getParentFile().getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
File parent1 = targetFile.getParentFile();
File parent2 = targetFile.getParentFile().getParentFile();
File parent3 = targetFile.getParentFile().getParentFile().getParentFile();
if (!parent1.exists() && !parent1.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent1);
}
if (!parent2.exists() && !parent2.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent2);
}
if (!parent3.exists() && !parent3.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent3);
}
for (String file : protoFiles) {
exportResource("google/protobuf/" + file, classLoader);
exportResource(file, classLoader);
}

} catch (Exception e) {
Expand Down Expand Up @@ -181,10 +189,10 @@ static String exportResource(String resourceName, ClassLoader classLoader) throw
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
} catch (Exception ex) {
throw ex;
} finally {
assert stream != null;
stream.close();
assert resStreamOut != null;
resStreamOut.close();
}
return jarFolder + resourceName;
Expand Down Expand Up @@ -312,4 +320,34 @@ public void setParentCmdParser(JCommander parentCmdParser) {
public void setSelfCmdParser(JCommander selfCmdParser) {

}

private List<String> readProperties(ClassLoader classLoader) {
InputStream initialStream;
List<String> protoFilesList = new ArrayList<>();
try {
initialStream = classLoader.getResourceAsStream("standardProtos.properties");
if (initialStream == null) {
throw new Exception("Cannot get resource standardProtos.properties from Jar file.");
}
try {
String fileName;
BufferedReader reader = new BufferedReader(new InputStreamReader(initialStream));
while ((fileName = reader.readLine()) != null) {
protoFilesList.add(fileName);
}

} finally {
try {
initialStream.close();
} catch (Throwable e) {
LOG.error("", e);
}
}
} catch (Exception e) {
LOG.error("", e);
}
return protoFilesList;
}
}


12 changes: 12 additions & 0 deletions misc/grpc-ballerina/src/main/resources/standardProtos.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
google/protobuf/any.proto
google/protobuf/api.proto
google/protobuf/descriptor.proto
google/protobuf/duration.proto
google/protobuf/empty.proto
google/protobuf/field_mask.proto
google/protobuf/source_context.proto
google/protobuf/struct.proto
google/protobuf/timestamp.proto
google/protobuf/type.proto
google/protobuf/wrappers.proto
google/protobuf/compiler/plugin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static org.ballerinalang.net.grpc.builder.BalGenConstants.NEW_LINE_CHARACTER;
import static org.ballerinalang.net.grpc.builder.BalGenConstants.SERVICE_INDEX;
import static org.ballerinalang.net.grpc.builder.utils.BalGenerationUtils.attributeListToMap;
import static org.ballerinalang.net.grpc.builder.utils.BalGenerationUtils.getMappingBalType;
import static org.ballerinalang.net.grpc.builder.utils.BalGenerationUtils.getTypeName;
import static org.ballerinalang.net.grpc.builder.utils.BalGenerationUtils.writeFile;
Expand Down Expand Up @@ -68,22 +66,15 @@ public void build() {
List<DescriptorProtos.DescriptorProto> messageTypeList = fileDescriptorSet.getMessageTypeList();
List<DescriptorProtos.MethodDescriptorProto> methodList = fileDescriptorSet
.getService(SERVICE_INDEX).getMethodList();
Map<String, DescriptorProtos.DescriptorProto> descriptorProtoMap = attributeListToMap(fileDescriptorSet
.getMessageTypeList());

String descriptorMapString = constantBuilder.buildMap();
String descriptorKey = constantBuilder.buildKey();
StringBuilder streamingActionList = new StringBuilder();
StringBuilder blockingActionList = new StringBuilder();
int i = 0;
String methodName = null;
String methodName;
String reqMessageName;
String resMessageName;
String methodID = null;
String reqStructFieldName = null;
String reqStructFieldType = null;
String resStructFieldName = null;
String resStructFieldType = null;
String methodID;
ActionBuilder actionBuilder;
for (DescriptorProtos.MethodDescriptorProto methodDescriptorProto : methodList) {
MethodDescriptor.MethodType methodType = MessageUtil.getMethodType(methodDescriptorProto);
Expand All @@ -96,37 +87,8 @@ public void build() {
.getName() + "/" + methodName;
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
if ((descriptorProtoMap.get(typeIn) != null) && (descriptorProtoMap.get(typeOut) != null) &&
(descriptorProtoMap.get(typeIn).getFieldList().size() != 0) &&
(descriptorProtoMap.get(typeOut).getFieldList().size() != 0)) {
reqStructFieldName = descriptorProtoMap.get(typeIn).getFieldList().get(0).getName();
reqStructFieldType = getTypeName(descriptorProtoMap.get(typeIn).getFieldList().get(0)
.getType().getNumber());
resStructFieldName = descriptorProtoMap.get(typeOut).getFieldList().get(0)
.getName();
resStructFieldType = getTypeName(descriptorProtoMap.get(typeOut).getFieldList().get(0)
.getType().getNumber());
} else if ((descriptorProtoMap.get(typeOut) != null) &&
(descriptorProtoMap.get(typeOut).getFieldList().size() != 0)) {
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
resStructFieldName = descriptorProtoMap.get(typeOut).getFieldList().get(0)
.getName();
resStructFieldType = getTypeName(descriptorProtoMap.get(typeOut).getFieldList().get(0)
.getType().getNumber());
} else if ((descriptorProtoMap.get(typeIn) != null) &&
(descriptorProtoMap.get(typeIn).getFieldList().size() != 0)) {
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
reqStructFieldName = descriptorProtoMap.get(typeIn).getFieldList().get(0).getName();
reqStructFieldType = getTypeName(descriptorProtoMap.get(typeIn).getFieldList().get(0)
.getType().getNumber());
} else {
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
}
actionBuilder = new ActionBuilder(methodName, reqMessageName, resMessageName
, methodID, reqStructFieldName, reqStructFieldType, resStructFieldName, resStructFieldType,
, methodID,
methodType);
if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
blockingActionList = blockingActionList.append(NEW_LINE_CHARACTER).append(actionBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,20 @@ public class ActionBuilder {
private String reqMessageName;
private String resMessageName;
private String methodID;
private String reqStructFieldName;
private String reqStructFieldType;
private String resStructFieldName;
private String resStructFieldType;
private MethodDescriptor.MethodType methodType;


public ActionBuilder(String methodName, String reqMessageName, String resMessageName, String methodID,
String reqStructFieldName, String reqStructFieldType,
String resStructFieldName, String resStructFieldType, MethodDescriptor.MethodType isStream) {
MethodDescriptor.MethodType isStream) {
this.methodName = methodName;
this.reqMessageName = reqMessageName;
this.resMessageName = resMessageName;
this.methodID = methodID;
this.reqStructFieldName = reqStructFieldName;
this.reqStructFieldType = reqStructFieldType;
this.resStructFieldName = resStructFieldName;
this.resStructFieldType = resStructFieldType;
this.methodType = isStream;
}


public String build() {
// String templPrt2 = "";
String unaryTemplate =
" action %s (%s req) (%s, error) {" + NEW_LINE_CHARACTER +
" %s" + NEW_LINE_CHARACTER +
Expand Down Expand Up @@ -96,41 +86,6 @@ public String build() {
" }" + NEW_LINE_CHARACTER +
" return null;" + NEW_LINE_CHARACTER +
" }";
// if (reqStructFieldName != null) {
// String template2 =
// "var request, err = (%s)req;" + NEW_LINE_CHARACTER +
// " any value;" + NEW_LINE_CHARACTER +
// " if (err != null) {" + NEW_LINE_CHARACTER +
// " %s reqObj = {};" + NEW_LINE_CHARACTER +
// " var %s, err2 = (%s)req;" + NEW_LINE_CHARACTER +
// " if (err2 != null) {" + NEW_LINE_CHARACTER +
// " error e = {msg:err2.msg};" + NEW_LINE_CHARACTER +
// " return null, e;" + NEW_LINE_CHARACTER +
// " }" + NEW_LINE_CHARACTER +
// " reqObj.%s = %s;" + NEW_LINE_CHARACTER +
// " value = reqObj;" + NEW_LINE_CHARACTER +
// " } else {" + NEW_LINE_CHARACTER +
// " value = request;" + NEW_LINE_CHARACTER +
// " }" + NEW_LINE_CHARACTER;
// templPrt1 = String.format(template2, reqMessageName, reqMessageName, reqStructFieldName,
// reqStructFieldType, reqStructFieldName, reqStructFieldName);
// }
// if (reqStructFieldName == null) {
// templPrt1 = "";
// }
// templPrt2 = String.format("var response, err2 = (%s)res;" + NEW_LINE_CHARACTER +
// " if (err2 != null) {" + NEW_LINE_CHARACTER +
// " error e = {msg:err2.msg};" + NEW_LINE_CHARACTER +
// " return null, e;" + NEW_LINE_CHARACTER +
// " }" + NEW_LINE_CHARACTER, resMessageName);
// String connectorIn, executeIn;
// if (!BalGenerationUtils.isStructType(reqMessageName)) {
// connectorIn = reqMessageName;
// executeIn = "req";
// } else {
// connectorIn = "any";
// executeIn = "value";
// }
if (methodType.equals(MethodDescriptor.MethodType.SERVER_STREAMING)) {
return String.format(serverStreamTemplate, methodName, reqMessageName, "", "req", methodID);
} else if (methodType.equals(MethodDescriptor.MethodType.CLIENT_STREAMING) ||
Expand Down

0 comments on commit f95af7b

Please sign in to comment.