Fix default valeu for reference types and non-reference types (#34)

* .

* ok

* sw

* .

* .
This commit is contained in:
Stef Heyenrath
2022-05-08 18:19:10 +02:00
committed by GitHub
parent b4c490aef9
commit 87b2b6c6c3
5 changed files with 67 additions and 15 deletions
@@ -1,11 +1,12 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ProxyInterfaceSourceGenerator.Enums;
namespace ProxyInterfaceSourceGenerator.Extensions;
internal static class ParameterSymbolExtensions
{
private const string ParameterValueDefault = "default";
// private const string ParameterValueDefault = "default";
private const string ParameterValueNull = "null";
public static string GetRefPrefix(this IParameterSymbol ps)
@@ -37,20 +38,17 @@ internal static class ParameterSymbolExtensions
}
string defaultValue;
if (ps.ExplicitDefaultValue is null)
if (ps.ExplicitDefaultValue == null)
{
defaultValue = ps.NullableAnnotation == NullableAnnotation.Annotated
? ParameterValueNull
: ParameterValueDefault;
defaultValue = ps.Type.IsReferenceType ? ParameterValueNull : $"default({ps.Type})";
}
else
{
defaultValue = ps.ExplicitDefaultValue.ToString();
defaultValue = SymbolDisplay.FormatPrimitive(ps.ExplicitDefaultValue, true, false);
}
return $" = {defaultValue}";
}
public static TypeEnum GetTypeEnum(this IParameterSymbol p) =>
p.Type.GetTypeEnum();
public static TypeEnum GetTypeEnum(this IParameterSymbol p) => p.Type.GetTypeEnum();
}
@@ -115,9 +115,6 @@ namespace {ns}
var whereStatement = GetWhereStatementFromMethod(method);
//public static string GetWhereStatement(this IMethodSymbol method) =>
// !method.IsGenericMethod ? string.Empty : string.Join("", method.TypeParameters.Select(tp => tp.GetWhereConstraints()));
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){whereStatement};");
str.AppendLine();
}
@@ -30,6 +30,14 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
string HelloWorld(string name);
string HelloWorld2(string? name = "x");
string HelloWorld3(char? ch = 'c');
string HelloWorld4(char ch);
string HelloWorld5(char? ch);
void WithParams(params string[] values);
string Add(string s, string @string);
@@ -46,7 +54,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
System.Threading.Tasks.Task<string?> Method3Async();
void CreateInvokeHttpClient(int i = 5, string? appId = null, System.Threading.CancellationToken token = default);
void CreateInvokeHttpClient(int i = 5, string? appId = null, System.Collections.Generic.IReadOnlyDictionary<string, string> metadata = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken));
@@ -48,6 +48,34 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
return result_282270798;
}
public string HelloWorld2(string? name = "x")
{
string? name_ = name;
var result__1104421408 = _Instance.HelloWorld2(name_);
return result__1104421408;
}
public string HelloWorld3(char? ch = 'c')
{
char? ch_ = ch;
var result__1104421409 = _Instance.HelloWorld3(ch_);
return result__1104421409;
}
public string HelloWorld4(char ch)
{
char ch_ = ch;
var result__1104421414 = _Instance.HelloWorld4(ch_);
return result__1104421414;
}
public string HelloWorld5(char? ch)
{
char? ch_ = ch;
var result__1104421415 = _Instance.HelloWorld5(ch_);
return result__1104421415;
}
public void WithParams(params string[] values)
{
string[] values_ = values;
@@ -105,12 +133,13 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
return result__57684656;
}
public void CreateInvokeHttpClient(int i = 5, string? appId = null, System.Threading.CancellationToken token = default)
public void CreateInvokeHttpClient(int i = 5, string? appId = null, System.Collections.Generic.IReadOnlyDictionary<string, string> metadata = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken))
{
int i_ = i;
string? appId_ = appId;
System.Collections.Generic.IReadOnlyDictionary<string, string> metadata_ = metadata;
System.Threading.CancellationToken token_ = token;
_Instance.CreateInvokeHttpClient(i_, appId_, token_);
_Instance.CreateInvokeHttpClient(i_, appId_, metadata_, token_);
}
@@ -28,6 +28,26 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
{
return $"Hello {name} !";
}
public string HelloWorld2(string? name = "x")
{
return $"Hello {name} !";
}
public string HelloWorld3(char? ch = 'c')
{
return $"Hello {ch} !";
}
public string HelloWorld4(char ch)
{
return $"Hello {ch} !";
}
public string HelloWorld5(char? ch)
{
return $"Hello {ch} !";
}
public void WithParams(params string[] values)
{
@@ -70,7 +90,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
return Task.FromResult((string?)"");
}
public void CreateInvokeHttpClient(int i = 5, string? appId = null, CancellationToken token = default)
public void CreateInvokeHttpClient(int i = 5, string? appId = null, IReadOnlyDictionary<string, string> metadata = null, CancellationToken token = default)
{
}
}