Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Jul 15, 2025

Link issues

fixes #6419

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Introduce a typed socket data converter interface and integrate it into the data package adapter pipeline, refactoring the conversion methods and updating extensions and tests to use the new approach

New Features:

  • Introduce a generic ISocketDataConverter interface and its abstract base class for socket data conversion

Enhancements:

  • Refactor IDataPackageAdapter and DataPackageAdapter to use the new ISocketDataConverter for generic TryConvertTo support
  • Extend ITcpSocketClient.SetDataPackageAdapter extension to accept a socket data converter parameter

Tests:

  • Update unit tests to utilize the new converter interface and verify conversion results

@bb-auto bb-auto bot added the enhancement New feature or request label Jul 15, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 15, 2025

Reviewer's Guide

This PR introduces a generic ISocketDataConverter interface (with a base class) to unify socket data conversion, refactors IDataPackageAdapter and DataPackageAdapter to use the new converter-based TryConvertTo API, updates the ITcpSocketClientExtensions to accept a converter parameter, and aligns unit tests to the new API.

Class diagram for ISocketDataConverter and related classes

classDiagram
    class ISocketDataConverter~TEntity~ {
        +bool TryConvertTo(ReadOnlyMemory<byte> data, out TEntity? entity)
    }
    class SocketDataConverterBase~TEntity~ {
        <<abstract>>
        +bool TryConvertTo(ReadOnlyMemory<byte> data, out TEntity? entity)
    }
    ISocketDataConverter~TEntity~ <|.. SocketDataConverterBase~TEntity~

    class IDataPackageAdapter {
        +ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token)
        +bool TryConvertTo~TEntity~(ReadOnlyMemory<byte> data, ISocketDataConverter~TEntity~ socketDataConverter, out TEntity? entity)
    }
    class DataPackageAdapter {
        +ValueTask HandlerAsync(ReadOnlyMemory<byte> data, CancellationToken token)
        +bool TryConvertTo~TEntity~(ReadOnlyMemory<byte> data, ISocketDataConverter~TEntity~ socketDataConverter, out TEntity? entity)
    }
    IDataPackageAdapter <|.. DataPackageAdapter
Loading

Class diagram for ITcpSocketClientExtensions.SetDataPackageAdapter changes

classDiagram
    class ITcpSocketClientExtensions {
        +void SetDataPackageAdapter~TEntity~(ITcpSocketClient client, IDataPackageAdapter adapter, ISocketDataConverter~TEntity~ socketDataConverter, Func<TEntity?, Task> callback)
    }
    class ITcpSocketClient {
        +ReceivedCallBack
    }
    ITcpSocketClientExtensions ..> ITcpSocketClient : uses
    ITcpSocketClientExtensions ..> IDataPackageAdapter : uses
    ITcpSocketClientExtensions ..> ISocketDataConverter~TEntity~ : uses
Loading

File-Level Changes

Change Details Files
Add generic data converter interface and base class
  • Create ISocketDataConverter with TryConvertTo signature
  • Implement abstract SocketDataConverterBase as a base converter
src/BootstrapBlazor/Services/TcpSocket/DataConverter/ISocketDataConverter.cs
src/BootstrapBlazor/Services/TcpSocket/DataConverter/SocketDataConverterBase.cs
Refactor IDataPackageAdapter to support generic converters
  • Change TryConvertTo signature to be generic and accept an ISocketDataConverter parameter
  • Update XML docs to reference the generic converter and default entity handling
src/BootstrapBlazor/Services/TcpSocket/DataPackageAdapter/IDataPackageAdapter.cs
Implement converter-based TryConvertTo in DataPackageAdapter
  • Update TryConvertTo to accept ISocketDataConverter and return TEntity
  • Delegate conversion to socketDataConverter.TryConvertTo and assign the output
  • Return success based on whether the result is non-null
src/BootstrapBlazor/Services/TcpSocket/DataPackageAdapter/DataPackageAdapter.cs
Extend SetDataPackageAdapter to use socket data converters
  • Add a socketDataConverter parameter to the extension method signature
  • Revise XML docs and method summary to mention the new converter
  • Replace adapter.TryConvertTo calls with socketDataConverter.TryConvertTo in callback
src/BootstrapBlazor/Extensions/ITcpSocketClientExtensions.cs
Update unit tests to align with the new converter-based API
  • Replace custom adapter subclasses with DataPackageAdapter plus MockEntitySocketDataConverter
  • Adjust SetDataPackageAdapter calls to include the converter argument
  • Remove obsolete adapter classes and refactor test converter implementations
  • Update TryConvertTo assertions and expected entity checks
