Skip to content

Commit

Permalink
Merge pull request #24 from dilanSachi/bug-fix
Browse files Browse the repository at this point in the history
Add descriptorMap to generated stub files and service config
  • Loading branch information
dilanSachi committed Jun 21, 2023
2 parents 1c641a1 + 0fec145 commit 5d6f149
Show file tree
Hide file tree
Showing 50 changed files with 948 additions and 145 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- [Add descriptor map to `grpc:Descriptor` and stub initialization](https://github.com/ballerina-platform/ballerina-standard-library/issues/4555)

## [0.1.1] - 2023-06-01

### Fixed

- [Fix required input error when `help` command is provided](https://github.com/ballerina-platform/ballerina-standard-library/issues/4446)

## [0.1.0] - 2023-02-24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public class GrpcConstants {
ANN_DESCRIPTOR);
public static final BString ANN_PROTOBUF_DESCRIPTOR = StringUtils.fromString(PROTOCOL_STRUCT_PACKAGE_PROTOBUF +
":" + ANN_DESCRIPTOR);
public static final String DESCRIPTOR_MAP = "_DESCRIPTOR_MAP";
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import static io.ballerina.protoc.builder.balgen.BalGenConstants.GOOGLE_STANDARD_LIB;
import static io.ballerina.protoc.builder.balgen.BalGenConstants.PACKAGE_SEPARATOR;
import static io.ballerina.protoc.builder.syntaxtree.utils.ClientSampleSyntaxTreeUtils.generateSyntaxTreeForClientSample;
import static io.ballerina.protoc.builder.syntaxtree.utils.CommonUtils.removeStandardImports;
import static io.ballerina.protoc.protobuf.utils.BalFileGenerationUtils.delete;
import static io.ballerina.protoc.protobuf.utils.BalFileGenerationUtils.handleProcessExecutionErrors;
import static io.ballerina.protoc.protobuf.utils.BalFileGenerationUtils.runProcess;
Expand Down Expand Up @@ -266,6 +267,8 @@ private void computeSourceContent(byte[] descriptor, String mode, boolean isRoot
stubFileObject.setRootDescriptor(stubRootDescriptor);
}

List<String> fileImports = removeStandardImports(stubFileObject.getImportList());

int serviceIndex = 0;
for (ServiceStub serviceStub : stubFileObject.getStubList()) {
if (BalGenConstants.GRPC_SERVICE.equals(mode)) {
Expand All @@ -276,7 +279,8 @@ private void computeSourceContent(byte[] descriptor, String mode, boolean isRoot
SyntaxTreeGenerator.generateSyntaxTreeForServiceSample(
serviceStub,
serviceIndex == 0,
stubFileObject.getFileName()
stubFileObject.getFileName(),
fileImports
),
serviceFilePath
);
Expand All @@ -297,7 +301,8 @@ private void computeSourceContent(byte[] descriptor, String mode, boolean isRoot
} else {
stubFilePath = generateOutputFile(this.balOutPath, filename + BalGenConstants.STUB_FILE_PREFIX);
}
writeOutputFile(SyntaxTreeGenerator.generateSyntaxTree(stubFileObject, isRoot, mode), stubFilePath);
writeOutputFile(SyntaxTreeGenerator.generateSyntaxTree(stubFileObject, isRoot, mode, fileImports),
stubFilePath);
} catch (IOException e) {
throw new CodeBuilderException("IO Error while reading proto file descriptor. " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Locale;
import java.util.Map;

import static io.ballerina.protoc.GrpcConstants.REGEX_DOT_SEPERATOR;

/**
* Message Definition bean class.
*
Expand Down Expand Up @@ -195,7 +197,7 @@ private static String getFieldType(String typeName, String packageName) {
if (!packageName.isBlank()) {
typeName = typeName.replaceFirst("^\\." + packageName, "");
}
String[] types = typeName.split("\\.");
String[] types = typeName.split(REGEX_DOT_SEPERATOR);
for (int i = 1; i < types.length; i++) {
if (fieldType.toString().isBlank()) {
fieldType.append(types[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class StubFile extends AbstractStub {
private final List<ServiceStub> stubList = new ArrayList<>();
private final List<String> importList = new ArrayList<>();
private final String fileName;

public StubFile(String fileName) {
this.fileName = fileName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.ballerina.protoc.builder.syntaxtree.components.Function;
import io.ballerina.protoc.builder.syntaxtree.components.Imports;
import io.ballerina.protoc.builder.syntaxtree.components.Listener;
import io.ballerina.protoc.builder.syntaxtree.components.Map;
import io.ballerina.protoc.builder.syntaxtree.components.Service;
import io.ballerina.protoc.builder.syntaxtree.components.Type;
import io.ballerina.protoc.builder.syntaxtree.constants.SyntaxTreeConstants;
Expand All @@ -53,19 +54,25 @@
import java.util.Set;
import java.util.TreeSet;

import static io.ballerina.protoc.GrpcConstants.DESCRIPTOR_MAP;
import static io.ballerina.protoc.GrpcConstants.ORG_NAME;
import static io.ballerina.protoc.GrpcConstants.REGEX_DOT_SEPERATOR;
import static io.ballerina.protoc.MethodDescriptor.MethodType.BIDI_STREAMING;
import static io.ballerina.protoc.MethodDescriptor.MethodType.CLIENT_STREAMING;
import static io.ballerina.protoc.MethodDescriptor.MethodType.SERVER_STREAMING;
import static io.ballerina.protoc.builder.BallerinaFileBuilder.dependentValueTypeMap;
import static io.ballerina.protoc.builder.BallerinaFileBuilder.protofileModuleMap;
import static io.ballerina.protoc.builder.BallerinaFileBuilder.streamClassMap;
import static io.ballerina.protoc.builder.balgen.BalGenConstants.COLON;
import static io.ballerina.protoc.builder.balgen.BalGenConstants.PACKAGE_SEPARATOR;
import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getCheckExpressionNode;
import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getFieldAccessExpressionNode;
import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getImplicitNewExpressionNode;
import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getMethodCallExpressionNode;
import static io.ballerina.protoc.builder.syntaxtree.components.Literal.getStringLiteralNode;
import static io.ballerina.protoc.builder.syntaxtree.components.Statement.getCallStatementNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getErrorTypeDescriptorNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getMapTypeDescriptorNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getObjectFieldNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getOptionalTypeDescriptorNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getQualifiedNameReferenceNode;
Expand Down Expand Up @@ -103,7 +110,8 @@ private SyntaxTreeGenerator() {

}

public static SyntaxTree generateSyntaxTree(StubFile stubFile, boolean isRoot, String mode) {
public static SyntaxTree generateSyntaxTree(StubFile stubFile, boolean isRoot, String mode,
List<String> fileImports) {
Set<String> ballerinaImports = new TreeSet<>();
Set<String> protobufImports = new TreeSet<>();
Set<String> grpcStreamImports = new TreeSet<>();
Expand All @@ -119,10 +127,20 @@ public static SyntaxTree generateSyntaxTree(StubFile stubFile, boolean isRoot, S
"public ",
"string",
descriptorName,
stubFile.getRootDescriptor() == null ? "" : stubFile.getRootDescriptor()
getStringLiteralNode(stubFile.getRootDescriptor() == null ? "" : stubFile.getRootDescriptor())
);
moduleMembers = moduleMembers.add(descriptor.getConstantDeclarationNode());

if (isRoot && fileImports.size() > 0) {
Constant descriptorMap = new Constant(
"public ",
getMapTypeDescriptorNode(SYNTAX_TREE_VAR_STRING),
stubFile.getFileName().toUpperCase() + DESCRIPTOR_MAP,
generateDescMap(fileImports).getMappingConstructorExpressionNode()
);
moduleMembers = moduleMembers.add(descriptorMap.getConstantDeclarationNode());
}

if (stubFile.getMessageMap().size() > 0) {
ballerinaImports.add("protobuf");
}
Expand All @@ -144,7 +162,8 @@ public static SyntaxTree generateSyntaxTree(StubFile stubFile, boolean isRoot, S
getQualifiedNameReferenceNode("grpc", "AbstractClientEndpoint")));
client.addMember(getObjectFieldNode("private", new String[]{"final"},
getQualifiedNameReferenceNode("grpc", "Client"), "grpcClient"));
client.addMember(getInitFunction(stubFile.getFileName()).getFunctionDefinitionNode());
client.addMember(getInitFunction(stubFile.getFileName(), fileImports.size() > 0)
.getFunctionDefinitionNode());

for (Method method : service.getUnaryFunctions()) {
client.addMember(UnaryUtils.getUnaryFunction(method, stubFile.getFileName())
Expand Down Expand Up @@ -250,8 +269,23 @@ private static String generateDescriptorName(String stubFilename) {
return stubFilename.toUpperCase() + ROOT_DESCRIPTOR;
}

private static Map generateDescMap(List<String> importList) {
Map descMap = new Map();
for (String filename: importList) {
String importDescName = generateDescriptorName(filename.split(REGEX_DOT_SEPERATOR)[0].replace("/", "_"));
String protoFileName = filename.substring(0, filename.lastIndexOf(PACKAGE_SEPARATOR));
if (protofileModuleMap.containsKey(protoFileName)) {
String subModuleName = protofileModuleMap.get(protoFileName);
importDescName = subModuleName.substring(subModuleName.lastIndexOf(PACKAGE_SEPARATOR) + 1) + COLON
+ importDescName;
}
descMap.addField("\"" + filename + "\"", getSimpleNameReferenceNode(importDescName));
}
return descMap;
}

public static SyntaxTree generateSyntaxTreeForServiceSample(ServiceStub serviceStub, boolean addListener,
String fileName) {
String fileName, List<String> fileImports) {
NodeList<ModuleMemberDeclarationNode> moduleMembers = AbstractNodeFactory.createEmptyNodeList();
NodeList<ImportDeclarationNode> imports = NodeFactory.createEmptyNodeList();
ImportDeclarationNode importForGrpc = Imports.getImportDeclarationNode("ballerina", "grpc");
Expand All @@ -271,7 +305,7 @@ public static SyntaxTree generateSyntaxTreeForServiceSample(ServiceStub serviceS
Listener listener = new Listener(
"ep",
getQualifiedNameReferenceNode("grpc", "Listener"),
getImplicitNewExpressionNode(new String[]{"9090"}),
getImplicitNewExpressionNode("9090"),
false
);
moduleMembers = moduleMembers.add(listener.getListenerDeclarationNode());
Expand All @@ -286,6 +320,12 @@ public static SyntaxTree generateSyntaxTreeForServiceSample(ServiceStub serviceS
"value",
generateDescriptorName(fileName)
);
if (fileImports.size() > 0) {
grpcServiceDescriptor.addField(
"descMap",
fileName.toUpperCase() + DESCRIPTOR_MAP
);
}
service.addAnnotation(grpcServiceDescriptor.getAnnotationNode());

for (Method method : methodList) {
Expand Down Expand Up @@ -343,7 +383,7 @@ public static SyntaxTree generateSyntaxTreeForServiceSample(ServiceStub serviceS
return syntaxTree.modifyWith(modulePartNode);
}

public static Function getInitFunction(String fileName) {
public static Function getInitFunction(String fileName, boolean addDescMap) {
Function function = new Function("init");
function.addRequiredParameter(SYNTAX_TREE_VAR_STRING, "url");
function.addIncludedRecordParameter(
Expand All @@ -363,8 +403,16 @@ public static Function getInitFunction(String fileName) {
getMethodCallExpressionNode(
getFieldAccessExpressionNode("self", "grpcClient"),
"initStub",
"self",
generateDescriptorName(fileName))
addDescMap ? new String[] {
"self",
generateDescriptorName(fileName),
fileName.toUpperCase() + DESCRIPTOR_MAP
} :
new String[] {
"self",
generateDescriptorName(fileName)
}
)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
package io.ballerina.protoc.builder.syntaxtree.components;

import io.ballerina.compiler.syntax.tree.AbstractNodeFactory;
import io.ballerina.compiler.syntax.tree.BasicLiteralNode;
import io.ballerina.compiler.syntax.tree.ConstantDeclarationNode;
import io.ballerina.compiler.syntax.tree.ExpressionNode;
import io.ballerina.compiler.syntax.tree.NodeFactory;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TypeDescriptorNode;
import io.ballerina.protoc.builder.syntaxtree.constants.SyntaxTreeConstants;

import static io.ballerina.protoc.builder.syntaxtree.components.Literal.getStringLiteralNode;
import static io.ballerina.protoc.builder.syntaxtree.components.TypeDescriptor.getBuiltinSimpleNameReferenceNode;

/**
Expand All @@ -39,14 +38,21 @@ public class Constant {
private final Token constKeyWord = AbstractNodeFactory.createIdentifierToken("const ");
private final TypeDescriptorNode typeDescriptor;
private final Token variableName;
private final BasicLiteralNode initializer;
private final ExpressionNode initializer;
private final Token visibility;

public Constant(String visibilty, String type, String name, String value) {
visibility = AbstractNodeFactory.createIdentifierToken(visibilty);
public Constant(String visibility, String type, String name, ExpressionNode value) {
this.visibility = AbstractNodeFactory.createIdentifierToken(visibility);
typeDescriptor = getBuiltinSimpleNameReferenceNode(type);
variableName = AbstractNodeFactory.createIdentifierToken(name);
initializer = getStringLiteralNode(value);
initializer = value;
}

public Constant(String visibility, TypeDescriptorNode type, String name, ExpressionNode value) {
this.visibility = AbstractNodeFactory.createIdentifierToken(visibility);
typeDescriptor = type;
variableName = AbstractNodeFactory.createIdentifierToken(name);
initializer = value;
}

public ConstantDeclarationNode getConstantDeclarationNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.ballerina.protoc.builder.syntaxtree.components.Map;
import io.ballerina.protoc.builder.syntaxtree.components.VariableDeclaration;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -64,6 +65,13 @@
*/
public class CommonUtils {

private static final Set<String> STANDARD_IMPORTS = Set.of(
"google/protobuf/wrappers.proto", "google/protobuf/timestamp.proto", "google/protobuf/duration.proto",
"google/protobuf/any.proto", "google/protobuf/empty.proto", "google/protobuf/type.proto",
"google/protobuf/struct.proto", "google/protobuf/descriptor.proto", "google/protobuf/api.proto",
"google/protobuf/field_mask.proto", "google/protobuf/source_context.proto", "google/api/annotations.proto",
"google/api/http.proto", "ballerina/protobuf/descriptor.proto");

private CommonUtils() {

}
Expand Down Expand Up @@ -297,6 +305,16 @@ public static void addImports(StubFile stubFile, Set<String> ballerinaImports, S
}
}

public static List<String> removeStandardImports(List<String> importList) {
List<String> filteredImports = new ArrayList<>();
for (String importName: importList) {
if (!STANDARD_IMPORTS.contains(importName)) {
filteredImports.add(importName);
}
}
return filteredImports;
}

public static String getModulePrefix(String contextParam, String filename) {
if (componentsModuleMap.containsKey(contextParam) && protofileModuleMap.containsKey(filename)) {
if (!protofileModuleMap.get(filename).equals(componentsModuleMap.get(contextParam))) {
Expand Down
Loading

0 comments on commit 5d6f149

Please sign in to comment.