Skip to content

Commit

Permalink
Fix options NRE
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-choi committed Feb 29, 2024
1 parent dfe98ba commit 79e97f8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public static class ArgumentArityEnumTestFactory
{
Arity = global::System.CommandLine.ArgumentArity.OneOrMore
};
var handlerAdapter = new ArgumentArityEnumTestCommandHandlerAdapter(options.Handler, symbol, symbol1, symbol2, symbol3, symbol4);
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.ArgumentArityEnumTest> handler = null;
if (options != null)
handler = options.Handler;
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new ArgumentArityEnumTestCommandHandlerAdapter(handler, symbol, symbol1, symbol2, symbol3, symbol4);
var cmd = new global::System.CommandLine.RootCommand("")
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public static class CommandHandlerTestFactory
{
Arity = new global::System.CommandLine.ArgumentArity(2, 4)
};
var handler = options.Handler;
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest> handler = null;
if (options != null)
handler = options.Handler;
if (handler == null)
handler = new global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest_CommandHandler();
var handlerAdapter = new CommandHandlerTestCommandHandlerAdapter(handler, symbol, symbol1, symbol2, symbol3, symbol4, symbol5);
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new CommandHandlerTestCommandHandlerAdapter(handler, symbol, symbol1, symbol2, symbol3, symbol4, symbol5);
var cmd = new global::System.CommandLine.RootCommand("")
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public static class SubFactory

