private Maps Validate(MapReceiver receiver, Compilation compilation)
        {
            var maps = ImmutableDictionary.CreateBuilder <(ITypeSymbol, ITypeSymbol), (ImmutableArray <Diagnostic>, SyntaxNode, ImmutableArray <string>)>();

            MappingInformation.ValidateMapTo(receiver.MapToCandidates, maps, compilation);
            MappingInformation.ValidateMapFrom(receiver.MapFromCandidates, maps, compilation);
            MappingInformation.ValidateMap(receiver.MapCandidates, maps, compilation);

            return(maps.ToImmutable());
        }
示例#2
0
        private Maps Validate(MapReceiver receiver, Compilation compilation)
        {
            var maps = ImmutableDictionary.CreateBuilder <(ITypeSymbol, ITypeSymbol), (ImmutableArray <Diagnostic>, SyntaxNode, ImmutableArray <string>)>();

            foreach (var target in receiver.Targets)
            {
                MappingInformation.ValidatePairs(target.origination, target.source, target.destination, maps);
            }

            return(maps.ToImmutable());
        }
示例#3
0
        private static (ImmutableArray <Diagnostic> diagnostics, string?name, SourceText?text) GenerateMapping(
            ITypeSymbol sourceType, AttributeData attributeData, ConfigurationValues configurationValues)
        {
            var information = new MappingInformation(sourceType, attributeData);

            if (!information.Diagnostics.Any(_ => _.Severity == DiagnosticSeverity.Error))
            {
                var text = new MappingBuilder(information, configurationValues).Text;
                return(information.Diagnostics, $"{sourceType.Name}_To_{information.DestinationType.Name}_Map.g.cs", text);
            }

            return(information.Diagnostics, null, null);
        }
        private static void ValidateMap(List <AttributeSyntax> mapCandidates, Maps.Builder maps, Compilation compilation)
        {
            var mapAttributeSymbol = compilation.GetTypeByMetadataName(typeof(MapAttribute).FullName);

            foreach (var candidateAttributeNode in mapCandidates)
            {
                var attributeSymbol = compilation.GetSemanticModel(candidateAttributeNode.SyntaxTree)
                                      .GetSymbolInfo(candidateAttributeNode).Symbol !.ContainingSymbol;

                if (attributeSymbol.Equals(mapAttributeSymbol, SymbolEqualityComparer.Default))
                {
                    var attributeData = compilation.Assembly.GetAttributes().Single(
                        _ => _.ApplicationSyntaxReference !.GetSyntax() == candidateAttributeNode);
                    var sourceType      = (INamedTypeSymbol)attributeData.ConstructorArguments[0].Value !;
                    var destinationType = (INamedTypeSymbol)attributeData.ConstructorArguments[1].Value !;
                    MappingInformation.ValidatePairs(candidateAttributeNode, sourceType, destinationType, maps);
                }
            }
        }
        private static ImmutableArray <(ImmutableArray <Diagnostic> diagnostics, string?name, SourceText?text)> GenerateMappings(
            MapReceiver receiver, Compilation compilation, AnalyzerConfigOptionsProvider optionsProvider)
        {
            var results     = ImmutableArray.CreateBuilder <(ImmutableArray <Diagnostic> diagnostics, string?name, SourceText?text)>();
            var information = new MappingInformation(receiver, compilation);

            foreach (var mapPair in information.Maps)
            {
                if (!mapPair.Value.diagnostics.Any(_ => _.Severity == DiagnosticSeverity.Error))
                {
                    var text = new MappingBuilder(mapPair.Key.source, mapPair.Key.destination, mapPair.Value.maps,
                                                  new ConfigurationValues(optionsProvider, mapPair.Value.node.SyntaxTree)).Text;
                    results.Add((mapPair.Value.diagnostics, $"{mapPair.Key.source.Name}_To_{mapPair.Key.destination.Name}_Map.g.cs", text));
                }
                else
                {
                    results.Add((mapPair.Value.diagnostics, null, null));
                }
            }

            return(results.ToImmutable());
        }
示例#6
0
        private static SourceText Build(MappingInformation information, ConfigurationValues configurationValues)
        {
            using var writer       = new StringWriter();
            using var indentWriter = new IndentedTextWriter(writer,
                                                            configurationValues.IndentStyle == IndentStyle.Tab ? "\t" : new string (' ', (int)configurationValues.IndentSize));

            var usingStatements = new SortedSet <string>();

            if (!information.SourceType.IsValueType)
            {
                usingStatements.Add("using System;");
            }
            ;

            if (!information.DestinationType.ContainingNamespace.IsGlobalNamespace &&
                !information.SourceType.ContainingNamespace.ToDisplayString().StartsWith(
                    information.DestinationType.ContainingNamespace.ToDisplayString(), StringComparison.InvariantCulture))
            {
                usingStatements.Add($"using {information.DestinationType.ContainingNamespace.ToDisplayString()};");
            }

            foreach (var usingStatement in usingStatements)
            {
                indentWriter.WriteLine(usingStatement);
            }

            if (usingStatements.Count > 0)
            {
                indentWriter.WriteLine();
            }

            if (!information.SourceType.ContainingNamespace.IsGlobalNamespace)
            {
                indentWriter.WriteLine($"namespace {information.SourceType.ContainingNamespace.ToDisplayString()}");
                indentWriter.WriteLine("{");
                indentWriter.Indent++;
            }

            indentWriter.WriteLine($"public static partial class {information.SourceType.Name}MapToExtensions");
            indentWriter.WriteLine("{");
            indentWriter.Indent++;

            indentWriter.WriteLine($"public static {information.DestinationType.Name} MapTo{information.DestinationType.Name}(this {information.SourceType.Name} self) =>");
            indentWriter.Indent++;

            if (!information.SourceType.IsValueType)
            {
                indentWriter.WriteLine("self is null ? throw new ArgumentNullException(nameof(self)) :");
                indentWriter.Indent++;
            }

            indentWriter.WriteLine($"new {information.DestinationType.Name}");
            indentWriter.WriteLine("{");
            indentWriter.Indent++;

            foreach (var map in information.Maps)
            {
                indentWriter.WriteLine(map);
            }

            indentWriter.Indent--;
            indentWriter.WriteLine("};");

            if (!information.SourceType.IsValueType)
            {
                indentWriter.Indent--;
            }

            indentWriter.Indent--;
            indentWriter.Indent--;
            indentWriter.WriteLine("}");

            if (!information.SourceType.ContainingNamespace.IsGlobalNamespace)
            {
                indentWriter.Indent--;
                indentWriter.WriteLine("}");
            }

            return(SourceText.From(writer.ToString(), Encoding.UTF8));
        }
示例#7
0
 public MappingBuilder(MappingInformation information, ConfigurationValues configurationValues) =>
 this.Text = MappingBuilder.Build(information, configurationValues);