Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve client tool constant generation code #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading