Fix Default value (#19)
This commit is contained in:
@@ -27,6 +27,11 @@ namespace ProxyInterfaceConsumer
|
||||
|
||||
public IMyInterface MyInterface { get; set; }
|
||||
|
||||
public int DefaultValue(int x = 100)
|
||||
{
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
public string Add(string s, string @string)
|
||||
{
|
||||
return s + @string;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using AutoMapper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using AutoMapper;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
@@ -33,6 +32,9 @@ namespace ProxyInterfaceConsumer
|
||||
|
||||
IPerson p = new PersonProxy(new Person());
|
||||
p.Name = "test";
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue());
|
||||
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
|
||||
|
||||
var ap = new AddressProxy(new Address { HouseNumber = 42 });
|
||||
p.Address = ap;
|
||||
var add = p.AddAddress(ap);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
{
|
||||
internal static class ParameterSymbolExtensions
|
||||
{
|
||||
public static string GetRefPrefix(this IParameterSymbol ps)
|
||||
{
|
||||
switch (ps.RefKind)
|
||||
{
|
||||
case RefKind.In:
|
||||
return "in ";
|
||||
|
||||
case RefKind.Out:
|
||||
return "out ";
|
||||
|
||||
case RefKind.Ref:
|
||||
return "ref ";
|
||||
|
||||
default:
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetParamsPrefix(this IParameterSymbol ps) =>
|
||||
ps.IsParams ? "params " : string.Empty;
|
||||
|
||||
public static string GetDefaultValue(this IParameterSymbol ps) =>
|
||||
ps.HasExplicitDefaultValue ? $" = {ps.ExplicitDefaultValue}" : string.Empty;
|
||||
|
||||
public static TypeEnum GetTypeEnum(this IParameterSymbol p) =>
|
||||
p.Type.GetTypeEnum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
{
|
||||
internal static class PropertySymbolExtensions
|
||||
{
|
||||
public static TypeEnum GetTypeEnum(this IPropertySymbol p) =>
|
||||
p.Type.GetTypeEnum();
|
||||
|
||||
public static string ToPropertyText(this IPropertySymbol property, string? overrideType = null)
|
||||
{
|
||||
var get = property.GetMethod != null ? "get; " : string.Empty;
|
||||
var set = property.SetMethod != null ? "set; " : string.Empty;
|
||||
|
||||
var type = !string.IsNullOrEmpty(overrideType) ? overrideType : $"{property.Type}";
|
||||
|
||||
return $"{type} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _Instance.{property.GetSanitizedName()}; " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.GetSanitizedName()} = value; " : string.Empty;
|
||||
|
||||
return $"{property.Type} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.GetSanitizedName()}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.GetSanitizedName()} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
|
||||
return $"{overrideType} {property.GetSanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,98 +1,14 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
{
|
||||
internal static class SymbolExtensions
|
||||
{
|
||||
public static bool IsKeywordOrReserved(this ISymbol symbol)
|
||||
{
|
||||
return SyntaxFacts.GetKeywordKind(symbol.Name) != SyntaxKind.None || SyntaxFacts.GetContextualKeywordKind(symbol.Name) != SyntaxKind.None;
|
||||
}
|
||||
public static bool IsKeywordOrReserved(this ISymbol symbol) =>
|
||||
SyntaxFacts.GetKeywordKind(symbol.Name) != SyntaxKind.None || SyntaxFacts.GetContextualKeywordKind(symbol.Name) != SyntaxKind.None;
|
||||
|
||||
public static string SanitizedName(this ISymbol symbol)
|
||||
{
|
||||
return symbol.IsKeywordOrReserved() ? $"@{symbol.Name}" : symbol.Name;
|
||||
}
|
||||
|
||||
public static string GetRefPrefix(this IParameterSymbol ps)
|
||||
{
|
||||
switch (ps.RefKind)
|
||||
{
|
||||
case RefKind.In:
|
||||
return "in ";
|
||||
|
||||
case RefKind.Out:
|
||||
return "out ";
|
||||
|
||||
case RefKind.Ref:
|
||||
return "ref ";
|
||||
|
||||
default:
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetParamsPrefix(this IParameterSymbol ps)
|
||||
{
|
||||
return ps.IsParams ? "params " : string.Empty;
|
||||
}
|
||||
|
||||
public static TypeEnum GetTypeEnum(this IPropertySymbol p)
|
||||
{
|
||||
return GetTypeEnum(p.Type);
|
||||
}
|
||||
|
||||
public static TypeEnum GetTypeEnum(this IParameterSymbol p)
|
||||
{
|
||||
return GetTypeEnum(p.Type);
|
||||
}
|
||||
|
||||
public static TypeEnum GetTypeEnum(this ITypeSymbol ts)
|
||||
{
|
||||
if (ts.IsValueType || ts.IsString())
|
||||
{
|
||||
return TypeEnum.ValueTypeOrString;
|
||||
}
|
||||
|
||||
if (ts.TypeKind == TypeKind.Interface)
|
||||
{
|
||||
return TypeEnum.Interface;
|
||||
}
|
||||
|
||||
return TypeEnum.Complex;
|
||||
}
|
||||
|
||||
public static bool IsString(this ITypeSymbol ts)
|
||||
{
|
||||
return ts.ToString() == "string" || ts.ToString() == "string?";
|
||||
}
|
||||
|
||||
public static string ToPropertyText(this IPropertySymbol property, string? overrideType = null)
|
||||
{
|
||||
var get = property.GetMethod != null ? "get; " : string.Empty;
|
||||
var set = property.SetMethod != null ? "set; " : string.Empty;
|
||||
|
||||
var type = !string.IsNullOrEmpty(overrideType) ? overrideType : $"{property.Type}";
|
||||
|
||||
return $"{type} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _Instance.{property.SanitizedName()}; " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.SanitizedName()} = value; " : string.Empty;
|
||||
|
||||
return $"{property.Type} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.SanitizedName()}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.SanitizedName()} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
|
||||
return $"{overrideType} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
public static string GetSanitizedName(this ISymbol symbol) =>
|
||||
symbol.IsKeywordOrReserved() ? $"@{symbol.Name}" : symbol.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
{
|
||||
internal static class TypeSymbolExtensions
|
||||
{
|
||||
public static TypeEnum GetTypeEnum(this ITypeSymbol ts)
|
||||
{
|
||||
if (ts.IsValueType || ts.IsString())
|
||||
{
|
||||
return TypeEnum.ValueTypeOrString;
|
||||
}
|
||||
|
||||
if (ts.TypeKind == TypeKind.Interface)
|
||||
{
|
||||
return TypeEnum.Interface;
|
||||
}
|
||||
|
||||
return TypeEnum.Complex;
|
||||
}
|
||||
|
||||
public static bool IsString(this ITypeSymbol ts)
|
||||
{
|
||||
return ts.ToString() == "string" || ts.ToString() == "string?";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,11 +24,11 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return GetReplacedType(property.Type, out isReplaced);
|
||||
}
|
||||
|
||||
protected string GetReplacedType(ITypeSymbol property, out bool isReplaced)
|
||||
protected string GetReplacedType(ITypeSymbol typeSymbol, out bool isReplaced)
|
||||
{
|
||||
isReplaced = false;
|
||||
|
||||
var typeSymbolAsString = property.ToString();
|
||||
var typeSymbolAsString = typeSymbol.ToString();
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeSymbolAsString);
|
||||
if (existing is not null)
|
||||
@@ -42,7 +42,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return existing.InterfaceName;
|
||||
}
|
||||
|
||||
if (property is INamedTypeSymbol namedTypedSymbol)
|
||||
if (typeSymbol is INamedTypeSymbol namedTypedSymbol)
|
||||
{
|
||||
var propertyTypeAsStringToBeModified = typeSymbolAsString;
|
||||
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
@@ -65,7 +65,6 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
return propertyTypeAsStringToBeModified;
|
||||
}
|
||||
|
||||
|
||||
return typeSymbolAsString;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace {ns}
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
var type = ps.GetTypeEnum() == TypeEnum.Complex ? GetParameterType(ps, out _) : ps.Type.ToString();
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.SanitizedName()}");
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
}
|
||||
|
||||
str.AppendLine($" {GetReplacedType(method.ReturnType, out _)} {method.Name}({string.Join(", ", methodParameters)});");
|
||||
|
||||
@@ -129,17 +129,19 @@ namespace {ns}
|
||||
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
if (ps.GetTypeEnum() == TypeEnum.Complex)
|
||||
{
|
||||
var type = GetParameterType(ps, out _);
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.SanitizedName()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{ps.Type} {ps.SanitizedName()}");
|
||||
}
|
||||
var type = GetParameterType(ps, out _);
|
||||
//if (ps.GetTypeEnum() == TypeEnum.Complex)
|
||||
//{
|
||||
// var type = GetParameterType(ps, out _);
|
||||
// methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{ps.Type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
//}
|
||||
|
||||
invokeParameters.Add($"{ps.GetRefPrefix()}{ps.SanitizedName()}_");
|
||||
methodParameters.Add($"{ps.GetParamsPrefix()}{ps.GetRefPrefix()}{type} {ps.GetSanitizedName()}{ps.GetDefaultValue()}");
|
||||
invokeParameters.Add($"{ps.GetRefPrefix()}{ps.GetSanitizedName()}_");
|
||||
}
|
||||
|
||||
string returnTypeAsString = GetReplacedType(method.ReturnType, out var returnIsReplaced);
|
||||
@@ -148,7 +150,7 @@ namespace {ns}
|
||||
str.AppendLine(" {");
|
||||
foreach (var ps in method.Parameters)
|
||||
{
|
||||
string normalOrMap = $" = {ps.SanitizedName()}";
|
||||
string normalOrMap = $" = {ps.GetSanitizedName()}";
|
||||
if (ps.RefKind == RefKind.Out)
|
||||
{
|
||||
normalOrMap = string.Empty;
|
||||
@@ -158,11 +160,11 @@ namespace {ns}
|
||||
var type = GetParameterType(ps, out var isReplaced);
|
||||
if (isReplaced)
|
||||
{
|
||||
normalOrMap = $" = _mapper.Map<{ps.Type}>({ps.SanitizedName()})";
|
||||
normalOrMap = $" = _mapper.Map<{ps.Type}>({ps.GetSanitizedName()})";
|
||||
}
|
||||
}
|
||||
|
||||
str.AppendLine($" {ps.Type} {ps.SanitizedName()}_{normalOrMap};");
|
||||
str.AppendLine($" {ps.Type} {ps.GetSanitizedName()}_{normalOrMap};");
|
||||
}
|
||||
|
||||
#pragma warning disable RS1024 // Compare symbols correctly
|
||||
@@ -181,17 +183,17 @@ namespace {ns}
|
||||
|
||||
foreach (var ps in method.Parameters.Where(p => p.RefKind == RefKind.Out))
|
||||
{
|
||||
string normalOrMap = $" = {ps.SanitizedName()}_";
|
||||
string normalOrMap = $" = {ps.GetSanitizedName()}_";
|
||||
if (ps.GetTypeEnum() == TypeEnum.Complex)
|
||||
{
|
||||
var type = GetParameterType(ps, out var isReplaced);
|
||||
if (isReplaced)
|
||||
{
|
||||
normalOrMap = $" = _mapper.Map<{type}>({ps.SanitizedName()}_)";
|
||||
normalOrMap = $" = _mapper.Map<{type}>({ps.GetSanitizedName()}_)";
|
||||
}
|
||||
}
|
||||
|
||||
str.AppendLine($" {ps.SanitizedName()}{normalOrMap};");
|
||||
str.AppendLine($" {ps.GetSanitizedName()}{normalOrMap};");
|
||||
}
|
||||
|
||||
if (returnTypeAsString != "void")
|
||||
|
||||
Reference in New Issue
Block a user