Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enum: Add EnumConverter with underlying type lookup #9

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text;
using AltV.Community.MValueAdapters.Generators.Models;

namespace AltV.Community.MValueAdapters.Generators.Converters;

internal class EnumConverter : BaseConverter
{
protected override void GenerateItemWriteCode(StringBuilder stringBuilder, ref int indentation, MValueClassInfo classInfo, MValuePropertyInfo propertyInfo)
{
stringBuilder.AppendLine(indentation, $"writer.Value(({propertyInfo.UnderlyingEnumTypeName})value.{propertyInfo.Name});");
riffy marked this conversation as resolved.
Show resolved Hide resolved
}

protected override void GenerateItemReadCode(StringBuilder stringBuilder, ref int indentation, MValueClassInfo classInfo, MValuePropertyInfo propertyInfo)
{
stringBuilder.AppendLine(indentation, $"c.{propertyInfo.Name} = ({propertyInfo.TypeName})reader.{Next(propertyInfo)}();");
}

protected override void GenerateCollectionWriteCode(StringBuilder stringBuilder, ref int indentation, MValueClassInfo classInfo, MValuePropertyInfo propertyInfo)
{
stringBuilder.AppendLine(indentation, $"writer.Value(({propertyInfo.UnderlyingEnumTypeName})item);");
}

protected override void GenerateCollectionReadCode(StringBuilder stringBuilder, ref int indentation, MValueClassInfo classInfo, MValuePropertyInfo propertyInfo)
{
stringBuilder.AppendLine(indentation, $"{propertyInfo.Name}Builder.Add(({propertyInfo.TypeName})reader.{Next(propertyInfo)}());");
}

private string Next(MValuePropertyInfo propertyInfo)
{
if (IsUnderlyingText(propertyInfo)) return "NextString";
else if (IsUnderlyingBool(propertyInfo)) return "NextBool";
else
return "NextDouble";
}

private bool IsUnderlyingText(MValuePropertyInfo propertyInfo)
{
return propertyInfo.UnderlyingEnumTypeName == "string" || propertyInfo.UnderlyingEnumTypeName == "char";
}

private bool IsUnderlyingBool(MValuePropertyInfo propertyInfo)
{
return propertyInfo.UnderlyingEnumTypeName == "bool";
}
}
24 changes: 18 additions & 6 deletions AltV.Community.MValueAdapters.Generators/MValueAdapterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public MValueAdapterGenerator()
{ "char", new CharConverter() },
// String
{ "string", new StringConverter() },
// Enum
{ "enum", new EnumConverter() },
// Guid
{ "Guid", new GuidConverter() },
// Numerics
Expand Down Expand Up @@ -151,11 +153,18 @@ private MValuePropertyInfo[] GetClassProperties(SemanticModel semanticModel, Cla
// ReSharper disable once TooWideLocalVariableScope
bool skipProperty;
string? customName;

string? underlyingEnumTypeName;
foreach (var propertyDeclarationSyntax in classProperties)
{
skipProperty = false;
customName = null;
underlyingEnumTypeName = null;

// Check if the property is an enum and retrieve underlying type
if (semanticModel.GetTypeInfo(propertyDeclarationSyntax.Type).Type is INamedTypeSymbol namedTypeSymbol &&
namedTypeSymbol.TypeKind == TypeKind.Enum)
underlyingEnumTypeName = namedTypeSymbol.EnumUnderlyingType!.ToString();


foreach (var attributeListSyntax in propertyDeclarationSyntax.AttributeLists)
{
Expand Down Expand Up @@ -185,7 +194,7 @@ private MValuePropertyInfo[] GetClassProperties(SemanticModel semanticModel, Cla
customName = NamingConventionHelpers.GetName(propertyDeclarationSyntax.Identifier.ValueText, namingConvention);

var propertyData = GetPropertyData(propertyDeclarationSyntax.Type.ToString());
handledProperties.Add(new MValuePropertyInfo(propertyData, propertyDeclarationSyntax.Identifier.ValueText, customName));
handledProperties.Add(new MValuePropertyInfo(propertyData, propertyDeclarationSyntax.Identifier.ValueText, customName, underlyingEnumTypeName));
}

return handledProperties.ToArray();
Expand Down Expand Up @@ -261,12 +270,15 @@ private void Execute(ImmutableArray<MValueClassInfo?> classes, SourceProductionC

foreach (var propertyInfo in classInfo.PropertyInfos)
{
if (!_typeConverters.TryGetValue(propertyInfo.TypeName, out var converter))
// Use underlying type for enums or base type for others
string typeName = (propertyInfo.IsEnum ? "enum" : propertyInfo.TypeName)!;

if (!_typeConverters.TryGetValue(typeName, out var converter))
{
foreach (var @class in classes)
{
if (@class is null || !@class.Name.Equals(propertyInfo.TypeName, StringComparison.Ordinal)) continue;
converter = new ByAdapterConverter(propertyInfo.TypeName);
if (@class is null || !@class.Name.Equals(typeName, StringComparison.Ordinal)) continue;
converter = new ByAdapterConverter(typeName);
additionalUsings.Add(@class.Namespace);
break;
}
Expand All @@ -275,7 +287,7 @@ private void Execute(ImmutableArray<MValueClassInfo?> classes, SourceProductionC
var diagnostic = Diagnostic.Create(
"MVC0001",
"Source Generator",
$"Unsupported type {propertyInfo.TypeName} at property {propertyInfo.Name}",
$"Unsupported type {propertyInfo.TypeName} at property {propertyInfo.Name} - Enum: {propertyInfo.IsEnum}",
DiagnosticSeverity.Error,
DiagnosticSeverity.Error,
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace AltV.Community.MValueAdapters.Generators.Models;
using System.Collections.Generic;

namespace AltV.Community.MValueAdapters.Generators.Models;

internal class MValuePropertyInfo
{
Expand All @@ -8,13 +10,17 @@ internal class MValuePropertyInfo
internal readonly string? CustomName;
internal readonly bool Nullable;
internal readonly bool NullableCollection;
internal bool IsEnum => UnderlyingEnumTypeName is not null;
// If set, prop is considered enum
internal readonly string? UnderlyingEnumTypeName;

internal MValuePropertyInfo(PropertyData propertyData, string name, string? customName = null)
internal MValuePropertyInfo(PropertyData propertyData, string name, string? customName = null, string? underlyingEnumTypeName = null)
{
PropertyType = propertyData.Type;
TypeName = propertyData.TypeName;
Nullable = propertyData.Nullable;
NullableCollection = propertyData.NullableCollection;
UnderlyingEnumTypeName = underlyingEnumTypeName;

Name = name;
CustomName = customName;
Expand Down