Skip to content

[dotnet] [bidi] Add strongly-typed LocalValue.ConvertFrom overloads #15532

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
merged 15 commits into from
Apr 13, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Mar 28, 2025

User description

Description 

Add strongly-typed LocalValue.ConvertFrom overloads

Motivation and Context

Prevents users from being forced to guess which types we happen to support.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Added strongly-typed LocalValue.ConvertFrom overloads for better type safety.

  • Refactored ConvertFrom logic to delegate to specific type overloads.

  • Introduced new methods for handling various data structures and primitives.

  • Improved code maintainability and readability by reducing duplication.


Changes walkthrough 📝

Relevant files
Enhancement
LocalValue.cs
Refactored and extended `LocalValue.ConvertFrom` functionality

dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs

  • Added strongly-typed overloads for ConvertFrom method.
  • Refactored implicit operators to use ConvertFrom methods.
  • Introduced specialized methods for handling primitives and
    collections.
  • Enhanced maintainability by reducing redundant code and improving
    structure.
  • +219/-63

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Redundant Code

    The convertedArray variable on line 207 is created but then not used in the return statement on line 208, which creates a new array conversion instead.

    LocalValue[] convertedArray = Array.ConvertAll(values, ConvertFrom);
    return new ArrayLocalValue(Array.ConvertAll(values, ConvertFrom));
    Parameter Naming

    In several nullable type conversion methods (lines 103-156), the parameter name 'b' is used for the unwrapped value, which is inconsistent with the parameter type (should use more descriptive names like 'intValue' instead of 'b' for integers).

    public static LocalValue ConvertFrom(bool? value)
    {
        if (value is bool b)
        {
            return new BooleanLocalValue(b);
        }
    
        return new NullLocalValue();
    }
    
    public static LocalValue ConvertFrom(int value)
    {
        return new NumberLocalValue(value);
    }
    
    public static LocalValue ConvertFrom(int? value)
    {
        if (value is int b)
        {
            return new NumberLocalValue(b);
        }
    
        return new NullLocalValue();
    }
    
    public static LocalValue ConvertFrom(double value)
    {
        return new NumberLocalValue(value);
    }
    
    public static LocalValue ConvertFrom(double? value)
    {
        if (value is double b)
        {
            return new NumberLocalValue(b);
        }
    
        return new NullLocalValue();
    }
    
    public static LocalValue ConvertFrom(long value)
    {
        return new NumberLocalValue(value);
    }
    
    public static LocalValue ConvertFrom(long? value)
    {
        if (value is long b)
        {
            return new NumberLocalValue(b);
        }
    
        return new NullLocalValue();
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 28, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Remove redundant array conversion
    Suggestion Impact:The commit completely removed the problematic method ConvertFrom(object?[]?) that contained the redundant array conversion. Instead of fixing the specific issue, the commit refactored the code to use generic methods, eliminating the need for multiple specialized methods including the one with the redundant conversion.

    code diff:

    -    public static LocalValue ConvertFrom(object?[]? values)
    -    {
    -        if (values is null)
    -        {
    -            return new NullLocalValue();
    -        }
    -
    -        LocalValue[] convertedArray = Array.ConvertAll(values, ConvertFrom);
    -        return new ArrayLocalValue(Array.ConvertAll(values, ConvertFrom));
    -    }

    The method is converting the array twice unnecessarily. The convertedArray
    variable is created but never used, and then the conversion is performed again
    in the return statement.

    dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs [200-209]

     public static LocalValue ConvertFrom(object?[]? values)
     {
         if (values is null)
         {
             return new NullLocalValue();
         }
     
         LocalValue[] convertedArray = Array.ConvertAll(values, ConvertFrom);
    -    return new ArrayLocalValue(Array.ConvertAll(values, ConvertFrom));
    +    return new ArrayLocalValue(convertedArray);
     }

    [Suggestion has been applied]

    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies a significant bug where the array is converted twice unnecessarily. The code creates a convertedArray variable but then ignores it, performing the conversion again in the return statement. This wastes CPU cycles and memory, potentially impacting performance for large arrays.

    High
    • Update

    @nvborisenko
    Copy link
    Member

    Our big concern here is whether we should support nullable primitive types. I guess it is more philosophic question, from implementation perspective for us it doesn't matter. For end users it is also kind of "doesn't matter".

    @nvborisenko
    Copy link
    Member

    Thank you, Mike! Let's move on and improve later when necessary.

    @nvborisenko nvborisenko merged commit 5534d2e into SeleniumHQ:trunk Apr 13, 2025
    10 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants