From 87b2b6c6c3b8fb98d1e3a184014d8805a490a8ec Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 8 May 2022 18:19:10 +0200 Subject: [PATCH] Fix default valeu for reference types and non-reference types (#34) * . * ok * sw * . * . --- .../Extensions/ParameterSymbolExtensions.cs | 14 ++++---- .../PartialInterfacesGenerator.cs | 3 -- ...ceSourceGeneratorTests.Source.IPerson.g.cs | 10 +++++- ...urceGeneratorTests.Source.PersonProxy.g.cs | 33 +++++++++++++++++-- .../Source/Person.cs | 22 ++++++++++++- 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs b/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs index fdbfc2e..e7a6bee 100644 --- a/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs +++ b/src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs @@ -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(); } \ No newline at end of file diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs index 774efd4..db03085 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs @@ -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(); } diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs index cfdcee5..8fd5b4e 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs @@ -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 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 metadata = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)); diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs index 7498250..80bd704 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs @@ -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 metadata = null, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { int i_ = i; string? appId_ = appId; + System.Collections.Generic.IReadOnlyDictionary metadata_ = metadata; System.Threading.CancellationToken token_ = token; - _Instance.CreateInvokeHttpClient(i_, appId_, token_); + _Instance.CreateInvokeHttpClient(i_, appId_, metadata_, token_); } diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs index 4836eab..9d585cb 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs @@ -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 metadata = null, CancellationToken token = default) { } }