-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from HasithaAthukorala/service-types
Add service types generation
- Loading branch information
Showing
9 changed files
with
229 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/io/ballerina/asyncapi/codegenerator/controller/ServiceTypesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 Inc. 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.codegenerator.controller; | ||
|
||
import io.apicurio.datamodels.Library; | ||
import io.apicurio.datamodels.asyncapi.models.AaiChannelItem; | ||
import io.apicurio.datamodels.asyncapi.models.AaiDocument; | ||
import io.apicurio.datamodels.asyncapi.models.AaiMessage; | ||
import io.apicurio.datamodels.asyncapi.v2.models.Aai20Document; | ||
import io.ballerina.asyncapi.codegenerator.configuration.BallerinaAsyncApiException; | ||
import io.ballerina.asyncapi.codegenerator.configuration.Constants; | ||
import io.ballerina.asyncapi.codegenerator.usecase.ExtractServiceTypesFromSpec; | ||
import io.ballerina.asyncapi.codegenerator.usecase.GenerateServiceTypeNode; | ||
import io.ballerina.asyncapi.codegenerator.usecase.UseCase; | ||
import io.ballerina.compiler.syntax.tree.ModuleMemberDeclarationNode; | ||
import io.ballerina.compiler.syntax.tree.ModulePartNode; | ||
import io.ballerina.compiler.syntax.tree.SyntaxTree; | ||
import io.ballerina.tools.text.TextDocuments; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.ballerinalang.formatter.core.Formatter; | ||
import org.ballerinalang.formatter.core.FormatterException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ServiceTypesController implements Controller { | ||
private static final Logger logger = LogManager.getLogger(ServiceTypesController.class); | ||
|
||
@Override | ||
public void generateBalCode(String spec, String balTemplate) throws BallerinaAsyncApiException { | ||
AaiDocument asyncApiSpec = (Aai20Document) Library.readDocumentFromJSONString(spec); | ||
|
||
UseCase extractServiceTypes = new ExtractServiceTypesFromSpec(asyncApiSpec); | ||
Map<String, List<String>> serviceTypes = extractServiceTypes.execute(); | ||
|
||
List<ModuleMemberDeclarationNode> serviceNodes = new ArrayList<>(); | ||
for (Map.Entry<String, List<String>> service : serviceTypes.entrySet()) { | ||
UseCase generateServiceTypeNode = new GenerateServiceTypeNode(service.getKey(), service.getValue()); | ||
serviceNodes.add(generateServiceTypeNode.execute()); | ||
} | ||
|
||
var textDocument = TextDocuments.from(balTemplate); | ||
var syntaxTree = SyntaxTree.from(textDocument); | ||
ModulePartNode oldRoot = syntaxTree.rootNode(); | ||
ModulePartNode newRoot = oldRoot.modify().withMembers(oldRoot.members().addAll(serviceNodes)).apply(); | ||
var modifiedTree = syntaxTree.replaceNode(oldRoot, newRoot); | ||
|
||
try { | ||
var formattedSourceCode = Formatter.format(modifiedTree).toSourceCode(); | ||
logger.debug("Generated the source code for the service types: {}", formattedSourceCode); | ||
} catch (FormatterException e) { | ||
logger.error("Could not format the generated code, may be syntax issue in the generated code. " + | ||
"Generated code: {}", modifiedTree.toSourceCode()); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/io/ballerina/asyncapi/codegenerator/usecase/ExtractServiceTypesFromSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 Inc. 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.codegenerator.usecase; | ||
|
||
import io.apicurio.datamodels.asyncapi.models.AaiChannelItem; | ||
import io.apicurio.datamodels.asyncapi.models.AaiDocument; | ||
import io.apicurio.datamodels.asyncapi.models.AaiMessage; | ||
import io.ballerina.asyncapi.codegenerator.configuration.BallerinaAsyncApiException; | ||
import io.ballerina.asyncapi.codegenerator.configuration.Constants; | ||
import io.ballerina.asyncapi.codegenerator.usecase.utils.CodegenUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ExtractServiceTypesFromSpec implements UseCase { | ||
private final AaiDocument asyncApiSpec; | ||
private final CodegenUtils codegenUtils = new CodegenUtils(); | ||
|
||
public ExtractServiceTypesFromSpec(AaiDocument asyncApiSpec) { | ||
this.asyncApiSpec = asyncApiSpec; | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> execute() throws BallerinaAsyncApiException { | ||
Map<String, List<String>> serviceTypes = new HashMap<>(); | ||
for (Map.Entry<String, AaiChannelItem> channel : asyncApiSpec.channels.entrySet()) { | ||
List<String> remoteFunctions = new ArrayList<>(); | ||
String serviceType; | ||
if (channel.getValue().getExtension(Constants.X_BALLERINA_SERVICE_TYPE) == null) { | ||
serviceType = codegenUtils.getValidName(channel.getKey(), true); | ||
} else { | ||
serviceType = channel.getValue().getExtension(Constants.X_BALLERINA_SERVICE_TYPE).value.toString(); | ||
} | ||
AaiMessage mainMessage = channel.getValue().subscribe.message; | ||
if (mainMessage.oneOf != null) { | ||
for(AaiMessage message: mainMessage.oneOf) { | ||
if (message.getExtension(Constants.X_BALLERINA_EVENT_TYPE) == null) { | ||
throw new BallerinaAsyncApiException( | ||
"Could not find the ".concat(Constants.X_BALLERINA_EVENT_TYPE) | ||
.concat(" attribute in the message of the channel ").concat(channel.getKey())); | ||
} | ||
remoteFunctions.add( | ||
message.getExtension(Constants.X_BALLERINA_EVENT_TYPE).value.toString()); | ||
} | ||
} else { | ||
if (mainMessage.getExtension(Constants.X_BALLERINA_EVENT_TYPE) == null) { | ||
throw new BallerinaAsyncApiException( | ||
"Could not find the ".concat(Constants.X_BALLERINA_EVENT_TYPE) | ||
.concat(" attribute in the message of the channel ").concat(channel.getKey())); | ||
} | ||
remoteFunctions.add(channel.getValue() | ||
.subscribe.message.getExtension(Constants.X_BALLERINA_EVENT_TYPE).value.toString()); | ||
} | ||
serviceTypes.put(serviceType, remoteFunctions); | ||
} | ||
return serviceTypes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/io/ballerina/asyncapi/codegenerator/usecase/GenerateServiceTypeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 Inc. 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.codegenerator.usecase; | ||
|
||
import io.ballerina.asyncapi.codegenerator.configuration.BallerinaAsyncApiException; | ||
import io.ballerina.asyncapi.codegenerator.usecase.utils.CodegenUtils; | ||
import io.ballerina.compiler.syntax.tree.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.*; | ||
import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createToken; | ||
import static io.ballerina.compiler.syntax.tree.NodeFactory.*; | ||
import static io.ballerina.compiler.syntax.tree.SyntaxKind.*; | ||
|
||
public class GenerateServiceTypeNode implements UseCase { | ||
private final String serviceTypeName; | ||
private final List<String> remoteFunctionNames; | ||
private final CodegenUtils codegenUtils = new CodegenUtils(); | ||
|
||
public GenerateServiceTypeNode(String serviceTypeName, List<String> remoteFunctionNames) { | ||
this.serviceTypeName = serviceTypeName; | ||
this.remoteFunctionNames = remoteFunctionNames; | ||
} | ||
|
||
@Override | ||
public TypeDefinitionNode execute() throws BallerinaAsyncApiException { | ||
List<Node> remoteFunctions = new ArrayList<>(); | ||
remoteFunctionNames.forEach(remoteFunction -> { | ||
var methodDeclarationNode = createMethodDeclarationNode( | ||
SyntaxKind.METHOD_DECLARATION, null, createNodeList(createToken(REMOTE_KEYWORD)), | ||
createToken(SyntaxKind.FUNCTION_KEYWORD), createIdentifierToken(remoteFunction), createEmptyNodeList(), | ||
createFunctionSignatureNode( | ||
createToken(OPEN_PAREN_TOKEN), createSeparatedNodeList(), | ||
createToken(CLOSE_PAREN_TOKEN), null), | ||
createToken(SyntaxKind.SEMICOLON_TOKEN)); | ||
remoteFunctions.add(methodDeclarationNode); | ||
}); | ||
var serviceTypeToken = AbstractNodeFactory | ||
.createIdentifierToken(serviceTypeName); | ||
var recordTypeDescriptorNode = | ||
NodeFactory.createRecordTypeDescriptorNode(createIdentifierToken("service object"), | ||
createToken(OPEN_BRACE_TOKEN), createNodeList(remoteFunctions), null, | ||
createToken(SyntaxKind.CLOSE_BRACE_TOKEN)); | ||
return createTypeDefinitionNode(null, createToken(PUBLIC_KEYWORD), | ||
createToken(TYPE_KEYWORD), serviceTypeToken, recordTypeDescriptorNode, createToken(SEMICOLON_TOKEN)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.