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

Remove unused functions #2

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
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 @@ -48,7 +48,6 @@
import io.ballerina.compiler.syntax.tree.CaptureBindingPatternNode;
import io.ballerina.compiler.syntax.tree.CheckExpressionNode;
import io.ballerina.compiler.syntax.tree.DefaultableParameterNode;
import io.ballerina.compiler.syntax.tree.DoStatementNode;
import io.ballerina.compiler.syntax.tree.ElseBlockNode;
import io.ballerina.compiler.syntax.tree.ExpressionNode;
import io.ballerina.compiler.syntax.tree.FieldAccessExpressionNode;
Expand All @@ -62,7 +61,6 @@
import io.ballerina.compiler.syntax.tree.MarkdownDocumentationNode;
import io.ballerina.compiler.syntax.tree.MarkdownParameterDocumentationLineNode;
import io.ballerina.compiler.syntax.tree.MetadataNode;
import io.ballerina.compiler.syntax.tree.MethodCallExpressionNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NodeFactory;
import io.ballerina.compiler.syntax.tree.NodeList;
Expand Down Expand Up @@ -109,24 +107,18 @@
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.CLIENT_CRED;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.CONNECTION_CONFIG;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.CUSTOM_HEADERS;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.ENSURE_TYPE;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.HTTP;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.HTTP_API_KEY;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.OAUTH2;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.PASSWORD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.PING_PONG_HANDLER_FIELD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.PING_PONG_SERVICE;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.PING_PONG_SERVICE_FIELD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.QUERY_PARAMS;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.REFRESH_TOKEN;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.RETRY_CONFIG_FIELD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.SECURE_SOCKET;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.SECURE_SOCKET_FIELD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.SELF;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.STRING;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.USER_PASSWORD;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.VALIDATION;
import static io.ballerina.asyncapi.websocketscore.GeneratorConstants.WEB_SOCKET_RETRY_CONFIG;
import static io.ballerina.asyncapi.websocketscore.GeneratorUtils.escapeIdentifier;
import static io.ballerina.asyncapi.websocketscore.GeneratorUtils.getValidName;
import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createEmptyNodeList;
Expand All @@ -140,7 +132,6 @@
import static io.ballerina.compiler.syntax.tree.NodeFactory.createCaptureBindingPatternNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createCheckExpressionNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createDefaultableParameterNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createDoStatementNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createElseBlockNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createFieldAccessExpressionNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createIfElseStatementNode;
Expand Down Expand Up @@ -181,7 +172,6 @@
import static io.ballerina.compiler.syntax.tree.SyntaxKind.COMMA_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.DECIMAL_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.DOT_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.DO_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.ELSE_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.EQUAL_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.FINAL_KEYWORD;
Expand Down Expand Up @@ -260,7 +250,7 @@ public boolean isHttpApiKey() {
*
* @return {@link boolean}
*/
public boolean isHttpOROAuth() {
public boolean isHttpOrOAuth() {
return httpOROAuth;
}

Expand Down Expand Up @@ -915,148 +905,6 @@ private Node getServiceURLNode(String serviceUrl) {
return serviceURLNode;
}

/**
* Generate if-else statements for the do block in client init function.
* <pre>
* if config.http1Settings is ClientHttp1Settings {
* ClientHttp1Settings settings = check config.http1Settings.ensureType(ClientHttp1Settings);
* httpClientConfig.http1Settings = {...settings};
* }
* </pre>
*
* @param fieldName name of the field
* @param fieldType type of the field
* @return
*/
private IfElseStatementNode getDoBlockIfElseStatementNodes(String fieldName, String fieldType) {
ExpressionNode expressionNode = createFieldAccessExpressionNode(
createRequiredExpressionNode(createIdentifierToken(CLIENT_CONFIG)),
createToken(DOT_TOKEN), createSimpleNameReferenceNode(createIdentifierToken(fieldName)));

ExpressionNode condition = createBinaryExpressionNode(null,
expressionNode,
createToken(IS_KEYWORD),
createIdentifierToken(fieldType)
);

List<StatementNode> statementNodes = new ArrayList<>();

// httpClientConfig.http2Settings = check config.http2Settings.ensureType(http:ClientHttp2Settings);
FieldAccessExpressionNode fieldAccessExpressionNode = createFieldAccessExpressionNode(
createRequiredExpressionNode(createIdentifierToken(CLIENT_CONFIG)),
createToken(DOT_TOKEN),
createSimpleNameReferenceNode(createIdentifierToken(fieldName)));

MethodCallExpressionNode methodCallExpressionNode = createMethodCallExpressionNode(
createFieldAccessExpressionNode(createRequiredExpressionNode(createIdentifierToken(CLIENT_CONFIG)),
createToken(DOT_TOKEN),
createSimpleNameReferenceNode(createIdentifierToken(fieldName))),
createToken(DOT_TOKEN),
createSimpleNameReferenceNode(createIdentifierToken(ENSURE_TYPE)),
createToken(OPEN_PAREN_TOKEN),
createSeparatedNodeList(createPositionalArgumentNode(
createRequiredExpressionNode(createIdentifierToken(fieldType)))),
createToken(CLOSE_PAREN_TOKEN));
CheckExpressionNode checkExpressionNode = createCheckExpressionNode(null, createToken(CHECK_KEYWORD),
methodCallExpressionNode);
AssignmentStatementNode varAssignmentNode = createAssignmentStatementNode(fieldAccessExpressionNode,
createToken(EQUAL_TOKEN), checkExpressionNode, createToken(SEMICOLON_TOKEN));
statementNodes.add(varAssignmentNode);

NodeList<StatementNode> statementList = createNodeList(statementNodes);
BlockStatementNode ifBody = createBlockStatementNode(createToken(OPEN_BRACE_TOKEN), statementList,
createToken(CLOSE_BRACE_TOKEN));
return createIfElseStatementNode(createToken(IF_KEYWORD), condition,
ifBody, null);
}

/**
* Generate do block in client init function.
*
* @return {@link DoStatementNode}
*/
public DoStatementNode getClientConfigDoStatementNode() {
List<StatementNode> doStatementNodeList = new ArrayList<>();
// ClientHttp1Settings if statement
// {
// ExpressionNode expressionNode = createFieldAccessExpressionNode(
// createRequiredExpressionNode(createIdentifierToken(CONFIG)),
// createToken(DOT_TOKEN), createSimpleNameReferenceNode(
// createIdentifierToken(CLIENT_HTTP1_SETTINGS_FIELD)));
//
// ExpressionNode condition = createBinaryExpressionNode(null,
// expressionNode,
// createToken(IS_KEYWORD),
// createIdentifierToken(CLIENT_HTTP1_SETTINGS)
// );
//
// List<StatementNode> statementNodes = new ArrayList<>();
//
// // ClientHttp1Settings settings = check config.http1Settings.ensureType(ClientHttp1Settings);
// SimpleNameReferenceNode typeBindingPattern = createSimpleNameReferenceNode(
// createIdentifierToken(CLIENT_HTTP1_SETTINGS));
// CaptureBindingPatternNode bindingPattern = createCaptureBindingPatternNode(
// createIdentifierToken(SETTINGS));
// TypedBindingPatternNode typedBindingPatternNode = createTypedBindingPatternNode(typeBindingPattern,
// bindingPattern);
// MethodCallExpressionNode methodCallExpressionNode = createMethodCallExpressionNode(
// createFieldAccessExpressionNode(createRequiredExpressionNode(createIdentifierToken(CONFIG)),
// createToken(DOT_TOKEN),
// createSimpleNameReferenceNode(createIdentifierToken(CLIENT_HTTP1_SETTINGS_FIELD))),
// createToken(DOT_TOKEN),
// createSimpleNameReferenceNode(createIdentifierToken(ENSURE_TYPE)),
// createToken(OPEN_PAREN_TOKEN),
// createSeparatedNodeList(createPositionalArgumentNode(
// createRequiredExpressionNode(createIdentifierToken(CLIENT_HTTP1_SETTINGS)))),
// createToken(CLOSE_PAREN_TOKEN));
// CheckExpressionNode checkExpressionNode = createCheckExpressionNode(null, createToken(CHECK_KEYWORD),
// methodCallExpressionNode);
// AssignmentStatementNode varAssignmentNode = createAssignmentStatementNode(typedBindingPatternNode,
// createToken(EQUAL_TOKEN), checkExpressionNode, createToken(SEMICOLON_TOKEN));
// statementNodes.add(varAssignmentNode);
//
// // httpClientConfig.http1Settings = {...settings};
// FieldAccessExpressionNode fieldAccessExpressionNode = createFieldAccessExpressionNode(
// createRequiredExpressionNode(createIdentifierToken(WEBSOCKET_CLIENT_CONFIG)),
// createToken(DOT_TOKEN),
// createSimpleNameReferenceNode(createIdentifierToken(CLIENT_HTTP1_SETTINGS_FIELD)));
// MappingConstructorExpressionNode mappingConstructorExpressionNode =
// createMappingConstructorExpressionNode(
// createToken(OPEN_BRACE_TOKEN),
// createSeparatedNodeList(
// createRestArgumentNode(createToken(ELLIPSIS_TOKEN),
// createRequiredExpressionNode(createIdentifierToken(SETTINGS)))),
// createToken(CLOSE_BRACE_TOKEN));
//
// AssignmentStatementNode fieldAssignmentNode = createAssignmentStatementNode(fieldAccessExpressionNode,
// createToken(EQUAL_TOKEN), mappingConstructorExpressionNode, createToken(SEMICOLON_TOKEN));
//
// statementNodes.add(fieldAssignmentNode);
//
// NodeList<StatementNode> statementList = createNodeList(statementNodes);
// BlockStatementNode ifBody = createBlockStatementNode(createToken(OPEN_BRACE_TOKEN), statementList,
// createToken(CLOSE_BRACE_TOKEN));
//
// IfElseStatementNode ifElseStatementNode = createIfElseStatementNode(createToken(IF_KEYWORD), condition,
// ifBody, null);
// doStatementNodeList.add(ifElseStatementNode);
// }

doStatementNodeList.addAll(Arrays.asList(
getDoBlockIfElseStatementNodes(SECURE_SOCKET_FIELD, SECURE_SOCKET),
// getDoBlockIfElseStatementNodes(COOKIES_FIELD, HTTP2_SETTINGS),
// getDoBlockIfElseStatementNodes(CACHE_CONFIG_FIELD, CACHE_CONFIG),
getDoBlockIfElseStatementNodes(PING_PONG_HANDLER_FIELD, PING_PONG_SERVICE),

getDoBlockIfElseStatementNodes(RETRY_CONFIG_FIELD, WEB_SOCKET_RETRY_CONFIG)));

BlockStatementNode blockStatementNode = createBlockStatementNode(createToken(OPEN_BRACE_TOKEN),
createNodeList(doStatementNodeList), createToken(CLOSE_BRACE_TOKEN));

return createDoStatementNode(createToken(DO_KEYWORD),
blockStatementNode, null);
}

//TODO: Use this if want to use this in Choreo

/**
Expand Down Expand Up @@ -1089,7 +937,7 @@ public VariableDeclarationNode getWebsocketClientConfigVariableNode() {

List<Node> argumentsList = new ArrayList<>();

if (isHttpOROAuth() && !isHttpApiKey()) {
if (isHttpOrOAuth() && !isHttpApiKey()) {
ExpressionNode authValExp = createFieldAccessExpressionNode(
createSimpleNameReferenceNode(createIdentifierToken(CLIENT_CONFIG)),
createToken(DOT_TOKEN), createSimpleNameReferenceNode(createIdentifierToken(AUTH)));
Expand Down Expand Up @@ -1291,7 +1139,7 @@ private List<Node> getClientConfigRecordFields() {
Token questionMarkToken = createToken(QUESTION_MARK_TOKEN);

// add auth field
if (isHttpOROAuth() && !isHttpApiKey()) {
if (isHttpOrOAuth() && !isHttpApiKey()) {
MetadataNode authMetadataNode = getMetadataNode("Configurations related to client authentication");
IdentifierToken authFieldName = AbstractNodeFactory.createIdentifierToken(escapeIdentifier(
AUTH));
Expand All @@ -1300,7 +1148,7 @@ private List<Node> getClientConfigRecordFields() {
RecordFieldNode authFieldNode = NodeFactory.createRecordFieldNode(authMetadataNode, null,
authFieldTypeNode, authFieldName, null, semicolonToken);
recordFieldNodes.add(authFieldNode);
} else if (isHttpOROAuth() && isHttpApiKey()) {
} else if (isHttpOrOAuth() && isHttpApiKey()) {
MetadataNode authMetadataNode = getMetadataNode(
"Provides Auth configurations needed when communicating with a remote Websocket " +
"service endpoint.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
public class CommonFunctionUtils {

private AsyncApi25DocumentImpl asyncAPI;
private final AsyncApi25DocumentImpl asyncAPI;

public CommonFunctionUtils(AsyncApi25DocumentImpl asyncAPI) {
this.asyncAPI = asyncAPI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ private FunctionBodyNode getInitFunctionBodyNode(AsyncApi25SchemaImpl querySchem
//TODO: Attempt to map auth configurations
// If both apiKey and httpOrOAuth is supported
// todo : After revamping
if (ballerinaAuthConfigGenerator.isHttpApiKey() && ballerinaAuthConfigGenerator.isHttpOROAuth()) {
if (ballerinaAuthConfigGenerator.isHttpApiKey() && ballerinaAuthConfigGenerator.isHttpOrOAuth()) {
assignmentNodes.add(ballerinaAuthConfigGenerator.handleInitForMixOfApiKeyAndHTTPOrOAuth());
}

Expand Down Expand Up @@ -956,7 +956,7 @@ private FunctionBodyNode getInitFunctionBodyNode(AsyncApi25SchemaImpl querySchem
whileLoopBreakVariables.add(IS_ACTIVE);

for (String whileLoopBreakVariable : whileLoopBreakVariables) {
addInitsOFWhileLoopBreaksNodes(whileLoopBreakVariable, assignmentNodes, TRUE_KEYWORD.stringValue());
addInitsOfWhileLoopBreaksNodes(whileLoopBreakVariable, assignmentNodes, TRUE_KEYWORD.stringValue());
}

ArrayList<String> workers = new ArrayList<>();
Expand All @@ -975,7 +975,7 @@ private FunctionBodyNode getInitFunctionBodyNode(AsyncApi25SchemaImpl querySchem
}

// Get API key assignment node if authentication mechanism type is only `apiKey`
if (ballerinaAuthConfigGenerator.isHttpApiKey() && !ballerinaAuthConfigGenerator.isHttpOROAuth()) {
if (ballerinaAuthConfigGenerator.isHttpApiKey() && !ballerinaAuthConfigGenerator.isHttpOrOAuth()) {
assignmentNodes.add(ballerinaAuthConfigGenerator.getApiKeyAssignmentNode());
}
if (ballerinaAuthConfigGenerator.isHttpApiKey()) {
Expand All @@ -991,7 +991,7 @@ private FunctionBodyNode getInitFunctionBodyNode(AsyncApi25SchemaImpl querySchem
return createFunctionBodyBlockNode(openBraceToken, null, statementList, closeBraceToken, null);
}

private void addInitsOFWhileLoopBreaksNodes(String initName, List<StatementNode> assignmentNodes,
private void addInitsOfWhileLoopBreaksNodes(String initName, List<StatementNode> assignmentNodes,
String booleanValue) {
SimpleNameReferenceNode selfIsMessageWritingValue = createSimpleNameReferenceNode(createIdentifierToken(
booleanValue));
Expand Down Expand Up @@ -1180,7 +1180,7 @@ private MetadataNode getInitDocComment() {
//todo: setInitDocComment() pass the references

docs.addAll(DocCommentsGenerator.createAPIDescriptionDoc(clientInitDocComment, true));
if (ballerinaAuthConfigGenerator.isHttpApiKey() && !ballerinaAuthConfigGenerator.isHttpOROAuth()) {
if (ballerinaAuthConfigGenerator.isHttpApiKey() && !ballerinaAuthConfigGenerator.isHttpOrOAuth()) {
MarkdownParameterDocumentationLineNode apiKeyConfig = DocCommentsGenerator.createAPIParamDoc(
API_KEY_CONFIG, DEFAULT_API_KEY_DESC);
docs.add(apiKeyConfig);
Expand Down Expand Up @@ -1414,7 +1414,7 @@ private FunctionDefinitionNode createAttemptToCloseConnectionFunction() {

private FunctionDefinitionNode createConnectionCloseFunction(boolean streamsPresent) {
ArrayList<StatementNode> lockStatements = new ArrayList<>();
addInitsOFWhileLoopBreaksNodes(IS_ACTIVE, lockStatements, FALSE_KEYWORD.stringValue());
addInitsOfWhileLoopBreaksNodes(IS_ACTIVE, lockStatements, FALSE_KEYWORD.stringValue());

//check self.writeMessageQueue.immediateClose();
ExpressionStatementNode writeMessageStatementNode = getCloseLockStatementNode(WRITE_MESSAGE_QUEUE,
Expand Down