Skip to content
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

[New Feature]: Convert to configurable code action #41345

Open
malinthar opened this issue Sep 5, 2023 · 5 comments · May be fixed by #42983
Open

[New Feature]: Convert to configurable code action #41345

malinthar opened this issue Sep 5, 2023 · 5 comments · May be fixed by #42983
Assignees
Labels
Area/CodeAction Language Server Code Actions Team/LanguageServer Language Server Implementation related issues. #Compiler Type/NewFeature
Milestone

Comments

@malinthar
Copy link
Contributor

Description

Consider the following code.

import ballerina/lang.array;
import ballerinax/googleapis.drive;
import ballerinax/googleapis.sheets;
import ballerinax/openai.images;

string googleAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2V4YW1wbGUu";
string openAIToken = "i8vYXBpLmV4YW1wbGUuY29tL2NhbGFuZGFyL3YxLyIsInN1OjE";
string sheetId = "id";
string sheetName = "name";
string gDriveFolderId = "id";

public function main() returns error? {
    sheets:Client gSheets = check new ({auth: {token: googleAccessToken}});
    images:Client openAIImages = check new ({auth: {token: openAIToken}});
    drive:Client gDrive = check new ({auth: {token: googleAccessToken}});

    sheets:Column range = check gSheets->getColumn(sheetId, sheetName, "A");
    foreach var cell in range.values {
        string prompt = cell.toString();
        images:CreateImageRequest imagePrompt = {
            prompt,
            response_format: "b64_json"
        };
        images:ImagesResponse imageRes = check openAIImages->/images/generations.post(imagePrompt);
        string? encodedImage = imageRes.data[0].b64_json;
        if encodedImage is () {
            return error(string `Failed to generate image for prompt: ${prompt}`);
        }

        // Decode the Base64 string and store image in Google Drive
        byte[] imageBytes = check array:fromBase64(encodedImage);
        _ = check gDrive->uploadFileUsingByteArray(imageBytes, string `${cell}.png`, gDriveFolderId);
    }
}

In the code above I have used module variables to define secrets. A new code action to convert them to configurables would be useful.

The code action should add the secrets to the Config.toml file while converting the module vars to configurable variables.

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Editor

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@malinthar malinthar added Type/Improvement Team/LanguageServer Language Server Implementation related issues. #Compiler Area/CodeAction Language Server Code Actions labels Sep 5, 2023
@ballerina-bot ballerina-bot added needTriage The issue has to be inspected and labeled manually userCategory/Editor labels Sep 5, 2023
@nipunayf nipunayf removed needTriage The issue has to be inspected and labeled manually userCategory/Editor labels Mar 7, 2024
@nipunayf nipunayf changed the title [Improvement]: Add a new code action to convert secrets to configurables [New Feature]: Add a new code action to convert secrets to configurables May 16, 2024
@nipunayf
Copy link
Contributor

Upon the execution of the code action, we should provide two options to capture the user preference:

  1. Whether the user wants the value as a default value.
  2. Whether the user wants to move the value to the Config.toml.

@LakshanWeerasinghe
Copy link
Contributor

LakshanWeerasinghe commented Jun 20, 2024

To make this feature complete we need to implement three code actions.

1. Convert to configurable code action

The code action targets a module variable defined in the source code. The cursor must be placed within the literal expression of the variable user want to convert. Then it transforms the variable declaration by adding the configurable keyword.
Before Applying the Code Action:

string githubPAT = "this-is-my-fake-github-pat";

After Applying the Code Action:

configurable string githubPAT = "this-is-my-fake-github-pat";

2. Extract to configurable code action
This code action can be broken into two parts.

  • Extract a single literal value
    This part functions similarly to the "extract to local variable" code action. When a single literal value is selected, the user will be prompted with a rename popup to update the configurable variable name.
    Before Applying the Code Action:
final mysql:Client myDb = check new ("<CURSOR>localhost", "my_user", "dummy_password", "dummy_database");

