Skip to content

Commit

Permalink
Support searching types in the search api
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunayf committed Feb 28, 2025
1 parent d8b68b2 commit ce6c895
Show file tree
Hide file tree
Showing 22 changed files with 2,119 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public enum Name {
IMPORTED_FUNCTIONS("Imported Functions", "Functions imported from other integrations",
List.of("Imported", "Function", "Library")),
AVAILABLE_FUNCTIONS("Available Functions", "Functions available in the library",
List.of("Available", "Function", "Library"));
List.of("Available", "Function", "Library")),
IMPORTED_TYPES("Imported Types", "Types imported from other integrations",
List.of("Imported", "Type", "Library")),
AVAILABLE_TYPES("Available Types", "Types available in the library",
List.of("Available", "Type", "Library"));

final String name;
final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public abstract class SearchCommand {
protected final int limit;
protected final int offset;
protected final boolean includeAvailableNodes;
protected final SearchDatabaseManager dbManager;
protected final DefaultViewHolder defaultViewHolder;
final SearchDatabaseManager dbManager;
final DefaultViewHolder defaultViewHolder;

protected static final String INCLUDE_AVAILABLE_FUNCTIONS_FLAG = "includeAvailableFunctions";
protected static final String INCLUDE_AVAILABLE_FUNCTIONS_FLAG = "includeAvailable";
protected static final String DATA_MAPPER_FILE_NAME = "data_mappings.bal";
private static final Gson GSON = new Gson();

Expand All @@ -65,7 +65,7 @@ public static SearchCommand from(Kind kind, Module module, LineRange position, M
return switch (kind) {
case FUNCTION -> new FunctionSearchCommand(module, position, queryMap);
case CONNECTOR -> new ConnectorSearchCommand(module, position, queryMap);
case TYPE -> null;
case TYPE -> new TypeSearchCommand(module, position, queryMap);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2025, 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.flowmodelgenerator.core.search;

import io.ballerina.flowmodelgenerator.core.model.AvailableNode;
import io.ballerina.flowmodelgenerator.core.model.Category;
import io.ballerina.flowmodelgenerator.core.model.Codedata;
import io.ballerina.flowmodelgenerator.core.model.Item;
import io.ballerina.flowmodelgenerator.core.model.Metadata;
import io.ballerina.flowmodelgenerator.core.model.NodeKind;
import io.ballerina.modelgenerator.commons.CommonUtils;
import io.ballerina.modelgenerator.commons.SearchResult;
import io.ballerina.projects.Module;
import io.ballerina.tools.text.LineRange;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Represents a command to search for types within a module. This class extends SearchCommand and provides functionality
* to search for both project-specific and library types.
*
* <p>
* The search includes:
* <li>Types within the current project/module </li>
* <li>Imported types from dependencies</li>
* <li>Available types from the standard library (if enabled)</li>
*
* <p>The search results are organized into different categories:</p>
* <li>CURRENT_INTEGRATION: Types from the current project</li>
* <li>IMPORTED_TYPES: Types from imported modules</li>
* <li>AVAILABLE_TYPES: Types available but not imported (optional)</li>
* </p>
*
* @see SearchCommand
* @since 2.0.0
*/
class TypeSearchCommand extends SearchCommand {

private final List<String> moduleNames;

public TypeSearchCommand(Module module, LineRange position, Map<String, String> queryMap) {
super(module, position, queryMap);

// Obtain the imported module names
module.getCompilation();
moduleNames = module.moduleDependencies().stream()
.map(moduleDependency -> moduleDependency.descriptor().name().packageName().value())
.toList();
}

@Override
protected List<Item> defaultView() {
List<SearchResult> searchResults = new ArrayList<>();
if (!moduleNames.isEmpty()) {
searchResults.addAll(dbManager.searchTypesByPackages(moduleNames, limit, offset));
}
buildLibraryNodes(searchResults, false);
return rootBuilder.build().items();
}

@Override
protected List<Item> search() {
List<SearchResult> typeSearchList = dbManager.searchTypes(query, limit, offset);
buildLibraryNodes(typeSearchList, true);
return rootBuilder.build().items();
}

@Override
protected Map<String, List<SearchResult>> fetchPopularItems() {
// Return empty value as required
return Collections.emptyMap();
}

private void buildLibraryNodes(List<SearchResult> typeSearchList, boolean includeAllResults) {
// Set the categories based on available flags
Category.Builder importedTypesBuilder = rootBuilder.stepIn(Category.Name.IMPORTED_TYPES);
Category.Builder availableTypesBuilder = null;
if (includeAllResults && includeAvailableNodes) {
availableTypesBuilder = rootBuilder.stepIn(Category.Name.AVAILABLE_TYPES);
}

// Add the library types
for (SearchResult searchResult : typeSearchList) {
SearchResult.Package packageInfo = searchResult.packageInfo();

// Skip if the module is not imported and we're not including all results
boolean isImportedModule = moduleNames.contains(packageInfo.name());
if (!isImportedModule && !includeAllResults) {
continue;
}

// Add the type to the respective category
String icon = CommonUtils.generateIcon(packageInfo.org(), packageInfo.name(), packageInfo.version());
Metadata metadata = new Metadata.Builder<>(null)
.label(searchResult.name())
.description(searchResult.description())
.icon(icon)
.build();
Codedata codedata = new Codedata.Builder<>(null)
.node(NodeKind.TYPEDESC)
.org(packageInfo.org())
.module(packageInfo.name())
.symbol(searchResult.name())
.version(packageInfo.version())
.build();
Category.Builder builder = isImportedModule ? importedTypesBuilder : availableTypesBuilder;
if (builder != null) {
builder.stepIn(packageInfo.name(), "", List.of())
.node(new AvailableNode(metadata, codedata, true));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down Expand Up @@ -520,6 +520,45 @@
"enabled": true
}
]
},
{
"metadata": {
"label": "time",
"description": "",
"keywords": []
},
"items": [
{
"metadata": {
"label": "utcFromString",
"description": "Converts from RFC 3339 timestamp (e.g., `2007-12-03T10:15:30.00Z`) to Utc.\n```ballerina\ntime:Utc|time:Error utc = time:utcFromString(\"2007-12-03T10:15:30.00Z\");\n```",
"icon": "https://bcentral-packageicons.azureedge.net/images/ballerina_time_2.6.0.png"
},
"codedata": {
"node": "FUNCTION_CALL",
"org": "ballerina",
"module": "time",
"symbol": "utcFromString",
"version": "2.6.0"
},
"enabled": true
},
{
"metadata": {
"label": "utcNow",
"description": "Returns the UTC representing the current time (current instant of the system clock in seconds from the epoch of `1970-01-01T00:00:00`).\n```ballerina\ntime:Utc utc = time:utcNow();\n```",
"icon": "https://bcentral-packageicons.azureedge.net/images/ballerina_time_2.6.0.png"
},
"codedata": {
"node": "FUNCTION_CALL",
"org": "ballerina",
"module": "time",
"symbol": "utcNow",
"version": "2.6.0"
},
"enabled": true
}
]
}
]
},
Expand Down Expand Up @@ -602,45 +641,6 @@
"enabled": true
}
]
},
{
"metadata": {
"label": "time",
"description": "",
"keywords": []
},
"items": [
{
"metadata": {
"label": "utcFromString",
"description": "Converts from RFC 3339 timestamp (e.g., `2007-12-03T10:15:30.00Z`) to Utc.\n```ballerina\ntime:Utc|time:Error utc = time:utcFromString(\"2007-12-03T10:15:30.00Z\");\n```",
"icon": "https://bcentral-packageicons.azureedge.net/images/ballerina_time_2.6.0.png"
},
"codedata": {
"node": "FUNCTION_CALL",
"org": "ballerina",
"module": "time",
"symbol": "utcFromString",
"version": "2.6.0"
},
"enabled": true
},
{
"metadata": {
"label": "utcNow",
"description": "Returns the UTC representing the current time (current instant of the system clock in seconds from the epoch of `1970-01-01T00:00:00`).\n```ballerina\ntime:Utc utc = time:utcNow();\n```",
"icon": "https://bcentral-packageicons.azureedge.net/images/ballerina_time_2.6.0.png"
},
"codedata": {
"node": "FUNCTION_CALL",
"org": "ballerina",
"module": "time",
"symbol": "utcNow",
"version": "2.6.0"
},
"enabled": true
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "sum",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "UNION SELECT NULL,@@version;--",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "NEAR(hello) OR 1=1--",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "test) FROM sqlite_master;--",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "test' UNION SELECT name FROM sqlite_master WHERE type='table';--",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "* OR rowid < 0;--",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "json to xml",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "log",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "parseString",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "println",
"includeAvailableFunctions": "true"
"includeAvailable": "true"
},
"categories": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"queryMap": {
"q": "write to a file",
"includeAvailableFunctions": "true",
"includeAvailable": "true",
"limit": "12",
"offset": "0"
},
Expand Down
Loading

0 comments on commit ce6c895

Please sign in to comment.