Skip to content

Commit

Permalink
Refactor code and reduce duplication (#176)
Browse files Browse the repository at this point in the history
Puts all wrapper logic in one place and dedups the methods that had been
copied. Also removes wrapper interfaces, since they can't participate in
deserialization.
  • Loading branch information
agocke committed Jul 3, 2024
1 parent 53b8baf commit 39275e8
Show file tree
Hide file tree
Showing 35 changed files with 698 additions and 1,265 deletions.
4 changes: 2 additions & 2 deletions src/generator/Generator.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ private static MethodDeclarationSyntax GenerateCustomDeserializeMethod(
var m = members[i];
string wrapperName;
var memberType = m.Type.WithNullableAnnotation(m.NullableAnnotation).ToDisplayString();
if (TryGetExplicitWrapper(m, context, SerdeUsage.Deserialize, inProgress) is { } explicitWrap)
if (Wrappers.TryGetExplicitWrapper(m, context, SerdeUsage.Deserialize, inProgress) is { } explicitWrap)
{
wrapperName = explicitWrap.ToString();
}
else if (ImplementsSerde(m.Type, m.Type, context, SerdeUsage.Deserialize))
{
wrapperName = memberType;
}
else if (TryGetAnyWrapper(m.Type, context, SerdeUsage.Deserialize, inProgress) is { } wrap)
else if (Wrappers.TryGetImplicitWrapper(m.Type, context, SerdeUsage.Deserialize, inProgress) is { Wrapper: {} wrap })
{
wrapperName = wrap.ToString();
}
Expand Down
75 changes: 0 additions & 75 deletions src/generator/Generator.Helpers.cs

This file was deleted.

83 changes: 2 additions & 81 deletions src/generator/Generator.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,6 @@ internal enum SerdeUsage : byte
Both = Serialize | Deserialize,
}

internal sealed class GeneratorExecutionContext
{
private bool _frozen = false;
private readonly List<Diagnostic> _diagnostics = new();
private readonly SortedSet<(string FileName, string Content)> _sources = new();

public Compilation Compilation { get; }

public GeneratorExecutionContext(GeneratorAttributeSyntaxContext context)
{
Compilation = context.SemanticModel.Compilation;
}

public GenerationOutput GetOutput()
{
_frozen = true;
return new GenerationOutput(_diagnostics, _sources);
}

public void ReportDiagnostic(Diagnostic diagnostic)
{
if (_frozen)
throw new InvalidOperationException("Cannot add diagnostics after GetDiagnostics() has been called.");
_diagnostics.Add(diagnostic);
}

internal void AddSource(string fileName, string content)
{
if (_frozen)
throw new InvalidOperationException("Cannot add sources after GetSources() has been called.");
_sources.Add((fileName, content));
}
}

partial class SerdeImplRoslynGenerator
{
internal static void GenerateImpl(
Expand All @@ -68,7 +34,7 @@ internal static void GenerateImpl(
// Generate statements for the implementation
var (implMembers, baseList) = usage switch
{
SerdeUsage.Serialize => SerializeImplRoslynGenerator.GenerateSerializeGenericImpl(context, receiverType, inProgress),
SerdeUsage.Serialize => SerializeImplGen.GenSerialize(context, receiverType, inProgress),
SerdeUsage.Deserialize => DeserializeImplGenerator.GenerateDeserializeImpl(context, receiverType, inProgress),
_ => throw ExceptionUtilities.Unreachable
};
Expand All @@ -77,7 +43,7 @@ internal static void GenerateImpl(
MemberDeclarationSyntax newType;
if (typeKind == SyntaxKind.EnumDeclaration)
{
var wrapperName = GetWrapperName(typeName);
var wrapperName = Wrappers.GetWrapperName(typeName);
newType = StructDeclaration(
attributeLists: default,
modifiers: TokenList(Token(SyntaxKind.PartialKeyword)),
Expand Down Expand Up @@ -169,34 +135,6 @@ internal static bool ImplementsSerde(ITypeSymbol targetType, ITypeSymbol argType
return false;
}

private static TypeSyntax? TryGetEnumWrapper(ITypeSymbol type, SerdeUsage usage)
{
if (type.TypeKind is not TypeKind.Enum)
{
return null;
}

// Check for the generation attributes
if (!HasGenerateAttribute(type, usage))
{
return null;
}

var wrapperName = GetWrapperName(type.Name);
var containing = type.ContainingType?.ToDisplayString();
if (containing is null && type.ContainingNamespace is { IsGlobalNamespace: false } ns)
{
containing = ns.ToDisplayString();
}
var wrapperFqn = containing is not null
? containing + "." + wrapperName
: "global::" + wrapperName;

return SyntaxFactory.ParseTypeName(wrapperFqn);
}

internal static string GetWrapperName(string typeName) => typeName + "Wrap";

internal static bool HasGenerateAttribute(ITypeSymbol memberType, SerdeUsage usage)
{
var attributes = memberType.GetAttributes();
Expand Down Expand Up @@ -224,21 +162,4 @@ internal static bool HasGenerateAttribute(ITypeSymbol memberType, SerdeUsage usa
}
return false;
}
}

internal static class WrapUsageExtensions
{
public static string GetInterfaceName(this SerdeUsage usage) => usage switch
{
SerdeUsage.Serialize => "ISerialize",
SerdeUsage.Deserialize => "IDeserialize",
_ => throw ExceptionUtilities.Unreachable
};

public static string GetImplName(this SerdeUsage usage) => usage switch
{
SerdeUsage.Serialize => "SerializeImpl",
SerdeUsage.Deserialize => "DeserializeImpl",
_ => throw ExceptionUtilities.Unreachable
};
}
Loading

0 comments on commit 39275e8

Please sign in to comment.