Skip to content

Commit

Permalink
Merge pull request #42 from rashmibharambe/patch-1
Browse files Browse the repository at this point in the history
Improve client tool constant generation code
  • Loading branch information
dilanSachi authored Oct 16, 2023
2 parents 98f45ae + 1116319 commit da5b700
Showing 1 changed file with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -90,6 +91,24 @@ public class ClientSampleSyntaxTreeUtils {

private static boolean firstLine;

private static final Map<String, ExpressionNode> 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<anydata>", 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<String, Message> msgMap) {
NodeList<ModuleMemberDeclarationNode> moduleMembers = AbstractNodeFactory.createEmptyNodeList();
Expand Down Expand Up @@ -285,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<anydata>":
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();
Expand Down

0 comments on commit da5b700

Please sign in to comment.