-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor migration libraries API #672
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
Merged
xlight05
merged 1 commit into
ballerina-platform:copilot-library-unify
from
VellummyilumVinoth:refactor-libraries-migration
Feb 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ClassSymbolHandler.java
This file contains hidden or 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,112 @@ | ||
| /* | ||
| * Copyright (c) 2026, 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; | ||
|
|
||
| import io.ballerina.compiler.api.SemanticModel; | ||
| import io.ballerina.compiler.api.symbols.ClassSymbol; | ||
| import io.ballerina.compiler.api.symbols.Qualifier; | ||
| import io.ballerina.flowmodelgenerator.core.model.Client; | ||
| import io.ballerina.flowmodelgenerator.core.model.LibraryFunction; | ||
| import io.ballerina.flowmodelgenerator.core.model.TypeDef; | ||
| import io.ballerina.modelgenerator.commons.FunctionData; | ||
| import io.ballerina.modelgenerator.commons.FunctionDataBuilder; | ||
| import io.ballerina.modelgenerator.commons.ModuleInfo; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Handler for processing CLASS symbols from semantic model. | ||
| * Extracts client classes and normal classes with their methods. | ||
| * | ||
| * @since 1.0.1 | ||
| */ | ||
| public class ClassSymbolHandler { | ||
|
|
||
| private ClassSymbolHandler() { | ||
| // Prevent instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Processes a CLASS symbol and returns either a Client or TypeDef. | ||
| * | ||
| * @param classSymbol the class symbol to process | ||
| * @param semanticModel the semantic model | ||
| * @param moduleInfo the module information | ||
| * @param org the organization name | ||
| * @param packageName the package name | ||
| * @return Optional containing either Client or TypeDef, or empty if not public | ||
| */ | ||
| public static Optional<Object> process(ClassSymbol classSymbol, | ||
| SemanticModel semanticModel, | ||
| ModuleInfo moduleInfo, | ||
| String org, | ||
| String packageName) { | ||
| // Process only PUBLIC classes: CLIENT classes (connectors) and normal classes | ||
| if (!classSymbol.qualifiers().contains(Qualifier.PUBLIC)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| boolean isClient = classSymbol.qualifiers().contains(Qualifier.CLIENT); | ||
| String className = classSymbol.getName().orElse(isClient ? "Client" : "Class"); | ||
|
|
||
| FunctionData.Kind classKind = isClient ? FunctionData.Kind.CONNECTOR : FunctionData.Kind.CLASS_INIT; | ||
|
|
||
| FunctionData classData = new FunctionDataBuilder() | ||
| .semanticModel(semanticModel) | ||
| .moduleInfo(moduleInfo) | ||
| .name(className) | ||
| .parentSymbol(classSymbol) | ||
| .functionResultKind(classKind) | ||
| .build(); | ||
|
|
||
| List<LibraryFunction> functions = new ArrayList<>(); | ||
|
|
||
| // Add the constructor/init function first | ||
| LibraryFunction constructor = LibraryModelConverter.functionDataToModel(classData, org, packageName); | ||
| constructor.setName("init"); // Override name to "init" for constructor | ||
| functions.add(constructor); | ||
|
|
||
| // Then add all other methods (remote functions, resource functions, etc.) | ||
| List<FunctionData> classMethods = new FunctionDataBuilder() | ||
| .semanticModel(semanticModel) | ||
| .moduleInfo(moduleInfo) | ||
| .parentSymbolType(className) | ||
| .parentSymbol(classSymbol) | ||
| .buildChildNodes(); | ||
|
|
||
| for (FunctionData method : classMethods) { | ||
| LibraryFunction methodFunc = LibraryModelConverter.functionDataToModel(method, org, packageName); | ||
| functions.add(methodFunc); | ||
| } | ||
|
|
||
| if (isClient) { | ||
| Client client = new Client(className, classData.description()); | ||
| client.setFunctions(functions); | ||
| return Optional.of(client); | ||
| } else { | ||
| TypeDef typeDef = new TypeDef(); | ||
| typeDef.setName(className); | ||
| typeDef.setDescription(classData.description()); | ||
| typeDef.setFunctions(functions); | ||
| return Optional.of(typeDef); | ||
| } | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
...erator-core/src/main/java/io/ballerina/flowmodelgenerator/core/ConstantSymbolHandler.java
This file contains hidden or 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,71 @@ | ||
| /* | ||
| * Copyright (c) 2026, 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; | ||
|
|
||
| import io.ballerina.compiler.api.symbols.ConstantSymbol; | ||
| import io.ballerina.compiler.api.symbols.Qualifier; | ||
| import io.ballerina.compiler.api.symbols.TypeSymbol; | ||
| import io.ballerina.compiler.api.values.ConstantValue; | ||
| import io.ballerina.flowmodelgenerator.core.model.Type; | ||
| import io.ballerina.flowmodelgenerator.core.model.TypeDef; | ||
| import io.ballerina.modelgenerator.commons.TypeDefData; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Handler for processing CONSTANT symbols from semantic model. | ||
| * | ||
| * @since 1.0.1 | ||
| */ | ||
| public class ConstantSymbolHandler { | ||
|
|
||
| private ConstantSymbolHandler() { | ||
| } | ||
|
|
||
| public static Optional<TypeDef> process(ConstantSymbol constantSymbol, | ||
| String org, | ||
| String packageName) { | ||
| if (!constantSymbol.qualifiers().contains(Qualifier.PUBLIC)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| TypeDefData constantData = TypeDefDataBuilder.buildFromConstant(constantSymbol); | ||
| TypeDef typeDef = LibraryModelConverter.typeDefDataToModel(constantData, org, packageName); | ||
|
|
||
| // Add varType using ConstantValue | ||
| String varTypeName = ""; | ||
| Object constValue = constantSymbol.constValue(); | ||
| if (constValue instanceof ConstantValue constantValue) { | ||
| varTypeName = constantValue.valueType().typeKind().getName(); | ||
| } | ||
|
|
||
| // Fallback to type descriptor if constValue is null or not ConstantValue | ||
| if (varTypeName.isEmpty()) { | ||
| TypeSymbol typeSymbol = constantSymbol.typeDescriptor(); | ||
| if (typeSymbol != null && !typeSymbol.signature().isEmpty()) { | ||
| varTypeName = typeSymbol.signature(); | ||
| } | ||
| } | ||
|
|
||
| Type varType = new Type(varTypeName); | ||
| typeDef.setVarType(varType); | ||
|
|
||
| return Optional.of(typeDef); | ||
| } | ||
| } | ||
134 changes: 134 additions & 0 deletions
134
...erator-core/src/main/java/io/ballerina/flowmodelgenerator/core/CopilotLibraryManager.java
This file contains hidden or 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,134 @@ | ||
| /* | ||
| * Copyright (c) 2026, 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; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonElement; | ||
| import io.ballerina.compiler.api.SemanticModel; | ||
| import io.ballerina.flowmodelgenerator.core.model.Library; | ||
| import io.ballerina.flowmodelgenerator.core.model.Service; | ||
| import io.ballerina.modelgenerator.commons.ModuleInfo; | ||
| import io.ballerina.modelgenerator.commons.PackageUtil; | ||
|
|
||
| import java.io.IOException; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Core orchestrator for Copilot library operations. | ||
| * Coordinates between database access, symbol processing, and service loading. | ||
| * | ||
| * @since 1.0.1 | ||
| */ | ||
| public class CopilotLibraryManager { | ||
|
|
||
| private static final Gson GSON = new Gson(); | ||
|
|
||
| /** | ||
| * Loads all libraries from the database. | ||
| * Returns a list of libraries with name and description only. | ||
| * | ||
| * @return List of Library objects containing name and description | ||
| */ | ||
| public List<Library> loadLibrariesFromDatabase() { | ||
| List<Library> libraries = new ArrayList<>(); | ||
|
|
||
| try { | ||
| Map<String, String> packageToDescriptionMap = LibraryDatabaseAccessor.loadAllPackages(); | ||
|
|
||
| for (Map.Entry<String, String> entry : packageToDescriptionMap.entrySet()) { | ||
| Library library = new Library(entry.getKey(), entry.getValue()); | ||
| libraries.add(library); | ||
| } | ||
| } catch (IOException | SQLException e) { | ||
| throw new RuntimeException("Failed to load libraries from database: " + e.getMessage(), e); | ||
| } | ||
|
|
||
| return libraries; | ||
| } | ||
|
|
||
| /** | ||
| * Loads filtered libraries using the semantic model. | ||
| * Returns libraries with full details including clients, functions, typedefs, and services. | ||
| * | ||
| * @param libraryNames Array of library names in "org/package_name" format to filter | ||
| * @return List of Library objects with complete information | ||
| */ | ||
| public List<Library> loadFilteredLibraries(String[] libraryNames) { | ||
| List<Library> libraries = new ArrayList<>(); | ||
|
|
||
| for (String libraryName : libraryNames) { | ||
| // Parse library name "org/package_name" | ||
| String[] parts = libraryName.split("/"); | ||
| if (parts.length != 2) { | ||
| continue; // Skip invalid format | ||
| } | ||
| String org = parts[0]; | ||
| String packageName = parts[1]; | ||
|
|
||
| // Create module info (use latest version by passing null) | ||
| ModuleInfo moduleInfo = new ModuleInfo(org, packageName, org + "/" + | ||
| packageName, null); | ||
|
|
||
| // Get semantic model for the module | ||
| Optional<SemanticModel> optSemanticModel = PackageUtil.getSemanticModel(org, packageName); | ||
| if (optSemanticModel.isEmpty()) { | ||
| continue; // Skip if semantic model not found | ||
| } | ||
|
|
||
| SemanticModel semanticModel = optSemanticModel.get(); | ||
|
|
||
| // Get the package description from database | ||
| String description = LibraryDatabaseAccessor.getPackageDescription(org, packageName).orElse(""); | ||
|
|
||
| // Create library object | ||
| Library library = new Library(libraryName, description); | ||
|
|
||
| // Process module symbols to extract clients, functions, and typedefs | ||
| SymbolProcessor.SymbolProcessingResult symbolResult = SymbolProcessor.processModuleSymbols( | ||
| semanticModel, | ||
| moduleInfo, | ||
| org, | ||
| packageName | ||
| ); | ||
|
|
||
| library.setClients(symbolResult.getClients()); | ||
| library.setFunctions(symbolResult.getFunctions()); | ||
| library.setTypeDefs(symbolResult.getTypeDefs()); | ||
|
|
||
| // Load services from both inbuilt triggers and generic services | ||
| // For now, keep using JSON and convert to Service POJOs | ||
| JsonArray servicesJson = ServiceLoader.loadAllServices(libraryName); | ||
| List<Service> services = new ArrayList<>(); | ||
| for (JsonElement serviceElement : servicesJson) { | ||
| Service service = GSON.fromJson(serviceElement, Service.class); | ||
| services.add(service); | ||
| } | ||
| library.setServices(services); | ||
|
|
||
| libraries.add(library); | ||
| } | ||
|
|
||
| return libraries; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
...-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/EnumSymbolHandler.java
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * Copyright (c) 2026, 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; | ||
|
|
||
| import io.ballerina.compiler.api.symbols.EnumSymbol; | ||
| import io.ballerina.compiler.api.symbols.Qualifier; | ||
| import io.ballerina.flowmodelgenerator.core.model.TypeDef; | ||
| import io.ballerina.modelgenerator.commons.TypeDefData; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Handler for processing ENUM symbols from semantic model. | ||
| * | ||
| * @since 1.0.1 | ||
| */ | ||
| public class EnumSymbolHandler { | ||
|
|
||
| private EnumSymbolHandler() { | ||
| } | ||
|
|
||
| public static Optional<TypeDef> process(EnumSymbol enumSymbol, | ||
| String org, | ||
| String packageName) { | ||
| if (!enumSymbol.qualifiers().contains(Qualifier.PUBLIC)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| TypeDefData enumData = TypeDefDataBuilder.buildFromEnum(enumSymbol); | ||
| TypeDef typeDef = LibraryModelConverter.typeDefDataToModel(enumData, org, packageName); | ||
| return Optional.of(typeDef); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if theres a kind or type which we can do instanceof. There is, we should follow that approach