Search in sources :

Example 1 with ServiceConfiguration

use of org.ballerinalang.net.grpc.config.ServiceConfiguration in project ballerina by ballerina-lang.

the class ServiceProtoBuilder method process.

@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
    for (AnnotationAttachmentNode annotationNode : annotations) {
        if (ANN_MESSAGE_LISTENER.equals(annotationNode.getAnnotationName().getValue())) {
            return;
        }
    }
    try {
        File fileDefinition = ServiceProtoUtils.generateProtoDefinition(serviceNode);
        ServiceConfiguration serviceConfig = ServiceProtoUtils.getServiceConfiguration(serviceNode);
        ServiceProtoUtils.writeServiceFiles(fileDefinition, serviceNode.getName().getValue(), serviceConfig.isGenerateClientConnector());
    } catch (GrpcServerException e) {
        dlog.logDiagnostic(Diagnostic.Kind.WARNING, serviceNode.getPosition(), e.getMessage());
    }
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) File(org.ballerinalang.net.grpc.proto.definition.File) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 2 with ServiceConfiguration

use of org.ballerinalang.net.grpc.config.ServiceConfiguration in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getServiceConfiguration.

static ServiceConfiguration getServiceConfiguration(ServiceNode serviceNode) {
    String rpcEndpoint = null;
    boolean clientStreaming = false;
    boolean serverStreaming = false;
    boolean generateClientConnector = false;
    for (AnnotationAttachmentNode annotationNode : serviceNode.getAnnotationAttachments()) {
        if (!ServiceProtoConstants.ANN_SERVICE_CONFIG.equals(annotationNode.getAnnotationName().getValue())) {
            continue;
        }
        if (annotationNode.getExpression() instanceof BLangRecordLiteral) {
            List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) annotationNode.getExpression()).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue attributeNode : attributes) {
                String attributeName = attributeNode.getKey().toString();
                String attributeValue = attributeNode.getValue() != null ? attributeNode.getValue().toString() : null;
                switch(attributeName) {
                    case ServiceProtoConstants.SERVICE_CONFIG_RPC_ENDPOINT:
                        {
                            rpcEndpoint = attributeValue != null ? attributeValue : null;
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_CLIENT_STREAMING:
                        {
                            clientStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_SERVER_STREAMING:
                        {
                            serverStreaming = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    case ServiceProtoConstants.SERVICE_CONFIG_GENERATE_CLIENT:
                        {
                            generateClientConnector = attributeValue != null && Boolean.parseBoolean(attributeValue);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }
    }
    return new ServiceConfiguration(rpcEndpoint, clientStreaming, serverStreaming, generateClientConnector);
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 3 with ServiceConfiguration

use of org.ballerinalang.net.grpc.config.ServiceConfiguration in project ballerina by ballerina-lang.

the class ServiceProtoUtils method generateProtoDefinition.

static File generateProtoDefinition(ServiceNode serviceNode) throws GrpcServerException {
    // Protobuf file definition builder.
    String packageName = serviceNode.getPosition().getSource().getPackageName();
    File.Builder fileBuilder;
    if (!".".equals(packageName)) {
        fileBuilder = File.newBuilder(serviceNode.getName() + ServiceProtoConstants.PROTO_FILE_EXTENSION).setSyntax(ServiceProtoConstants.PROTOCOL_SYNTAX).setPackage(serviceNode.getPosition().getSource().getPackageName());
    } else {
        fileBuilder = File.newBuilder(serviceNode.getName() + ServiceProtoConstants.PROTO_FILE_EXTENSION).setSyntax(ServiceProtoConstants.PROTOCOL_SYNTAX);
    }
    ServiceConfiguration serviceConfig = getServiceConfiguration(serviceNode);
    Service serviceDefinition;
    if (serviceConfig.getRpcEndpoint() != null && (serviceConfig.isClientStreaming())) {
        serviceDefinition = getStreamingServiceDefinition(serviceNode, serviceConfig, fileBuilder);
    } else {
        serviceDefinition = getUnaryServiceDefinition(serviceNode, fileBuilder);
    }
    fileBuilder.setService(serviceDefinition);
    return fileBuilder.build();
}
Also used : ServiceConfiguration(org.ballerinalang.net.grpc.config.ServiceConfiguration) Service(org.ballerinalang.net.grpc.proto.definition.Service) File(org.ballerinalang.net.grpc.proto.definition.File)

Aggregations

ServiceConfiguration (org.ballerinalang.net.grpc.config.ServiceConfiguration)3 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)2 File (org.ballerinalang.net.grpc.proto.definition.File)2 GrpcServerException (org.ballerinalang.net.grpc.exception.GrpcServerException)1 Service (org.ballerinalang.net.grpc.proto.definition.Service)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1