Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhashinee committed Jul 2, 2024
1 parent 3e87d60 commit d454a7b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package io.ballerina.asyncapi.cmd;

import io.ballerina.asyncapi.cmd.websockets.AsyncAPIDiagnostic;
import io.ballerina.asyncapi.cmd.websockets.AsyncApiDiagnostic;
import io.ballerina.asyncapi.cmd.websockets.AsyncApiToBallerinaGenerator;
import io.ballerina.asyncapi.cmd.websockets.BallerinaToAsyncApiGenerator;
import io.ballerina.asyncapi.cmd.websockets.CmdConstants;
Expand Down Expand Up @@ -281,13 +281,13 @@ private void ballerinaToAsyncApiWs(String fileName) {
if (!errors.isEmpty()) {
for (AsyncApiConverterDiagnostic error : errors) {
if (error instanceof ExceptionDiagnostic exceptionDiagnostic) {
AsyncAPIDiagnostic diagnostic = CmdUtils.constructAsyncAPIDiagnostic(exceptionDiagnostic.getCode(),
AsyncApiDiagnostic diagnostic = CmdUtils.constructAsyncAPIDiagnostic(exceptionDiagnostic.getCode(),
exceptionDiagnostic.getMessage(), exceptionDiagnostic.getDiagnosticSeverity(),
exceptionDiagnostic.getLocation().orElse(null));
outStream.println(diagnostic);
exitError(this.exitWhenFinish);
} else if (error instanceof IncompatibleRemoteDiagnostic incompatibleError) {
AsyncAPIDiagnostic diagnostic = CmdUtils.constructAsyncAPIDiagnostic(incompatibleError.getCode(),
AsyncApiDiagnostic diagnostic = CmdUtils.constructAsyncAPIDiagnostic(incompatibleError.getCode(),
incompatibleError.getMessage(), incompatibleError.getDiagnosticSeverity(),
incompatibleError.getLocation().get());
outStream.println(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.asyncapi.cmd;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
* Represents a {@code Diagnostic} related to asyncapi command.
*
*/
public class AsyncAPIDiagnostic extends Diagnostic {
public class AsyncApiDiagnostic extends Diagnostic {
private final DiagnosticInfo diagnosticInfo;
private final Location location;
private final String message;

public AsyncAPIDiagnostic(DiagnosticInfo diagnosticInfo, Location location, Object[] args) {
public AsyncApiDiagnostic(DiagnosticInfo diagnosticInfo, Location location, Object[] args) {
this.diagnosticInfo = diagnosticInfo;
this.location = location;
this.message = MessageFormat.format(diagnosticInfo.messageFormat(), args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class CmdUtils {
/**
* This util method is used to generate {@code Diagnostic} for asyncapi command errors.
*/
public static AsyncAPIDiagnostic constructAsyncAPIDiagnostic(String code, String message,
public static AsyncApiDiagnostic constructAsyncAPIDiagnostic(String code, String message,
DiagnosticSeverity severity, Location location,
Object... args) {

DiagnosticInfo diagnosticInfo = new DiagnosticInfo(code, message, severity);
if (location == null) {
location = new ConverterCommonUtils.NullLocation();
}
return new AsyncAPIDiagnostic(diagnosticInfo, location, args);
return new AsyncApiDiagnostic(diagnosticInfo, location, args);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void generate(String specPath, String outputPath) throws BallerinaAsyncAp

BalController dispatcherController = new DispatcherController(serviceTypes, eventIdentifierType,
eventIdentifierPath);
String dispatcherContent = "";
String dispatcherContent;
if (eventIdentifierType.equals(Constants.X_BALLERINA_EVENT_TYPE_BODY)) {
String dispatcherTemplateForEventIdentifierInBody = fileRepository
.getFileContentFromResources(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,28 @@ private TypeDescriptorNode getTypeDescriptorNodeForObjects(AsyncApiSchema schema
if (schema.getProperties() != null) {
return getRecordTypeDescriptorNode(schema);
} else if (schema.getType() != null) {
return getTypeDescriptorNodeFroPreDefined(schema);
return getTypeDescriptorNodeForPreDefined(schema);
} else {
throw new BallerinaAsyncApiException(
"Unsupported Async Api Spec data type `" + schema.getType() + "`");
}
}

private TypeDescriptorNode getTypeDescriptorNodeFroPreDefined(AsyncApiSchema schema)
private TypeDescriptorNode getTypeDescriptorNodeForPreDefined(AsyncApiSchema schema)
throws BallerinaAsyncApiException {
String type;
Token typeName;
switch (schema.getType()) {
case Constants.INTEGER:
case Constants.STRING:
case Constants.BOOLEAN:
String type = convertAsyncAPITypeToBallerina(schema.getType().trim());
Token typeName = AbstractNodeFactory.createIdentifierToken(type);
type = convertAsyncApiTypeToBallerina(schema.getType().trim());
typeName = AbstractNodeFactory.createIdentifierToken(type);
return createBuiltinSimpleNameReferenceNode(null, typeName);
case Constants.NUMBER:
type = convertAsyncAPITypeToBallerina(schema.getType().trim());
if (schema.getType().equals("number") && schema.getFormat() != null) {
type = convertAsyncAPITypeToBallerina(schema.getFormat().trim());
type = convertAsyncApiTypeToBallerina(schema.getType().trim());
if (schema.getFormat() != null) {
type = convertAsyncApiTypeToBallerina(schema.getFormat().trim());
}
typeName = AbstractNodeFactory.createIdentifierToken(type);
return createBuiltinSimpleNameReferenceNode(null, typeName);
Expand All @@ -241,7 +243,7 @@ private TypeDescriptorNode getTypeDescriptorNodeFroPreDefined(AsyncApiSchema sch
typeName = AbstractNodeFactory.createIdentifierToken(type);
} else {
typeName = AbstractNodeFactory.createIdentifierToken(
convertAsyncAPITypeToBallerina(schema.getType().trim()));
convertAsyncApiTypeToBallerina(schema.getType().trim()));
}
return createBuiltinSimpleNameReferenceNode(null, typeName);
default:
Expand Down Expand Up @@ -277,7 +279,7 @@ private RecordTypeDescriptorNode getRecordTypeDescriptorNode(AsyncApiSchema sche
* @param type AsyncApi parameter types
* @return ballerina type
*/
public static String convertAsyncAPITypeToBallerina(String type) throws BallerinaAsyncApiException {
public static String convertAsyncApiTypeToBallerina(String type) throws BallerinaAsyncApiException {
String convertedType;
switch (type) {
case Constants.INTEGER:
Expand Down Expand Up @@ -335,7 +337,7 @@ public TypeDescriptorNode getTypeDescriptorNodeForArraySchema(AsyncApiSchema sch
return createArrayTypeDescriptorNode(memberTypeDesc, createNodeList(arrayDimensionNode));
} else if (schemaItem.getType() != null) {
type = schemaItem.getType();
typeName = AbstractNodeFactory.createIdentifierToken(convertAsyncAPITypeToBallerina(type));
typeName = AbstractNodeFactory.createIdentifierToken(convertAsyncApiTypeToBallerina(type));
memberTypeDesc = createBuiltinSimpleNameReferenceNode(null, typeName);
return createArrayTypeDescriptorNode(memberTypeDesc, createNodeList(arrayDimensionNode));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@

import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.CAMEL_CASE_PATTERN;
import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.FALSE;
import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.FUNCTION_DEFAULT_NAME_CONTAINS_ERROR;
import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.FUNCTION_PARAMETERS_EXCEEDED;
import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.FUNCTION_SIGNATURE_WRONG_TYPE;
import static io.ballerina.asyncapi.websocketscore.generators.asyncspec.Constants.FUNCTION_WRONG_NAME;
Expand Down Expand Up @@ -129,10 +128,10 @@ public AsyncApi25ChannelsImpl getChannels(FunctionDefinitionNode resource,
/**
* Remote mapper when there have multiple remote methods.
* @param resource functionDefinitionNode which contains resource function
* @param classDefinitionNode
* @param dispatcherValue
* @param channelItem
* @return
* @param classDefinitionNode classDefinitionNode which contains class definition
* @param dispatcherValue dispatcher key value
* @param channelItem AsyncAPI channel
* @return AsyncAPI channel object
*/
private AsyncApi25ChannelsImpl handleRemoteFunctions(FunctionDefinitionNode resource,
ClassDefinitionNode classDefinitionNode,
Expand Down Expand Up @@ -208,8 +207,6 @@ private AsyncApi25ChannelsImpl handleRemoteFunctions(FunctionDefinitionNode reso
}
}
//TODO: Change because onError and onIdleTimeout in graphql over websocket
} else {
throw new NoSuchElementException(FUNCTION_DEFAULT_NAME_CONTAINS_ERROR);
}
} else {
throw new NoSuchElementException(FUNCTION_WRONG_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* This test class for the covering the unit tests for return type scenarios.
*/
public class APIDocTests {
public class ApiDocTests {
private static final Path RES_DIR = Paths.get("src/test/resources/websockets" +
"/ballerina-to-asyncapi").toAbsolutePath();
private Path tempDir;
Expand Down
2 changes: 1 addition & 1 deletion asyncapi-cli/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.AsyncApiConverterUtilsTest"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.RecordTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.ResponseTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.APIDocTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.ApiDocTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.EnumTypeTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.ListenerTests"/>
<class name="io.ballerina.asyncapi.wsgenerators.asyncapi.ModuleReferenceTests"/>
Expand Down

0 comments on commit d454a7b

Please sign in to comment.