test/UnitTest/Services/TcpSocketFactoryTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6419 Create an ISocketDataConverter interface.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto bot added this to the 9.8.0 milestone Jul 15, 2025
sourcery-ai[bot]
sourcery-ai bot previously approved these changes Jul 15, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ArgoZhang - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Services/TcpSocket/DataPackageAdapter/DataPackageAdapter.cs:56` </location>
<code_context>
     {
-        entity = null;
-        return false;
+        entity = default;
+        if (socketDataConverter.TryConvertTo(data, out var v))
+        {
+            entity = v;
+        }
+        return entity != null;
     }

</code_context>

<issue_to_address>
Returning 'entity != null' may not be correct for value types.

For value types, 'entity != null' will always be true, even if conversion fails. Use the result of TryConvertTo or another method to indicate success for value types.
</issue_to_address>

### Comment 2
<location> `test/UnitTest/Services/TcpSocketFactoryTest.cs:679` </location>
<code_context>
-        var result = adapter2.TryConvertTo(data, out var t);
-        Assert.False(result);
-        Assert.Null(t);
+        var result = adapter2.TryConvertTo(data, new MockEntitySocketDataConverter(), out var t);
+        Assert.True(result);
+        Assert.NotNull(t);
+        Assert.Equal([1, 2, 3, 4, 5], entity.Header);
     }

</code_context>

<issue_to_address>
Test coverage for failed conversion scenarios is missing.

Please add a test case where TryConvertTo returns false and the entity is null, to ensure failed conversions are properly handled.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        var result = adapter2.TryConvertTo(data, new MockEntitySocketDataConverter(), out var t);
        Assert.True(result);
        Assert.NotNull(t);
        Assert.Equal([1, 2, 3, 4, 5], entity.Header);
    }

=======
        var result = adapter2.TryConvertTo(data, new MockEntitySocketDataConverter(), out var t);
        Assert.True(result);
        Assert.NotNull(t);
        Assert.Equal([1, 2, 3, 4, 5], entity.Header);
    }

    [Fact]
    public void TryConvertTo_FailedConversion_ReturnsFalseAndNullEntity()
    {
        // Arrange
        var adapter = new DataPackageAdapter();
        var invalidData = new byte[] { 0xFF, 0xFF }; // Use data that will fail conversion
        var converter = new MockEntitySocketDataConverter();

        // Act
        var result = adapter.TryConvertTo(invalidData, converter, out var entity);

        // Assert
        Assert.False(result);
        Assert.Null(entity);
    }
>>>>>>> REPLACE

</suggested_fix>

### Comment 3
<location> `test/UnitTest/Services/TcpSocketFactoryTest.cs:714` </location>
<code_context>
         await tcs.Task;

-        Assert.Null(entity);
+        Assert.NotNull(entity);
     }

</code_context>

<issue_to_address>
Test assertion may not match the intended error scenario.

Please clarify whether this test is meant to check for a failed conversion (entity should be null). If not, consider renaming the test or adding a comment to explain the expected behavior. Also, ensure there is a test for the null entity case after a failed conversion.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Jul 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4fffebd) to head (90f2bb8).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #6420   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          722       722           
  Lines        31844     31846    +2     
  Branches      4492      4493    +1     
=========================================
+ Hits         31844     31846    +2     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ArgoZhang ArgoZhang merged commit cdc930c into main Jul 15, 2025
4 checks passed
@ArgoZhang ArgoZhang deleted the refactor-adapter branch July 15, 2025 04:44
ArgoZhang added a commit that referenced this pull request Jul 16, 2025
…interface (#6422)

* feat(ISocketDataConverter): add ISocketDataConverter interface (#6420)

* feat: 增加数据转换器设计

* refactor: 数据处理器增加数据转化器适配

* test: 增加单元测试

* refactor: 更新判断转换成功逻辑

* test: 更新单元测试

* feat: 增加 SocketDataConverter 标签

* feat: 增加 Parse 转化器

* test: 增加单元测试

* feat: 增加无符号数据类型转换

* test: 增加单元测试

* refactor: 精简代码

* test: 更新单元测试

* feat: 增加防止死锁逻辑

* test: 增加单元测试

* refactor: 重构代码

* feat: 增加转换器

* feat: 增加 GetConverterByType 扩展方法

* test: 更新单元测试

* test: 增加单元测试
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ISocketDataConverter): add ISocketDataConverter interface

2 participants