public static global::System.CommandLine.Command Create(SubOptions options)
{
var handler = options.Handler;
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest.Sub> handler = null;
if (options != null)
handler = options.Handler;
if (handler == null)
handler = new global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest_Sub_CommandHandler();
var handlerAdapter = new SubCommandHandlerAdapter(handler);
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new SubCommandHandlerAdapter(handler);
var cmd = new global::System.CommandLine.Command("sub", null)
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public static class Sub2Factory
{
var symbol = new global::System.CommandLine.Option<global::System.String>("-A", null);
var symbol1 = new global::System.CommandLine.Option<global::System.String>("-B", null);
var handler = options.Handler;
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest.Sub.Sub2> handler = null;
if (options != null)
handler = options.Handler;
if (handler == null)
handler = new global::System.CommandLine.SourceGenerator.Tests.CommandHandlerTest_Sub_Sub2_CommandHandler();
var handlerAdapter = new Sub2CommandHandlerAdapter(handler, symbol, symbol1);
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new Sub2CommandHandlerAdapter(handler, symbol, symbol1);
var cmd = new global::System.CommandLine.Command("sub2", null)
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public static class DerviedTestFactory
var symbol = new global::System.CommandLine.Option<global::System.Int32>("--virtual-has-option-override", null);
var symbol1 = new global::System.CommandLine.Option<global::System.Int32>("--virtual-no-option", null);
var symbol2 = new global::System.CommandLine.Option<global::System.Int32>("--option", null);
var handlerAdapter = new DerviedTestCommandHandlerAdapter(options.Handler, symbol, symbol1, symbol2);
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.DerviedTest> handler = null;
if (options != null)
handler = options.Handler;
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new DerviedTestCommandHandlerAdapter(handler, symbol, symbol1, symbol2);
var cmd = new global::System.CommandLine.RootCommand("")
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public static class StructTestFactory
public static global::System.CommandLine.RootCommand Create(StructTestOptions options)
{
var symbol = new global::System.CommandLine.Option<global::System.String>("--hello", null);
var handler = options.Handler;
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.StructTest> handler = null;
if (options != null)
handler = options.Handler;
if (handler == null)
handler = new global::System.CommandLine.SourceGenerator.Tests.StructTestHandler();
var handlerAdapter = new StructTestCommandHandlerAdapter(handler, symbol);
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new StructTestCommandHandlerAdapter(handler, symbol);
var cmd = new global::System.CommandLine.RootCommand("")
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public static class SubcommandsTestFactory
public static global::System.CommandLine.RootCommand Create(SubcommandsTestOptions options)
{
var symbol = new global::System.CommandLine.Option<global::System.Int32>("--option1", null);
var handlerAdapter = new SubcommandsTestCommandHandlerAdapter(options.Handler, symbol);
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.SubcommandsTest> handler = null;
if (options != null)
handler = options.Handler;
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new SubcommandsTestCommandHandlerAdapter(handler, symbol);
var cmd = new global::System.CommandLine.RootCommand("")
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public static class SubcommandsTest_AFactory
public static global::System.CommandLine.Command Create(SubcommandsTest_AOptions options)
{
var symbol = new global::System.CommandLine.Option<global::System.Int32>("--option2", null);
var handlerAdapter = new SubcommandsTest_ACommandHandlerAdapter(options.Handler, symbol);
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.SubcommandsTest_A> handler = null;
if (options != null)
handler = options.Handler;
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new SubcommandsTest_ACommandHandlerAdapter(handler, symbol);
var cmd = new global::System.CommandLine.Command("A", null)
{
Handler = handlerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public static class SubcommandsTest_BFactory
public static global::System.CommandLine.Command Create(SubcommandsTest_BOptions options)
{
var symbol = new global::System.CommandLine.Option<global::System.Int32>("--option3", null);
var handlerAdapter = new SubcommandsTest_BCommandHandlerAdapter(options.Handler, symbol);
ICommandHandler<global::System.CommandLine.SourceGenerator.Tests.SubcommandsTest_B> handler = null;
if (options != null)
handler = options.Handler;
global::System.CommandLine.Invocation.ICommandHandler handlerAdapter = null;
if (handler != null)
handlerAdapter = new SubcommandsTest_BCommandHandlerAdapter(handler, symbol);
var cmd = new global::System.CommandLine.Command("B", null)
{
Handler = handlerAdapter
Expand Down
43 changes: 29 additions & 14 deletions src/System.CommandLine.SourceGenerator/CommandLineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void GenerateOptionsClassDefinition(CommandDeclaration command, S
source.WriteLine($"{accessibility}class {command.OptionsType.Name}", true);
source.OpenBrace();

source.WriteLine($"public ICommandHandler<{command.TypeSymbol.ToFullyQualifiedDisplayString(true)}> Handler {{ get; set; }}", true);
source.WriteLine($"public {command.CommandHandlerTypeName} Handler {{ get; set; }}", true);

foreach (var subCommand in command.CommandDeclarations)
{
Expand Down Expand Up @@ -171,29 +171,44 @@ private static string GenerateCommandCreationCode(
.Select(x => GenerateSymbolCreationCode(source, methodContext, x))
.ToArray();

var handlerAdapterVariableName = methodContext.Declare("handlerAdapter", null);
// ICommandHandler<..>
var handlerVariableName = methodContext.Declare("handler", null);

source.WriteBlock(
$"""
{command.CommandHandlerTypeName} {handlerVariableName} = null;
if (options != null)
{handlerVariableName} = options.Handler;
""",
true);

source.WriteLine();

if (command.HandlerTypeSymbol is not null)
{
var handlerVariableName = methodContext.Declare("handler", null);
var handlerAdapterArgs = string.Join(", ", symbolVariableNames.Prepend(handlerVariableName));

source.WriteBlock(
$"""
var {handlerVariableName} = options.Handler;
if ({handlerVariableName} == null)
{handlerVariableName} = new {command.HandlerTypeSymbol.ToFullyQualifiedDisplayString(true)}();
var {handlerAdapterVariableName} = new {command.CommandHandlerAdapterType.Name}({handlerAdapterArgs});
""",
true);

source.WriteLine();
}
else
{
var handlerAdapterArgs = string.Join(", ", symbolVariableNames.Prepend("options.Handler"));
source.WriteLine($"var {handlerAdapterVariableName} = new {command.CommandHandlerAdapterType.Name}({handlerAdapterArgs});", true);
}

// ..CommandHandlerAdapter
var handlerAdapterVariableName = methodContext.Declare("handlerAdapter", null);
var handlerAdapterArgs = string.Join(", ", symbolVariableNames.Prepend(handlerVariableName));

source.WriteBlock(
$"""
{Types.ICommandHandler} {handlerAdapterVariableName} = null;
if ({handlerVariableName} != null)
{handlerAdapterVariableName} = new {command.CommandHandlerAdapterType.Name}({handlerAdapterArgs});
""",
true);

source.WriteLine();

// var cmd = new Command(..);
var commandVariableName = methodContext.Declare("cmd", command);
Expand Down Expand Up @@ -363,7 +378,7 @@ private static void GenerateCommandHandlerAdapterClassDefinition(SourceTextBuild
source.OpenBrace();

// private readonly ICommandHandler<{Command}> _commandHandler;
source.WriteLine($"private readonly ICommandHandler<{command.TypeSymbol.ToFullyQualifiedDisplayString(true)}> _commandHandler;", true);
source.WriteLine($"private readonly {command.CommandHandlerTypeName} _commandHandler;", true);

// private readonly IValueDescriptor<..> _v0;
// private readonly IValueDescriptor<..> _v1;
Expand Down Expand Up @@ -397,7 +412,7 @@ public ..CommandHandler(
*/
var ctorArguments = symbolFields
.Select(symbolField => $"{Types.IValueDescriptor}<{symbolField.Type}> {symbolField.ArgumentName}")
.Prepend($"ICommandHandler<{command.TypeSymbol.ToFullyQualifiedDisplayString(true)}> commandHandler")
.Prepend($"{command.CommandHandlerTypeName} commandHandler")
.ToArray();

if (ctorArguments.Length == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal class CommandDeclaration

public GeneratedTypeInfo CommandHandlerAdapterType { get; }

public string CommandHandlerTypeName => $"ICommandHandler<{TypeSymbol.ToFullyQualifiedDisplayString(true)}>";

public CommandDeclaration(
INamedTypeSymbol typeSymbol,
IAttributeDeclaration<CommandAttribute> attribute,
Expand Down

0 comments on commit 79e97f8

Please sign in to comment.