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]: New code action for extracting expressions to let variables within expression bodied functions #39295

Closed
madushajg opened this issue Jan 18, 2023 · 1 comment
Labels
Area/CodeAction Language Server Code Actions IceBox Older issues that are not being actively worked on but may be revisited in the future. Priority/Low Team/LanguageServer Language Server Implementation related issues. #Compiler Type/NewFeature

Comments

@madushajg
Copy link
Contributor

Description

$Subject
The Data Mapper users should be able to extract expressions into local variables(let declarations) and reuse within the function body.

The requirement is described in the following use case.

type Person record {
    string id;
    string firstName;
    string lastName;
    int age;
    string country;
    ContactMedium[] contactMediums;
};

type Student record {
    string id;
    string visaType;
    boolean isForeign;
    ContactAddress[] addresses;
    string[] countriesLived;
};

type Characteristic record {
    string emailAddress?;
    string phoneNumber?;
    ContactAddress address?;
};

type ContactMedium record {
    string mediumType;
    Characteristic characteristic;
};

type ContactAddress record {
    string address1;
    string name;
    string city;
    string zip;
    string country;
};

function transform(Person person) returns Student => {
    id: person.id + (person.country != "LK" ? "F" : ""),
    visaType: person.country != "LK" ? "D-4" : person.firstName,
    isForeign: person.country != "LK",
    addresses: from ContactMedium m in person.contactMediums
        where m.mediumType == "ADDRESS"
        select {
            zip: <string>m.characteristic.address?.zip,
            country: person.country == <string>m.characteristic.address?.country ? <string>m.characteristic.address?.country : person.country,
            city: <string>m.characteristic.address?.city,
            address1: <string>m.characteristic.address?.address1,
            name: <string>m.characteristic.address?.name 
        },
    countriesLived: from ContactMedium m in person.contactMediums
        where m.mediumType == "ADDRESS"
        select <string>m.characteristic.address?.country
};

In this case the user need to extract the below three expressions to local variables to avoid repeating the logic.

  1. person.country != "LK"
  2. <string>m.characteristic.address?.country
  3. from ContactMedium m in person.contactMediums where m.mediumType == "ADDRESS" select {...}

After extracting those to local variables, the code can be simplified like the following.

function transformWithLocalVariables(Person person) returns Student => let var isForeign = person.country != "LK",
    ContactAddress[] addresses = from ContactMedium m in person.contactMediums
        where m.mediumType == "ADDRESS"
        let string country = <string>m.characteristic.address?.country
        select {
            zip: <string>m.characteristic.address?.zip,
            country: person.country == country ? country : person.country,
            city: <string>m.characteristic.address?.city,
            address1: <string>m.characteristic.address?.address1,
            name: <string>m.characteristic.address?.name 
        }
    in {
        id: person.id + (isForeign ? "F" : ""),
        visaType: isForeign ? "D-4" : "n/a",
        isForeign: isForeign,
        addresses: addresses,
        countriesLived: from ContactAddress address in addresses
            select address.country
    };

Please note that when extracting expressions within query expressions, users should have two options to locate the local variable.
ie: <string>m.characteristic.address?.country

In above simplified function, this has placed as a let clause within the expressions. But the user can also place this at the top of the function right after the addresses local variable.

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@madushajg madushajg added Type/NewFeature Team/LanguageServer Language Server Implementation related issues. #Compiler Area/CodeAction Language Server Code Actions labels Jan 18, 2023
@IMS94 IMS94 changed the title [New Feature]: New code action for 'extracting expressions to let variables` within expression bodied functions [New Feature]: New code action for extracting expressions to let variables within expression bodied functions Jan 19, 2023
@anupama-pathirage anupama-pathirage added the IceBox Older issues that are not being actively worked on but may be revisited in the future. label Nov 20, 2024
@anupama-pathirage
Copy link
Contributor

We are closing this issue due to inactivity. If you need further assistance or have additional input, feel free to reopen it. Thank you for helping us maintain a relevant and focused issue list!

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 IceBox Older issues that are not being actively worked on but may be revisited in the future. Priority/Low Team/LanguageServer Language Server Implementation related issues. #Compiler Type/NewFeature
Projects
Archived in project
Development

No branches or pull requests

3 participants