Skip to content

Fix/context get generator service binding #148

Merged
GeWuYou merged 5 commits into
mainfrom
fix/context-get-generator-service-binding-
Mar 28, 2026
Merged

Fix/context get generator service binding #148
GeWuYou merged 5 commits into
mainfrom
fix/context-get-generator-service-binding-

Conversation

@GeWuYou

@GeWuYou GeWuYou commented Mar 28, 2026

Copy link
Copy Markdown
Owner

Summary by Sourcery

澄清并强制规定:在使用 [GetAll] 时,服务绑定应保持为“显式选择加入”(opt-in),而不是从任意引用类型字段中推断而来。

Bug 修复:

  • 防止在使用 [GetAll] 时,从任意引用类型字段中意外推断出服务和服务集合的绑定。

增强功能:

  • 优化用于上下文绑定的集合元素解析逻辑,改为使用结构化模式匹配。

文档:

  • 更新生成器的 README,说明 [GetAll] 只会推断 Model/System/Utility 绑定,而 Service/Services 绑定必须显式添加注解。

测试:

  • 增加在 [GetAll] 下对显式服务绑定的测试,并调整现有测试以匹配新的“服务绑定不再被推断”的行为。
Original summary in English

Summary by Sourcery

Clarify and enforce that service bindings under [GetAll] remain opt-in rather than inferred from arbitrary reference-type fields.

Bug Fixes:

  • Prevent unintended service and service-collection bindings from being inferred for arbitrary reference-type fields under [GetAll].

Enhancements:

  • Refine collection element resolution logic for context bindings to use structured pattern matching.

Documentation:

  • Update generator README to document that [GetAll] only infers Model/System/Utility bindings and that Service/Services bindings must be explicitly annotated.

Tests:

  • Add tests covering explicit service bindings under [GetAll] and adjust existing tests to match the new non-inferred service binding behavior.

GeWuYou added 2 commits March 28, 2026 19:30
- 移除了对引用类型的自动服务绑定推断,避免将非上下文服务字段误判为服务依赖
- 更新了GetAll特性的行为描述,明确Service和Services绑定不会自动推断
- 添加了显式GetService特性的测试用例验证正确的绑定行为
- 从测试代码中移除了未使用的GetService扩展方法声明
- 为BattlePanel测试类添加了IStrategy服务集合字段以完善测试覆盖
- 重构了EnumerateCollectionTypeCandidates方法的调用方式
- 使用模式匹配简化了类型参数的提取逻辑
- 移除了不必要的SelectMany操作提高代码可读性
- 添加了空值检查增强代码健壮性
- 在方法末尾添加了必要的换行符保持代码格式一致
@sourcery-ai

sourcery-ai Bot commented Mar 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

调整 ContextGet 源生成器,使在使用 [GetAll] 时不再自动推断服务和服务集合绑定;为显式服务绑定添加回归测试,并更新 README,记录服务现在需要显式选择加入的新行为。

Sequence diagram for ContextGetGenerator handling GetAll with explicit GetService

sequenceDiagram
    actor Developer
    participant Compiler
    participant ContextGetGenerator
    participant BattlePanel
    participant ContextAwareServiceExtensions

    Developer->>Compiler: Build project containing BattlePanel
    Compiler->>ContextGetGenerator: Invoke source generator

    ContextGetGenerator->>BattlePanel: Scan attributes on BattlePanel
    BattlePanel-->>ContextGetGenerator: GetAll on class

    ContextGetGenerator->>BattlePanel: Enumerate fields
    BattlePanel-->>ContextGetGenerator: _model: IInventoryModel
    ContextGetGenerator->>ContextGetGenerator: TryCreateInferredBinding for _model
    ContextGetGenerator-->>ContextGetGenerator: Recognize Model binding

    BattlePanel-->>ContextGetGenerator: _service: IStrategy with GetService
    ContextGetGenerator->>ContextGetGenerator: Skip inferred Service binding (opt in only)
    ContextGetGenerator->>ContextGetGenerator: Create explicit Service binding from GetServiceAttribute

    ContextGetGenerator-->>Compiler: Emit __InjectContextBindings_Generated
    Compiler-->>BattlePanel: Merge partial type with generated method

    Developer->>BattlePanel: Instantiate BattlePanel at runtime
    BattlePanel->>BattlePanel: __InjectContextBindings_Generated()
    BattlePanel->>ContextAwareServiceExtensions: GetModel~IInventoryModel~(this)
    BattlePanel->>ContextAwareServiceExtensions: GetService~IStrategy~(this)
    ContextAwareServiceExtensions-->>BattlePanel: Resolved IInventoryModel and IStrategy
Loading

Class diagram for ContextGetGenerator service binding behavior

