Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
#68: Add support for string<->enum conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Mar 9, 2019
1 parent 879fb2e commit 6c01358
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData
public Nullable<decimal> Savings { get; set; }
public DateTime Birthday { get; set; }
public AuthenticationKind Authentication { get; set;}
public string SecondAuthentication { get; set;}
public AuthenticationKind ThirdAuthentication { get; set;}
}

public class UserSourceDTO
Expand Down Expand Up @@ -92,6 +94,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData
public Nullable<decimal> Savings { get; set; }
public DateTime Birthday { get; set; }
public AuthenticationKind Authentication { get; set;}
public AuthenticationKind SecondAuthentication { get; set;}
public string ThirdAuthentication { get; set;}
}

public class AddressEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData
ExtraSavings = entity.ExtraSavings.Value,
Savings = entity.Savings,
Birthday = entity.Birthday,
Authentication = entity.Authentication
Authentication = entity.Authentication,
SecondAuthentication = entity.SecondAuthentication.ToString(),
ThirdAuthentication = Enum.Parse(typeof(AuthenticationKind), entity.ThirdAuthentication, true)
};
}
}
Expand All @@ -69,6 +71,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData
public Nullable<decimal> Savings { get; set; }
public DateTime Birthday { get; set; }
public AuthenticationKind Authentication { get; set;}
public string SecondAuthentication { get; set;}
public AuthenticationKind ThirdAuthentication { get; set;}
}

public class UserSourceDTO
Expand Down Expand Up @@ -128,6 +132,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData
public Nullable<decimal> Savings { get; set; }
public DateTime Birthday { get; set; }
public AuthenticationKind Authentication { get; set;}
public AuthenticationKind SecondAuthentication { get; set;}
public string ThirdAuthentication { get; set;}
}

public class AddressEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ private MappingElement TryToUnwrap(ITypeSymbol targetType, MappingElement elemen
Expression = (ExpressionSyntax) syntaxGenerator.MemberAccessExpression(sourceAccess, wrapper.UnwrappingProperty.Name),
ExpressionType = wrapper.UnwrappingProperty.Type
};
}else if (wrapper.Type == WrapperInfoType.Method)
}
if (wrapper.Type == WrapperInfoType.Method)
{
var unwrappingMethodAccess = syntaxGenerator.MemberAccessExpression(sourceAccess, wrapper.UnwrappingMethod.Name);

Expand All @@ -241,7 +242,33 @@ private MappingElement TryToUnwrap(ITypeSymbol targetType, MappingElement elemen
};
}

}else if(conversion.IsExplicit)
if (targetType.SpecialType == SpecialType.System_String && element.ExpressionType.TypeKind == TypeKind.Enum)
{
var toStringAccess = syntaxGenerator.MemberAccessExpression(element.Expression, "ToString");
return new MappingElement()
{
Expression = (InvocationExpressionSyntax)syntaxGenerator.InvocationExpression(toStringAccess),
ExpressionType = targetType
};
}

if (element.ExpressionType.SpecialType == SpecialType.System_String && targetType.TypeKind == TypeKind.Enum)
{
var parseEnumAccess = syntaxGenerator.MemberAccessExpression(SyntaxFactory.ParseTypeName("Enum"), "Parse");
return new MappingElement()
{
Expression = (InvocationExpressionSyntax)syntaxGenerator.InvocationExpression(parseEnumAccess, new[]
{
syntaxGenerator.TypeOfExpression(SyntaxFactory.ParseTypeName(targetType.Name)),
element.Expression,
syntaxGenerator.TrueLiteralExpression()
}),
ExpressionType = targetType
};
}

}
else if(conversion.IsExplicit)
{
return new MappingElement()
{
Expand Down

0 comments on commit 6c01358

Please sign in to comment.