From 796bae0d6913983d487d2abc1d352d8c2db73535 Mon Sep 17 00:00:00 2001 From: rashmibharambe <93034034+rashmibharambe@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:16:16 +0530 Subject: [PATCH 1/4] Improve client tool constant generation code --- .../utils/ClientSampleSyntaxTreeUtils.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java index cf047dc..3283594 100644 --- a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java +++ b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java @@ -53,6 +53,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.HashMap; import static io.ballerina.protoc.GrpcConstants.ORG_NAME; import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getCheckExpressionNode; @@ -90,6 +91,21 @@ public class ClientSampleSyntaxTreeUtils { private static boolean firstLine; + private static final Map INPUT_TYPE_EXPR_NODES = new HashMap<>(); + + static { + INPUT_TYPE_EXPR_NODES.put("int", getNumericLiteralNode(1)); + INPUT_TYPE_EXPR_NODES.put("float", getNumericLiteralNode(1)); + INPUT_TYPE_EXPR_NODES.put("decimal", getNumericLiteralNode(1)); + INPUT_TYPE_EXPR_NODES.put("boolean", getBooleanLiteralNode(true)); + INPUT_TYPE_EXPR_NODES.put("string", getStringLiteralNode("ballerina")); + INPUT_TYPE_EXPR_NODES.put("byte[]", getByteArrayLiteralNode("[72,101,108,108,111]")); + INPUT_TYPE_EXPR_NODES.put("time:Utc", getTupleLiteralNode("[1659688553,0.310073000d]")); + INPUT_TYPE_EXPR_NODES.put("time:Seconds", getDecimalLiteralNode("0.310073000d")); + INPUT_TYPE_EXPR_NODES.put("map", createBasicLiteralNode(SyntaxKind.MAP_TYPE_DESC, "{message: \"Hello Ballerina\"}")); + INPUT_TYPE_EXPR_NODES.put("'any:Any", getCheckExpressionNode(getFunctionCallExpressionNode("'any", "pack", "\"ballerina\""))); + } + public static SyntaxTree generateSyntaxTreeForClientSample(ServiceStub serviceStub, String filename, Map msgMap) { NodeList moduleMembers = AbstractNodeFactory.createEmptyNodeList(); From 891158d24c0339102404b38059e230a8c697ca2a Mon Sep 17 00:00:00 2001 From: rashmibharambe <93034034+rashmibharambe@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:01:25 +0530 Subject: [PATCH 2/4] Improve client tool constant generation code --- .../syntaxtree/utils/ClientSampleSyntaxTreeUtils.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java index 3283594..73a4c08 100644 --- a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java +++ b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java @@ -51,9 +51,9 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.HashMap; import static io.ballerina.protoc.GrpcConstants.ORG_NAME; import static io.ballerina.protoc.builder.syntaxtree.components.Expression.getCheckExpressionNode; @@ -102,8 +102,11 @@ public class ClientSampleSyntaxTreeUtils { INPUT_TYPE_EXPR_NODES.put("byte[]", getByteArrayLiteralNode("[72,101,108,108,111]")); INPUT_TYPE_EXPR_NODES.put("time:Utc", getTupleLiteralNode("[1659688553,0.310073000d]")); INPUT_TYPE_EXPR_NODES.put("time:Seconds", getDecimalLiteralNode("0.310073000d")); - INPUT_TYPE_EXPR_NODES.put("map", createBasicLiteralNode(SyntaxKind.MAP_TYPE_DESC, "{message: \"Hello Ballerina\"}")); - INPUT_TYPE_EXPR_NODES.put("'any:Any", getCheckExpressionNode(getFunctionCallExpressionNode("'any", "pack", "\"ballerina\""))); + INPUT_TYPE_EXPR_NODES.put("map", createBasicLiteralNode(SyntaxKind.MAP_TYPE_DESC, + "{message: \"Hello Ballerina\"}")); + INPUT_TYPE_EXPR_NODES.put("'any:Any", getCheckExpressionNode( + getFunctionCallExpressionNode("'any", "pack", "\"ballerina\"")) + ); } public static SyntaxTree generateSyntaxTreeForClientSample(ServiceStub serviceStub, String filename, From da01339caea4b659fc7034deed7418f82ab61424 Mon Sep 17 00:00:00 2001 From: rashmibharambe <93034034+rashmibharambe@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:13:13 +0530 Subject: [PATCH 3/4] Improve client tool constant generation code --- .../utils/ClientSampleSyntaxTreeUtils.java | 43 ++++--------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java index 73a4c08..8541ca5 100644 --- a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java +++ b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java @@ -304,41 +304,14 @@ private static VariableDeclarationNode getInputDeclarationStatement(Method metho method.getInputType() + " ")), getCaptureBindingPatternNode(getRequestName(method.getMethodName()))); ExpressionNode node = null; - switch (method.getInputType()) { - case "int": - case "float": - case "decimal": - node = getNumericLiteralNode(1); - break; - case "boolean": - node = getBooleanLiteralNode(true); - break; - case "string": - node = getStringLiteralNode("ballerina"); - break; - case "byte[]": - node = getByteArrayLiteralNode("[72,101,108,108,111]"); - break; - case "time:Utc": - node = getTupleLiteralNode("[1659688553,0.310073000d]"); - break; - case "time:Seconds": - node = getDecimalLiteralNode("0.310073000d"); - break; - case "map": - node = createBasicLiteralNode(SyntaxKind.MAP_TYPE_DESC, "{message: \"Hello Ballerina\"}"); - break; - case "'any:Any": - node = getCheckExpressionNode(getFunctionCallExpressionNode("'any", "pack", "\"ballerina\"")); - break; - default: - if (msgMap.containsKey(method.getInputType())) { - Message msg = msgMap.get(method.getInputType()); - node = NodeFactory.createMappingConstructorExpressionNode( - SyntaxTreeConstants.SYNTAX_TREE_OPEN_BRACE, - NodeFactory.createSeparatedNodeList(getFieldNodes(msg, msgMap)), - SyntaxTreeConstants.SYNTAX_TREE_CLOSE_BRACE); - } + if(INPUT_TYPE_EXPR_NODES.containsKey(method.getInputType())) { + node = INPUT_TYPE_EXPR_NODES.get(method.getInputType()); + } else if (msgMap.containsKey(method.getInputType())) { + Message msg = msgMap.get(method.getInputType()); + node = NodeFactory.createMappingConstructorExpressionNode( + SyntaxTreeConstants.SYNTAX_TREE_OPEN_BRACE, + NodeFactory.createSeparatedNodeList(getFieldNodes(msg, msgMap)), + SyntaxTreeConstants.SYNTAX_TREE_CLOSE_BRACE); } VariableDeclaration valueVariable = new VariableDeclaration(bindingPatternNode, node); return valueVariable.getVariableDeclarationNode(); From 1116319189993a82f018454a64df37a183d6dd61 Mon Sep 17 00:00:00 2001 From: rashmibharambe <93034034+rashmibharambe@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:24:22 +0530 Subject: [PATCH 4/4] Improve client tool constant generation code --- .../builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java index 8541ca5..98f4df5 100644 --- a/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java +++ b/protoc-cli/src/main/java/io/ballerina/protoc/builder/syntaxtree/utils/ClientSampleSyntaxTreeUtils.java @@ -304,7 +304,7 @@ private static VariableDeclarationNode getInputDeclarationStatement(Method metho method.getInputType() + " ")), getCaptureBindingPatternNode(getRequestName(method.getMethodName()))); ExpressionNode node = null; - if(INPUT_TYPE_EXPR_NODES.containsKey(method.getInputType())) { + if (INPUT_TYPE_EXPR_NODES.containsKey(method.getInputType())) { node = INPUT_TYPE_EXPR_NODES.get(method.getInputType()); } else if (msgMap.containsKey(method.getInputType())) { Message msg = msgMap.get(method.getInputType());