Skip to content

Commit

Permalink
Merge pull request #43476 from nipunayf/improve-components-api
Browse files Browse the repository at this point in the history
Enhance the `ballerinaPackage/components` API
  • Loading branch information
hasithaa authored Oct 9, 2024
2 parents 55b5a67 + 6117ddd commit a5c703d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
import io.ballerina.tools.text.LineRange;

Expand Down Expand Up @@ -63,6 +64,11 @@ public Optional<MapperObject> transformSyntaxNode(Node node) {

@Override
public Optional<MapperObject> transform(FunctionDefinitionNode functionDefinitionNode) {
if (functionDefinitionNode.functionName().text().equals(PackageServiceConstants.MAIN_FUNCTION)) {
return Optional.of(new MapperObject(PackageServiceConstants.AUTOMATIONS,
createDataObject(PackageServiceConstants.MAIN_FUNCTION, functionDefinitionNode)));
}

return Optional.of(new MapperObject(PackageServiceConstants.FUNCTIONS,
createDataObject(functionDefinitionNode.functionName().text(), functionDefinitionNode)));
}
Expand All @@ -80,8 +86,12 @@ public Optional<MapperObject> transform(ServiceDeclarationNode serviceDeclaratio
DataObject dataObject = createDataObject(name, serviceDeclarationNode);
serviceDeclarationNode.members().forEach(member -> {
if (member.kind() == SyntaxKind.RESOURCE_ACCESSOR_DEFINITION) {
dataObject.addResource(createDataObject(((FunctionDefinitionNode) member).functionName().text(),
member));
FunctionDefinitionNode functionDefinitionNode = (FunctionDefinitionNode) member;
String resourceName = functionDefinitionNode.functionName().text() + "-" +
functionDefinitionNode.relativeResourcePath().stream()
.map(Node::toSourceCode)
.collect(Collectors.joining(""));
dataObject.addResource(createDataObject(resourceName, member));
}
});
return Optional.of(new MapperObject(PackageServiceConstants.SERVICES, dataObject));
Expand Down Expand Up @@ -115,9 +125,17 @@ public Optional<MapperObject> transform(TypeDefinitionNode typeDefinitionNode) {

@Override
public Optional<MapperObject> transform(ModuleVariableDeclarationNode moduleVariableDeclarationNode) {
Optional<Token> isConfigurable = moduleVariableDeclarationNode.qualifiers().stream()
.filter(qualifier -> qualifier.kind() == SyntaxKind.CONFIGURABLE_KEYWORD)
.findFirst();
if (isConfigurable.isPresent()) {
return Optional.of(new MapperObject(PackageServiceConstants.CONFIGURABLE_VARIABLES,
createDataObject(moduleVariableDeclarationNode.typedBindingPattern().bindingPattern().toString(),
moduleVariableDeclarationNode)));
}
return Optional.of(new MapperObject(PackageServiceConstants.MODULE_LEVEL_VARIABLE,
createDataObject(moduleVariableDeclarationNode.typedBindingPattern().bindingPattern().toString(),
moduleVariableDeclarationNode)));
moduleVariableDeclarationNode)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ModuleObject {
private final List<DataObject> enums = new ArrayList<>();
private final List<DataObject> listeners = new ArrayList<>();
private final List<DataObject> moduleVariables = new ArrayList<>();
private final List<DataObject> configurableVariables = new ArrayList<>();
private final List<DataObject> automations = new ArrayList<>();

private String name;

Expand Down Expand Up @@ -103,6 +105,12 @@ protected void addDataObject(MapperObject mapperObject) {
case PackageServiceConstants.MODULE_LEVEL_VARIABLE:
this.moduleVariables.add(mapperObject.getDataObject());
break;
case PackageServiceConstants.CONFIGURABLE_VARIABLES:
this.configurableVariables.add(mapperObject.getDataObject());
break;
case PackageServiceConstants.AUTOMATIONS:
this.automations.add(mapperObject.getDataObject());
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class PackageServiceConstants {

protected static final String CAPABILITY_NAME = "ballerinaPackage";
protected static final String MAIN_FUNCTION = "main";
protected static final String NAME = "name";
protected static final String FILE_PATH = "filePath";
protected static final String START_LINE = "startLine";
Expand All @@ -41,4 +42,6 @@ public class PackageServiceConstants {
protected static final String CLASSES = "classes";
protected static final String LISTENERS = "listeners";
protected static final String MODULE_LEVEL_VARIABLE = "moduleVariables";
protected static final String CONFIGURABLE_VARIABLES = "configurableVariables";
protected static final String AUTOMATIONS = "automations";
}

0 comments on commit a5c703d

Please sign in to comment.