Skip to content

Commit

Permalink
Allow commands with no parameters to compile (#20)
Browse files Browse the repository at this point in the history
Use `Array.Empty<T>()` instead of `new[] { }` or `new IParameter[] { }` when no parameters are specified
  • Loading branch information
FiniteReality authored Jun 12, 2021
1 parent 773bb4a commit 02f7615
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions samples/Console/TestCommands/HelloWorldCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@ public ValueTask<ICommandResult> HelloWorldCommand(

return new ValueTask<ICommandResult>(new NoContentCommandResult());
}

[Command("code")]
public ValueTask<ICommandResult> HelloCodeCommand()
{
_logger.LogInformation("Hello code from HelloModule!");

return new ValueTask<ICommandResult>(new NoContentCommandResult());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ var parameterNamespaces
.Select(
x => $"new CommandFactory__{@class.Name}__{method.Name}__{x.Name}()"));

var parameterArray = parameterTypes == string.Empty
? "Array.Empty<IParameter>()"
: $@"new IParameter[]
{{
{parameterTypes}
}}";

var parameterAccessors = string.Join(", ",
method.Parameters
.Select(
Expand All @@ -97,6 +104,13 @@ var parameterNamespaces
method.Parameters
.Select(x => $"typeof({x.ToDisplayString()})"));

var reflectionArray = reflectionTypes == string.Empty
? "types: Array.Empty<Type>()"
: $@"types: new[]
{{
{reflectionTypes}
}}";

return
$@"{namespaces}
Expand All @@ -118,21 +132,15 @@ private static readonly MethodInfo Method
bindingAttr: {GetBindingFlags(method)},
binder: default,
callConvention: {GetCallConv(method)},
types: new[]
{{
{reflectionTypes}
}},
{reflectionArray},
modifiers: null)!;
private IReadOnlyDictionary<object, object?>? _data;
public CommandString Name {{ get; }} = {commandPath};
public IReadOnlyList<IParameter> Parameters {{ get; }}
= new IParameter[]
{{
{parameterTypes}
}};
= {parameterArray};
public IReadOnlyDictionary<object, object?> Data
{{
Expand Down

0 comments on commit 02f7615

Please sign in to comment.