-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor #42
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
Refactor #42
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MattSourceGenHelpers.Tests")] | ||
| [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MattSourceGenHelpers.Generators")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,78 +1,10 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
| using JetBrains.Annotations; | ||
|
|
||
| public static class Generate | ||
| { | ||
| public static IGeneratorsFactory CurrentGenerator { get; set; } = new RecordingGeneratorsFactory(); | ||
|
|
||
| public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator); | ||
| } | ||
|
|
||
| public interface IMethodBuilder | ||
| { | ||
| IMethodBuilder<TArg1> WithParameter<TArg1>(); | ||
| IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>(); | ||
| } | ||
|
|
||
| public interface IMethodBuilder<TArg1> | ||
| { | ||
| IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>(); | ||
| } | ||
|
|
||
| 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>(); | ||
| } | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| 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>(); | ||
| } | ||
|
|
||
| public class EmptyGeneratorsFactory : IGeneratorsFactory | ||
| { | ||
| public IMethodImplementationGenerator CreateImplementation() => new EmptyMethodImplementationGenerator(); | ||
| public IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>() => new EmptyMethodImplementationGenerator<TReturnType>(); | ||
| public IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>() => new EmptyMethodImplementationGenerator<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class EmptyMethodImplementationGenerator : IMethodImplementationGenerator | ||
| { | ||
| public IMethodImplementationGenerator WithBody(Action body) => this; | ||
| public IMethodImplementationGenerator WithBody(Func<object> body) => this; | ||
| } | ||
|
|
||
| public class EmptyMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType> | ||
| { | ||
| public IMethodImplementationGenerator UseBody(Func<object> body) => this; | ||
| } | ||
|
|
||
| public class EmptyMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator<TArg1, TReturnType> | ||
| public static class Generate | ||
| { | ||
| public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody() => | ||
| new RecordingMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>(new SwitchBodyRecord()); | ||
| public IMethodImplementationGenerator WithBody(Action body) => this; | ||
| public IMethodImplementationGenerator WithBody(Func<object> body) => this; | ||
| } | ||
| internal static IGeneratorsFactory CurrentGenerator { get; [UsedImplicitly] set; } = new MockGeneratorsFactory(); | ||
|
|
||
| public interface IGeneratorsFactory | ||
| { | ||
| IMethodImplementationGenerator CreateImplementation(); | ||
| IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>(); | ||
| IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>(); | ||
| } | ||
| public static IMethodBuilder Method() => CurrentGenerator.ForMethod(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| public interface IGeneratorsFactory | ||
| { | ||
| IMethodBuilder ForMethod(); | ||
|
|
||
| IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>(); | ||
| IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| public interface IMethodBuilder | ||
| { | ||
| IMethodBuilder<TArg1> WithParameter<TArg1>(); | ||
| IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>(); | ||
| } | ||
|
|
||
| public interface IMethodBuilder<TArg1> | ||
| { | ||
| IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>(); | ||
| } |
This file was deleted.
Oops, something went wrong.
14 changes: 5 additions & 9 deletions
14
MattSourceGenHelpers.Abstractions/IMethodImplementationGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,15 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
| using JetBrains.Annotations; | ||
|
|
||
| public interface IMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| public interface IMethodImplementationGenerator<[UsedImplicitly] TReturnType> : IMethodImplementationGenerator | ||
| { | ||
| IMethodImplementationGenerator UseBody(Func<object> body); | ||
| IMethodImplementationGenerator UseBody([UsedImplicitly] Func<object> body); | ||
| } | ||
|
|
||
| public interface IMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator | ||
| { | ||
| IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody(); | ||
| } | ||
|
|
||
| public interface IMethodImplementationGeneratorVoid | ||
| { | ||
| IMethodImplementationGenerator CompileTimeBody(Action func); | ||
| IMethodImplementationGenerator RuntimeBody(Action func); | ||
| } | ||
|
|
||
| public interface IMethodImplementationGenerator; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| public class MethodBuilder(IGeneratorsFactory generatorsFactory) : IMethodBuilder | ||
| { | ||
| public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(generatorsFactory); | ||
|
|
||
| public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => generatorsFactory.CreateImplementation<TReturnType>(); | ||
| } | ||
|
|
||
| public class MethodBuilder<TArg1>(IGeneratorsFactory generatorsFactory) : IMethodBuilder<TArg1> | ||
| { | ||
| public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => generatorsFactory.CreateImplementation<TArg1, TReturnType>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| public class MockGeneratorsFactory : IGeneratorsFactory | ||
| { | ||
| public IMethodBuilder ForMethod() => new MockMethodBuilder(); | ||
|
|
||
| public IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>() => new MockMethodImplementationGenerator<TReturnType>(); | ||
|
|
||
| public IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>() => | ||
| new MockMethodImplementationGenerator<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class MockMethodImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType> | ||
| { | ||
| public IMethodImplementationGenerator UseBody(Func<object> body) => this; | ||
| } | ||
|
|
||
| public class MockMethodImplementationGenerator<TArg1, TReturnType> : IMethodImplementationGenerator<TArg1, TReturnType> | ||
| { | ||
| public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> WithSwitchBody() => | ||
| new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> | ||
| { | ||
| public IMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType> ForCases(params object[] cases) | ||
| => new MockMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType>(); | ||
|
|
||
| public IMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType> ForDefaultCase() | ||
| => new MockMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class MockMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBodyDefaultCase<TArg1, | ||
| TReturnType> | ||
| { | ||
| public IMethodImplementationGenerator<TArg1, TReturnType> ReturnConstantValue(Func<TArg1, TReturnType> func) | ||
| => new MockMethodImplementationGenerator<TArg1, TReturnType>(); | ||
|
|
||
| public IMethodImplementationGenerator<TArg1, TReturnType> UseBody(Func<TArg1, Func<TReturnType>> func) | ||
| => new MockMethodImplementationGenerator<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class MockMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType> : IMethodImplementationGeneratorSwitchBodyCase<TArg1, TReturnType> | ||
| { | ||
| public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> ReturnConstantValue(Func<TArg1, TReturnType> constantValueFactory) | ||
| => new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>(); | ||
|
|
||
| public IMethodImplementationGeneratorSwitchBody<TArg1, TReturnType> UseBody(Func<TArg1, Action<TReturnType>> body) | ||
| => new MockMethodImplementationGeneratorSwitchBody<TArg1, TReturnType>(); | ||
| } | ||
|
|
||
| public class MockMethodBuilder : IMethodBuilder | ||
| { | ||
| public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MockMethodBuilder<TArg1>(); | ||
|
|
||
| public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => new MockImplementationGenerator<TReturnType>(); | ||
| } | ||
|
|
||
| public class MockImplementationGenerator<TReturnType> : IMethodImplementationGenerator<TReturnType> | ||
| { | ||
| public IMethodImplementationGenerator UseBody(Func<object> body) => this; | ||
| } | ||
|
|
||
| public class MockMethodBuilder<TArg1Input> : IMethodBuilder<TArg1Input> | ||
| { | ||
| public IMethodImplementationGenerator<TArg1Input, TReturnType> WithReturnType<TReturnType>() | ||
| => new MockMethodImplementationGenerator<TArg1Input, TReturnType>(); | ||
| } | ||
17 changes: 7 additions & 10 deletions
17
MattSourceGenHelpers.Abstractions/PartialMethodCalledDuringGenerationException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,14 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace MattSourceGenHelpers.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Thrown when a partial method is called during source generation. | ||
| /// Partial methods cannot be invoked inside generator methods because they are the | ||
| /// methods being generated — they do not have an implementation yet at generation time. | ||
| /// </summary> | ||
| public class PartialMethodCalledDuringGenerationException : InvalidOperationException | ||
| { | ||
| public PartialMethodCalledDuringGenerationException(string methodName, string typeName) | ||
| : base( | ||
| $"Partial method '{typeName}.{methodName}' was called during source generation. " + | ||
| $"Partial methods cannot be invoked inside generator methods because their implementations " + | ||
| $"are what is being generated. Remove the call to '{methodName}' from your generator method.") | ||
| { | ||
| } | ||
| } | ||
| [UsedImplicitly(ImplicitUseKindFlags.InstantiatedWithFixedConstructorSignature)] | ||
| public class PartialMethodCalledDuringGenerationException(string methodName, string typeName) : InvalidOperationException( | ||
| $"Partial method '{typeName}.{methodName}' was called during source generation. " + | ||
| $"Partial methods cannot be invoked inside generator methods because their implementations " + | ||
| $"are what is being generated. Remove the call to '{methodName}' from your generator method."); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two classes implementing
IMethodImplementationGenerator<TReturnType>with nearly identical implementations but different names:MockMethodImplementationGenerator<TReturnType>(line 13) andMockImplementationGenerator<TReturnType>(line 59). The second is used byMockMethodBuilder.WithReturnType<TReturnType>()but could simply reuseMockMethodImplementationGenerator<TReturnType>, eliminating the duplication and inconsistent naming.