After Applying the Code Action:

configurable string configurableVar = "localhost";
final mysql:Client myDb = check new (configurableVar, "my_user", "dummy_password", "dummy_database");
  • Extract a Selected Range of Function Arguments into Configurable Variables
    In this part, all arguments within the selected cursor range will be extracted into configurable variables. The names of these configurable variables will be derived from the function parameter names.
    Before Applying the Code Action:
final mysql:Client myDb2 = check new (<CURSOR_START>"localhost", "my_user", "dummy_password", "dummy_database"<CURSOR_END>);

After Applying the Code Action:

configurable string host = "localhost";
configurable string username = "my_user";
configurable string password = "dummy_password";
configurable string database = "dummy_database";
final mysql:Client myDb2 = check new (host, username, password, database);

3. Add Configurable to Config.toml

This code action will perform the following functions:

  • Add Missing Configurables: It checks for the presence of required configurables in the Config.toml file. If any configurables are missing, it adds them to the file.
  • Create Config.toml if Absent: If the Config.toml file does not exist, this code action creates a new Config.toml file and adds the required configurables to it.

@LakshanWeerasinghe
Copy link
Contributor

After an offline discussion with @KavinduZoysa, we came to a conclusion to support Convert to configurable code action only for these expression syntax kinds BOOLEAN_LITERAL, NUMERIC_LITERAL, STRING_LITERAL, MAPPING_CONSTRCUTOR and LIST_CONSTRUCTOR_EXPRESSION.

@MaryamZi
Copy link
Member

After an offline discussion with @KavinduZoysa, we came to a conclusion to support Convert to configurable code action only for these expression syntax kinds BOOLEAN_LITERAL, NUMERIC_LITERAL, STRING_LITERAL, MAPPING_CONSTRCUTOR and LIST_CONSTRUCTOR_EXPRESSION.

Any particular reason to support only these? I think it would be useful to have the support also (at least) for unary expressions (e.g., -1 is a unary expression), string template expressions, table constructor expressions, etc.

Technically we could support this with anything that has a static type belonging to anydata, right? I get that it may not make sense for all such expression though - e.g., is checks - and in such cases it can be a bit of a distraction.

@LakshanWeerasinghe
Copy link
Contributor

LakshanWeerasinghe commented Jun 24, 2024

After an offline discussion with @KavinduZoysa, we came to a conclusion to support Convert to configurable code action only for these expression syntax kinds BOOLEAN_LITERAL, NUMERIC_LITERAL, STRING_LITERAL, MAPPING_CONSTRCUTOR and LIST_CONSTRUCTOR_EXPRESSION.

Any particular reason to support only these? I think it would be useful to have the support also (at least) for unary expressions (e.g., -1 is a unary expression), string template expressions, table constructor expressions, etc.

Technically we could support this with anything that has a static type belonging to anydata, right? I get that it may not make sense for all such expression though - e.g., is checks - and in such cases it can be a bit of a distraction.

One reason to suggest only above syntax kinds was others won't make any sense. As you mentioned we can add UNARY_EXPRESSION, STRING_TEMPLATE_EXPRESSION, XML_TEMPLATE_EXPRESSION and TABLE_CONSTRUCTOR_EXPRESSOION.

@LakshanWeerasinghe LakshanWeerasinghe changed the title [New Feature]: Add a new code action to convert secrets to configurables [New Feature]: Convert to configurable code action Jun 24, 2024
@LakshanWeerasinghe LakshanWeerasinghe linked a pull request Jun 25, 2024 that will close this issue
13 tasks
@LakshanWeerasinghe LakshanWeerasinghe added this to the 2201.10.0 milestone Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/CodeAction Language Server Code Actions Team/LanguageServer Language Server Implementation related issues. #Compiler Type/NewFeature
Projects
Status: PR Sent
Development

Successfully merging a pull request may close this issue.

6 participants