use of org.ballerinalang.net.grpc.builder.components.DescriptorBuilder in project ballerina by ballerina-lang.
the class BallerinaFileBuilder method build.
public void build() {
try {
InputStream targetStream = new ByteArrayInputStream(rootDescriptor);
DescriptorProtos.FileDescriptorProto fileDescriptorSet = DescriptorProtos.FileDescriptorProto.parseFrom(targetStream);
List<DescriptorProtos.DescriptorProto> messageTypeList = fileDescriptorSet.getMessageTypeList();
List<DescriptorProtos.MethodDescriptorProto> methodList = fileDescriptorSet.getService(SERVICE_INDEX).getMethodList();
String methodName;
String reqMessageName;
String resMessageName;
String methodID;
String packageName = EMPTY_STRING.equals(fileDescriptorSet.getPackage()) ? BalGenConstants.DEFAULT_PACKAGE : fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + BalGenConstants.DEFAULT_PACKAGE;
ClientBuilder clientStubBal = new ClientBuilder(packageName, fileDescriptorSet.getService(SERVICE_INDEX).getName());
DescriptorBuilder descriptorBuilder;
String protoPackage = fileDescriptorSet.getPackage();
if (EMPTY_STRING.equals(protoPackage)) {
descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getName(), clientStubBal);
} else {
descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getPackage() + "." + fileDescriptorSet.getName(), clientStubBal);
}
descriptorBuilder.setRootDescriptor(rootDescriptor);
descriptorBuilder.buildMap();
descriptorBuilder.buildKey();
for (DescriptorProtos.MethodDescriptorProto methodDescriptorProto : methodList) {
MethodDescriptor.MethodType methodType = MessageUtils.getMethodType(methodDescriptorProto);
String[] outputTypes = methodDescriptorProto.getOutputType().split(PACKAGE_SEPARATOR_REGEX);
String typeOut = outputTypes[outputTypes.length - 1];
String[] inputTypes = methodDescriptorProto.getInputType().split(PACKAGE_SEPARATOR_REGEX);
String typeIn = inputTypes[inputTypes.length - 1];
methodName = methodDescriptorProto.getName();
if (!EMPTY_STRING.equals(fileDescriptorSet.getPackage())) {
methodID = fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
} else {
methodID = fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
}
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
ActionBuilder.build(methodName, reqMessageName, resMessageName, methodID, methodType, clientStubBal);
}
for (DescriptorProtos.DescriptorProto descriptorProto : messageTypeList) {
String[] attributesNameArr = new String[descriptorProto.getFieldCount()];
String[] attributesTypeArr = new String[descriptorProto.getFieldCount()];
int j = 0;
for (DescriptorProtos.FieldDescriptorProto fieldDescriptorProto : descriptorProto.getFieldList()) {
attributesNameArr[j] = fieldDescriptorProto.getName();
attributesTypeArr[j] = !fieldDescriptorProto.getTypeName().equals("") ? fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX)[fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX).length - 1] : getTypeName(fieldDescriptorProto.getType().getNumber());
j++;
}
clientStubBal.addStruct(descriptorProto.getName(), attributesNameArr, attributesTypeArr);
}
StubBuilder.build(clientStubBal, clientStubBal.isFunctionsUnaryNotEmpty());
ClientStruct sampleClient = new ClientStruct(clientStubBal.isFunctionsStremingNotEmpty(), clientStubBal.isFunctionsUnaryNotEmpty(), fileDescriptorSet.getService(SERVICE_INDEX).getName(), packageName);
if (this.balOutPath == null) {
String path = balOutPathGenerator(packageName + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName());
writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
} else {
String path = this.balOutPath + FILE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName();
writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
File sampleFile = new File(path + SAMPLE_FILE_PRIFIX);
if (!sampleFile.isFile()) {
Files.createFile(Paths.get(sampleFile.getAbsolutePath()));
}
writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
}
} catch (IOException e) {
throw new BalGenerationException("Error while generating .bal file.", e);
}
}
Aggregations