Search in sources :

Example 1 with ClientStruct

use of org.ballerinalang.net.grpc.builder.components.ClientStruct 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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClientStruct(org.ballerinalang.net.grpc.builder.components.ClientStruct) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) DescriptorProtos(com.google.protobuf.DescriptorProtos) IOException(java.io.IOException) MethodDescriptor(io.grpc.MethodDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) DescriptorBuilder(org.ballerinalang.net.grpc.builder.components.DescriptorBuilder) File(java.io.File) ClientBuilder(org.ballerinalang.net.grpc.builder.components.ClientBuilder)

Aggregations

DescriptorProtos (com.google.protobuf.DescriptorProtos)1 MethodDescriptor (io.grpc.MethodDescriptor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ClientBuilder (org.ballerinalang.net.grpc.builder.components.ClientBuilder)1 ClientStruct (org.ballerinalang.net.grpc.builder.components.ClientStruct)1 DescriptorBuilder (org.ballerinalang.net.grpc.builder.components.DescriptorBuilder)1 BalGenerationException (org.ballerinalang.net.grpc.exception.BalGenerationException)1