classDiagram
    direction LR

    class ContextGetGenerator {
        +TryCreateInferredBinding(fieldSymbol, binding)
        +TryResolveCollectionElement(targetType, elementType)
        +EnumerateCollectionTypeCandidates(typeSymbol)
    }

    class BindingInfo {
        +fieldSymbol
        +kind
        +serviceType
    }

    class BindingKind {
        <<enumeration>>
        Model
        Models
        System
        Systems
        Utility
        Utilities
        Service
        Services
    }

    class GetAllAttribute {
        <<attribute>>
        +usage: Class
    }

    class GetServiceAttribute {
        <<attribute>>
        +usage: Field
    }

    class ContextAwareBase {
    }

    class IContextAware {
        <<interface>>
    }

    class ContextAwareServiceExtensions {
        +GetModel~T~(contextAware)
        +GetModels~T~(contextAware)
        +GetSystem~T~(contextAware)
        +GetUtility~T~(contextAware)
        +GetService~T~(contextAware)
    }

    class BattlePanel {
        <<partial>>
        -_model: IInventoryModel
        -_service: IStrategy
        -_services: IReadOnlyList~IStrategy~
        +__InjectContextBindings_Generated()
    }

    class IInventoryModel {
        <<interface>>
    }

    class IStrategy {
        <<interface>>
    }

    class IModel {
        <<interface>>
    }

    class ISystem {
        <<interface>>
    }

    class IUtility {
        <<interface>>
    }

    IContextAware <|.. ContextAwareBase
    ContextAwareBase <|-- BattlePanel

    BindingKind <.. BindingInfo
    BindingInfo <.. ContextGetGenerator

    GetAllAttribute <.. BattlePanel
    GetServiceAttribute <.. BattlePanel

    IInventoryModel <|.. IModel
    IModel <.. IInventoryModel

    ISystem <.. ISystem
    IUtility <.. IUtility

    ContextAwareServiceExtensions ..> BattlePanel : extension_methods
    ContextGetGenerator ..> ContextAwareServiceExtensions : generates_calls
Loading

Flow diagram for inferred bindings under GetAll without service inference

flowchart TD
    A[Start: Field under GetAll] --> B{Is field a collection type?}

    B -- Yes --> C[Call TryResolveCollectionElement]
    C --> D{Element type is Model/System/Utility?}
    D -- Yes --> E[Create BindingInfo with kind Models/Systems/Utilities]
    D -- No --> F[No inferred binding]

    B -- No --> G{Field type is Model/System/Utility?}
    G -- Yes --> H[Create BindingInfo with kind Model/System/Utility]
    G -- No --> I[No inferred binding]

    E --> J[Return true]
    H --> J
    F --> K[Service collections stay opt in]
    I --> L[Service bindings stay opt in]
    K --> M[Require GetService or GetServices attribute]
    L --> M
    M --> N[End]
Loading

File-Level Changes

Change Details Files
将 [GetAll] 上下文绑定推断中的服务以及服务集合绑定改为仅支持显式选择加入。
  • TryCreateInferredBinding 中移除对引用类型集合元素类型的服务集合绑定的自动推断,改为返回 false
  • TryCreateInferredBinding 中移除对任意引用类型字段的单个服务绑定的自动推断,改为返回 false
  • 通过注释澄清设计意图,解释由于任意引用类型存在歧义,因此 Service 和 Services 绑定仍保持为显式选择加入。
  • TryResolveCollectionElement 中重构集合候选类型处理逻辑,使用对 TypeArguments 的列表模式匹配以更简洁地提取元素类型。
GFramework.SourceGenerators/Rule/ContextGetGenerator.cs
添加测试,确保显式的 [GetService] 绑定能被正确生成,并且服务集合不会被隐式推断。
  • 从现有测试夹具中移除未使用的 GetService<T> 扩展方法,以符合新的推断规则。
  • 在 BattlePanel 测试夹具中新增一个服务集合字段,用于覆盖「不会推断服务集合」的场景。
  • 新增测试 Generates_Explicit_Service_Binding_For_GetAll_Class,验证在 [GetAll] 下,对显式添加特性字段生成的 __InjectContextBindings_Generated 会调用 GetModelGetService
GFramework.SourceGenerators.Tests/Rule/ContextGetGeneratorTests.cs
在 README 中记录关于 [GetAll] 的新服务/服务集合显式选择加入行为。
  • 澄清 [GetAll] 只会自动推断 Model、System 和 Utility 的 GetX 调用。
  • 新增说明:在 [GetAll] 下不会自动推断 Service 和 Services 绑定,必须在字段上使用 [GetService] 或 [GetServices],以避免将任意引用类型字段误分类为服务。
GFramework.SourceGenerators/README.md

