Skip to content

Refactor fluent generator API to explicit Method().WithParameter().WithReturnType() chain#18

Merged
dex3r merged 3 commits into
mainfrom
copilot/refactor-fluent-api-implementation
Feb 28, 2026
Merged

Refactor fluent generator API to explicit Method().WithParameter().WithReturnType() chain#18
dex3r merged 3 commits into
mainfrom
copilot/refactor-fluent-api-implementation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

This PR replaces the ambiguous Generate.MethodImplementation<...>() entrypoint with an explicit fluent chain that makes parameter vs return type intent clear. It also updates all in-repo fluent examples/tests and removes the old API surface (no backward-compatibility shim, per issue scope).

  • Fluent API surface refactor (Abstractions)

    • Removed Generate.MethodImplementation(), Generate.MethodImplementation<TReturnType>(), and Generate.MethodImplementation<TArg1, TReturnType>().
    • Added new builder-based entrypoint:
      • Generate.Method()
      • WithParameter<TArg1>()
      • WithReturnType<TReturnType>()
    • Introduced minimal builder contracts/impls to route to existing IGeneratorsFactory.CreateImplementation(...) methods without changing generator internals.
  • Usage migration in examples

    • Updated PiExampleFluent to:
      • Generate.Method().WithParameter<int>().WithReturnType<int>()
    • Updated ColorsClassFluent to:
      • Generate.Method().WithReturnType<string>()
  • Usage migration + coverage in tests

    • Updated existing fluent test generator usage to the new chain.
    • Added mapper-like fluent coverage demonstrating non-identical parameter/return intent (int -> string) and validating generated switch output shape.
[GeneratesMethod(nameof(MapToMammal))]
static IMethodImplementationGenerator MapToMammal_Generator() =>
    Generate
        .Method().WithParameter<int>().WithReturnType<string>()
        .WithSwitchBody()
        .ForCases(1).ReturnConstantValue(_ => "Dog")
        .ForCases(2).ReturnConstantValue(_ => "Cat")
        .ForDefaultCase().WithBody(_ => () => "Unknown");
Original prompt

This section details on the original issue you should resolve

<issue_title>Fluent API Refactor</issue_title>
<issue_description>In PiExampleFluent the fluent code starts like this:

        Generate
            .MethodImplementation<int, int>()

However, this is not intuitive. You can't tell which generic type argument is the return type and which is the argument. Thats why, in the new Mapper class there is a new flue API example:

 Generate
            .Method().WithParameter<FourLeggedAnimal>().WithReturnType<MammalAnimal>()

Implement it. Replace the old way with the new way (both here, other examples, and in tests).
Remove the old way, the package was not released yet so no need to ensure backwards compatiblity nor Deprecated attribute. Just remove the old way and relpace all usages with the new way.

Also, ensure the new example works, create something similar for tests.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits February 28, 2026 20:39
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor fluent API for clarity and usability Refactor fluent generator API to explicit Method().WithParameter().WithReturnType() chain Feb 28, 2026
@dex3r dex3r marked this pull request as ready for review February 28, 2026 21:04
Copilot AI review requested due to automatic review settings February 28, 2026 21:04
@dex3r dex3r merged commit 511fe30 into main Feb 28, 2026
1 check passed
@dex3r dex3r deleted the copilot/refactor-fluent-api-implementation branch February 28, 2026 21:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the fluent generator entrypoint from ambiguous Generate.MethodImplementation<...>() overloads to an explicit builder chain (Generate.Method().WithParameter<T>().WithReturnType<T>()) to make parameter vs return type intent clear, and migrates in-repo examples/tests accordingly.

Changes:

  • Replaced Generate.MethodImplementation* overloads with a builder-based fluent entrypoint (Generate.Method() + WithParameter/WithReturnType).
  • Migrated fluent usage in examples to the new chain.
  • Updated tests to the new chain and added mapper-like coverage (int -> string) validating both runtime output and generated code shape.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
MattSourceGenHelpers.Abstractions/Generate.cs Removes old entrypoints and adds the new method builder chain routing to IGeneratorsFactory.CreateImplementation(...).
MattSourceGenHelpers.Examples/PiExampleFluent.cs Migrates Pi fluent example to Method().WithParameter<int>().WithReturnType<int>().
MattSourceGenHelpers.Examples/ColorsClassFluent.cs Migrates Colors fluent example to Method().WithReturnType<string>().
MattSourceGenHelpers.Tests/PiExampleFluentTests.cs Updates fluent usage to the new chain and adds mapper-like tests for int -> string generation/output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +21 to 45
public class MethodBuilder : IMethodBuilder
{
private readonly IGeneratorsFactory _generatorsFactory;

public MethodBuilder(IGeneratorsFactory generatorsFactory)
{
_generatorsFactory = generatorsFactory;
}

public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(_generatorsFactory);

public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TReturnType>();
}

public class MethodBuilder<TArg1> : IMethodBuilder<TArg1>
{
private readonly IGeneratorsFactory _generatorsFactory;

public MethodBuilder(IGeneratorsFactory generatorsFactory)
{
_generatorsFactory = generatorsFactory;
}

public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TArg1, TReturnType>();
}
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

MethodBuilder / MethodBuilder<TArg1> are only constructed internally via Generate.Method() and are not referenced elsewhere in the repo. Consider making these concrete builder types internal (and potentially sealed) to avoid expanding the public API surface unnecessarily; consumers can still program against the public IMethodBuilder interfaces returned by Generate.Method().

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +14
public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator);
}

public interface IMethodBuilder
{
IMethodBuilder<TArg1> WithParameter<TArg1>();
IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>();
}
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The Generate.Method().WithReturnType<TReturnType>() (no-parameter) path is part of the new fluent entrypoint, but current tests only exercise the .WithParameter<TArg1>().WithReturnType<TReturnType>() chain. Adding at least one test that uses the parameterless chain (similar to ColorsClassFluent) would help catch regressions in this new API surface.

Copilot uses AI. Check for mistakes.
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.

Fluent API Refactor

3 participants