From e3143877729241799c8c578bc4efb062ace8ba00 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Mon, 6 Dec 2021 16:26:14 +0000 Subject: [PATCH] Struct context --- src/MapTo/MappingContext.cs | 1 + src/MapTo/StructMappingContext.cs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/MapTo/StructMappingContext.cs diff --git a/src/MapTo/MappingContext.cs b/src/MapTo/MappingContext.cs index 59160c4..37c3a80 100644 --- a/src/MapTo/MappingContext.cs +++ b/src/MapTo/MappingContext.cs @@ -61,6 +61,7 @@ namespace MapTo MappingContext context = typeSyntax switch { ClassDeclarationSyntax => new ClassMappingContext(compilation, sourceGenerationOptions, typeSyntax), + StructDeclarationSyntax => new StructMappingContext(compilation, sourceGenerationOptions, typeSyntax), RecordDeclarationSyntax => new RecordMappingContext(compilation, sourceGenerationOptions, typeSyntax), _ => throw new ArgumentOutOfRangeException() }; diff --git a/src/MapTo/StructMappingContext.cs b/src/MapTo/StructMappingContext.cs new file mode 100644 index 0000000..25d0173 --- /dev/null +++ b/src/MapTo/StructMappingContext.cs @@ -0,0 +1,27 @@ +using System.Collections.Immutable; +using System.Linq; +using MapTo.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace MapTo +{ + internal class StructMappingContext : MappingContext + { + internal StructMappingContext(Compilation compilation, SourceGenerationOptions sourceGenerationOptions, TypeDeclarationSyntax typeSyntax) + : base(compilation, sourceGenerationOptions, typeSyntax) { } + + protected override ImmutableArray GetMappedProperties(ITypeSymbol typeSymbol, ITypeSymbol sourceTypeSymbol, bool hasInheritedClass) + { + var sourceProperties = sourceTypeSymbol.GetAllMembers().OfType().ToArray(); + + return typeSymbol + .GetAllMembers() + .OfType() + .Where(p => !p.HasAttribute(IgnorePropertyAttributeTypeSymbol)) + .Select(property => MapProperty(sourceTypeSymbol, sourceProperties, property)) + .Where(mappedProperty => mappedProperty is not null) + .ToImmutableArray()!; + } + } +} \ No newline at end of file