Tips and commands

Interacting with Sourcery

  • 触发新一次代码评审: 在 pull request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的评审评论。
  • 从评审评论生成 GitHub issue: 在某条评审评论下回复,要求 Sourcery 基于该评论创建 issue。你也可以在评审评论下回复 @sourcery-ai issue 来从该评论创建一个 issue。
  • 生成 pull request 标题: 在 pull request 标题中任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在对应位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来在任意时间(重新)生成摘要。
  • 生成 Reviewer's Guide: 在 pull request 上评论 @sourcery-ai guide,即可在任意时间(重新)生成 Reviewer's Guide。
  • 一次性解决所有 Sourcery 评论: 在 pull request 上评论 @sourcery-ai resolve 可以将所有 Sourcery 评论标记为已解决。如果你已经处理了所有评论且不想再看到它们,这会很有用。
  • 撤销所有 Sourcery 评审: 在 pull request 上评论 @sourcery-ai dismiss 可以撤销所有现有的 Sourcery 评审。尤其适用于你希望从一次全新的评审开始的情况——别忘了随后评论 @sourcery-ai review 以触发新的评审!

Customizing Your Experience

访问你的 dashboard 以:

  • 启用或禁用诸如 Sourcery 生成的 pull request 摘要、Reviewer's Guide 等评审功能。
  • 更改评审语言。
  • 添加、移除或编辑自定义评审指令。
  • 调整其他评审设置。

Getting Help

Original review guide in English

Reviewer's Guide

Adjusts the ContextGet source generator so that service and service-collection bindings are no longer inferred automatically under [GetAll], adds regression tests for explicit service bindings, and updates the README to document the new opt-in behavior for services.

Sequence diagram for ContextGetGenerator handling GetAll with explicit GetService

sequenceDiagram
    actor Developer
    participant Compiler
    participant ContextGetGenerator
    participant BattlePanel
    participant ContextAwareServiceExtensions

    Developer->>Compiler: Build project containing BattlePanel
    Compiler->>ContextGetGenerator: Invoke source generator

    ContextGetGenerator->>BattlePanel: Scan attributes on BattlePanel
    BattlePanel-->>ContextGetGenerator: GetAll on class

    ContextGetGenerator->>BattlePanel: Enumerate fields
    BattlePanel-->>ContextGetGenerator: _model: IInventoryModel
    ContextGetGenerator->>ContextGetGenerator: TryCreateInferredBinding for _model
    ContextGetGenerator-->>ContextGetGenerator: Recognize Model binding

    BattlePanel-->>ContextGetGenerator: _service: IStrategy with GetService
    ContextGetGenerator->>ContextGetGenerator: Skip inferred Service binding (opt in only)
    ContextGetGenerator->>ContextGetGenerator: Create explicit Service binding from GetServiceAttribute

    ContextGetGenerator-->>Compiler: Emit __InjectContextBindings_Generated
    Compiler-->>BattlePanel: Merge partial type with generated method

    Developer->>BattlePanel: Instantiate BattlePanel at runtime
    BattlePanel->>BattlePanel: __InjectContextBindings_Generated()
    BattlePanel->>ContextAwareServiceExtensions: GetModel~IInventoryModel~(this)
    BattlePanel->>ContextAwareServiceExtensions: GetService~IStrategy~(this)
    ContextAwareServiceExtensions-->>BattlePanel: Resolved IInventoryModel and IStrategy
Loading

Class diagram for ContextGetGenerator service binding behavior

classDiagram
    direction LR

    class ContextGetGenerator {
        +TryCreateInferredBinding(fieldSymbol, binding)
        +TryResolveCollectionElement(targetType, elementType)
        +EnumerateCollectionTypeCandidates(typeSymbol)
    }

    class BindingInfo {
        +fieldSymbol
        +kind
        +serviceType
    }

    class BindingKind {
        <<enumeration>>
        Model
        Models
        System
        Systems
        Utility
        Utilities
        Service
        Services
    }

    class GetAllAttribute {
        <<attribute>>
        +usage: Class
    }

    class GetServiceAttribute {
        <<attribute>>
        +usage: Field
    }

    class ContextAwareBase {
    }

    class IContextAware {
        <<interface>>
    }

    class ContextAwareServiceExtensions {
        +GetModel~T~(contextAware)
        +GetModels~T~(contextAware)
        +GetSystem~T~(contextAware)
        +GetUtility~T~(contextAware)
        +GetService~T~(contextAware)
    }

    class BattlePanel {
        <<partial>>
        -_model: IInventoryModel
        -_service: IStrategy
        -_services: IReadOnlyList~IStrategy~
        +__InjectContextBindings_Generated()
    }

    class IInventoryModel {
        <<interface>>
    }

    class IStrategy {
        <<interface>>
    }

    class IModel {
        <<interface>>
    }

    class ISystem {
        <<interface>>
    }

    class IUtility {
        <<interface>>
    }

    IContextAware <|.. ContextAwareBase
    ContextAwareBase <|-- BattlePanel

    BindingKind <.. BindingInfo
    BindingInfo <.. ContextGetGenerator

    GetAllAttribute <.. BattlePanel
    GetServiceAttribute <.. BattlePanel

    IInventoryModel <|.. IModel
    IModel <.. IInventoryModel

    ISystem <.. ISystem
    IUtility <.. IUtility

    ContextAwareServiceExtensions ..> BattlePanel : extension_methods
    ContextGetGenerator ..> ContextAwareServiceExtensions : generates_calls
Loading

Flow diagram for inferred bindings under GetAll without service inference

flowchart TD
    A[Start: Field under GetAll] --> B{Is field a collection type?}

    B -- Yes --> C[Call TryResolveCollectionElement]
    C --> D{Element type is Model/System/Utility?}
    D -- Yes --> E[Create BindingInfo with kind Models/Systems/Utilities]
    D -- No --> F[No inferred binding]

    B -- No --> G{Field type is Model/System/Utility?}
    G -- Yes --> H[Create BindingInfo with kind Model/System/Utility]
    G -- No --> I[No inferred binding]

    E --> J[Return true]
    H --> J
    F --> K[Service collections stay opt in]
    I --> L[Service bindings stay opt in]
    K --> M[Require GetService or GetServices attribute]
    L --> M
    M --> N[End]
Loading

File-Level Changes

Change Details Files
Make service and service-collection bindings opt-in only for [GetAll] context binding inference.
  • Remove automatic inference of service-collection bindings for reference-type collection element types in TryCreateInferredBinding, returning false instead.
  • Remove automatic inference of single service bindings for arbitrary reference-type fields in TryCreateInferredBinding, returning false instead.
  • Clarify intent with comments explaining why service and services bindings remain opt-in due to ambiguity of arbitrary reference types.
  • Refactor collection candidate handling in TryResolveCollectionElement to use list-pattern matching on TypeArguments for cleaner element-type extraction.
GFramework.SourceGenerators/Rule/ContextGetGenerator.cs
Add tests ensuring explicit [GetService] bindings are generated correctly and that service collections are not inferred implicitly.
  • Remove the unused GetService extension from an existing test fixture to match the new inference rules.
  • Add a field for a collection of services to the BattlePanel test fixture to cover non-inferred service collections.
  • Add a new test Generates_Explicit_Service_Binding_For_GetAll_Class that verifies __InjectContextBindings_Generated uses GetModel and GetService for explicitly attributed fields under [GetAll].
GFramework.SourceGenerators.Tests/Rule/ContextGetGeneratorTests.cs
Document the new service/service-collection opt-in behavior for [GetAll] in the README.
  • Clarify that [GetAll] only auto-infers Model, System, and Utility GetX calls.
  • Add guidance that Service and Services bindings are not auto-inferred under [GetAll] and must use [GetService] or [GetServices] on fields to avoid misclassifying arbitrary reference-type fields as services.
GFramework.SourceGenerators/README.md

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

@deepsource-io

deepsource-io Bot commented Mar 28, 2026

Copy link
Copy Markdown

DeepSource Code Review

We reviewed changes in e692c72...2f4ba63 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
C# Mar 28, 2026 11:56a.m. Review ↗
Secrets Mar 28, 2026 11:56a.m. Review ↗

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

嘿,我已经审查了你的修改,看起来很棒!


Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈来改进后续的审查。
Original comment in English

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


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.

GeWuYou added 3 commits March 28, 2026 19:40
- 将嵌套循环重构为使用 SelectMany 扁平化集合类型参数
- 简化了候选元素类型的遍历逻辑
- 提高了代码可读性和性能
- 移除了不必要的 continue 语句
- 保持了原有的类型匹配功能不变
- 在测试中添加换行符标准化功能,确保跨平台测试一致性
- 修复ContextGetGenerator中集合类型候选检测逻辑,跳过无效类型参数
- 添加针对可空服务字段的单元测试用例
- 优化生成器对不同系统换行符的处理机制
- 在 GFramework.Godot.SourceGenerators.Tests 中添加 Microsoft.CodeAnalysis 相关引用
- 在 GFramework.SourceGenerators 中添加 ImmutableCollections 和 StringBuilder 引用
- 在 GFramework.SourceGenerators 中添加 Common.Extensions 引用
- 在 GFramework.SourceGenerators.Tests 中添加测试框架相关引用
- 统一全局引用的组织结构
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant