| | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | 2 | |
|
| | 3 | | using DotNetApiDiff.Interfaces; |
| | 4 | | using DotNetApiDiff.Models.Configuration; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace DotNetApiDiff.ApiExtraction; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Implements namespace and type name mapping between assemblies |
| | 11 | | /// </summary> |
| | 12 | | public class NameMapper : INameMapper |
| | 13 | | { |
| | 14 | | private readonly ILogger<NameMapper> _logger; |
| | 15 | | private readonly MappingConfiguration _configuration; |
| | 16 | | private readonly StringComparison _stringComparison; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Creates a new instance of the NameMapper |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="configuration">Mapping configuration</param> |
| | 22 | | /// <param name="logger">Logger for diagnostic information</param> |
| 26 | 23 | | public NameMapper(MappingConfiguration configuration, ILogger<NameMapper> logger) |
| 26 | 24 | | { |
| 26 | 25 | | _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); |
| 26 | 26 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 26 | 27 | | _stringComparison = configuration.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| 26 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets the mapping configuration |
| | 32 | | /// </summary> |
| 0 | 33 | | public MappingConfiguration Configuration => _configuration; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Maps a source namespace to one or more target namespaces |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="sourceNamespace">The source namespace to map</param> |
| | 39 | | /// <returns>A collection of mapped target namespaces</returns> |
| | 40 | | public IEnumerable<string> MapNamespace(string sourceNamespace) |
| 14 | 41 | | { |
| 14 | 42 | | if (string.IsNullOrEmpty(sourceNamespace)) |
| 0 | 43 | | { |
| 0 | 44 | | return new[] { string.Empty }; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | // Check for exact match first |
| 55 | 48 | | foreach (var mapping in _configuration.NamespaceMappings) |
| 10 | 49 | | { |
| 10 | 50 | | if (string.Equals(mapping.Key, sourceNamespace, _stringComparison)) |
| 7 | 51 | | { |
| 7 | 52 | | _logger.LogDebug( |
| 7 | 53 | | "Mapped namespace {SourceNamespace} to {TargetNamespaces}", |
| 7 | 54 | | sourceNamespace, |
| 7 | 55 | | string.Join(", ", mapping.Value)); |
| 7 | 56 | | return mapping.Value; |
| | 57 | | } |
| 3 | 58 | | } |
| | 59 | |
|
| | 60 | | // Check for prefix matches (e.g., "Company.Product" -> "NewCompany.Product") |
| 25 | 61 | | foreach (var mapping in _configuration.NamespaceMappings) |
| 3 | 62 | | { |
| 3 | 63 | | if (sourceNamespace.StartsWith(mapping.Key + ".", _stringComparison)) |
| 2 | 64 | | { |
| 2 | 65 | | var suffix = sourceNamespace.Substring(mapping.Key.Length + 1); |
| 5 | 66 | | var results = mapping.Value.Select(target => CombineNamespaceParts(target, suffix)).ToList(); |
| | 67 | |
|
| 2 | 68 | | _logger.LogDebug( |
| 2 | 69 | | "Mapped namespace {SourceNamespace} to {TargetNamespaces} using prefix mapping", |
| 2 | 70 | | sourceNamespace, |
| 2 | 71 | | string.Join(", ", results)); |
| 2 | 72 | | return results; |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | // No mapping found, return the original |
| 5 | 77 | | return new[] { sourceNamespace }; |
| 14 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Maps a source type name to a target type name |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="sourceTypeName">The source type name to map</param> |
| | 84 | | /// <returns>The mapped target type name, or the original if no mapping exists</returns> |
| | 85 | | public string MapTypeName(string sourceTypeName) |
| 11 | 86 | | { |
| 11 | 87 | | if (string.IsNullOrEmpty(sourceTypeName)) |
| 0 | 88 | | { |
| 0 | 89 | | return string.Empty; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | // Check for exact match |
| 39 | 93 | | foreach (var mapping in _configuration.TypeMappings) |
| 5 | 94 | | { |
| 5 | 95 | | if (string.Equals(mapping.Key, sourceTypeName, _stringComparison)) |
| 4 | 96 | | { |
| 4 | 97 | | _logger.LogDebug( |
| 4 | 98 | | "Mapped type name {SourceTypeName} to {TargetTypeName}", |
| 4 | 99 | | sourceTypeName, |
| 4 | 100 | | mapping.Value); |
| 4 | 101 | | return mapping.Value; |
| | 102 | | } |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | // No mapping found, return the original |
| 7 | 106 | | return sourceTypeName; |
| 11 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Maps a fully qualified type name (namespace + type name) to one or more possible target type names |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="sourceFullName">The fully qualified source type name</param> |
| | 113 | | /// <returns>A collection of possible mapped target type names</returns> |
| | 114 | | public IEnumerable<string> MapFullTypeName(string sourceFullName) |
| 8 | 115 | | { |
| 8 | 116 | | if (string.IsNullOrEmpty(sourceFullName)) |
| 0 | 117 | | { |
| 0 | 118 | | return new[] { string.Empty }; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | // Check for exact type mapping first |
| 27 | 122 | | foreach (var mapping in _configuration.TypeMappings) |
| 2 | 123 | | { |
| 2 | 124 | | if (string.Equals(mapping.Key, sourceFullName, _stringComparison)) |
| 1 | 125 | | { |
| 1 | 126 | | _logger.LogDebug( |
| 1 | 127 | | "Mapped full type name {SourceFullName} to {TargetFullName} using exact type mapping", |
| 1 | 128 | | sourceFullName, |
| 1 | 129 | | mapping.Value); |
| 1 | 130 | | return new[] { mapping.Value }; |
| | 131 | | } |
| 1 | 132 | | } |
| | 133 | |
|
| | 134 | | // If no exact match, try to split into namespace and type name |
| 7 | 135 | | int lastDotIndex = sourceFullName.LastIndexOf('.'); |
| 7 | 136 | | if (lastDotIndex <= 0) |
| 0 | 137 | | { |
| | 138 | | // No namespace part or empty namespace |
| 0 | 139 | | return new[] { MapTypeName(sourceFullName) }; |
| | 140 | | } |
| | 141 | |
|
| 7 | 142 | | string sourceNamespace = sourceFullName.Substring(0, lastDotIndex); |
| 7 | 143 | | string sourceType = sourceFullName.Substring(lastDotIndex + 1); |
| | 144 | |
|
| | 145 | | // Map the namespace and type separately |
| 7 | 146 | | var mappedNamespaces = MapNamespace(sourceNamespace); |
| 7 | 147 | | string mappedType = MapTypeName(sourceType); |
| | 148 | |
|
| | 149 | | // Combine the mapped namespaces with the mapped type |
| 7 | 150 | | var results = mappedNamespaces |
| 9 | 151 | | .Select(ns => CombineNamespaceParts(ns, mappedType)) |
| 7 | 152 | | .ToList(); |
| | 153 | |
|
| 7 | 154 | | if (results.Count > 1) |
| 2 | 155 | | { |
| 2 | 156 | | _logger.LogDebug( |
| 2 | 157 | | "Mapped full type name {SourceFullName} to multiple targets: {TargetFullNames}", |
| 2 | 158 | | sourceFullName, |
| 2 | 159 | | string.Join(", ", results)); |
| 2 | 160 | | } |
| 5 | 161 | | else if (results.Count == 1) |
| 5 | 162 | | { |
| 5 | 163 | | _logger.LogDebug( |
| 5 | 164 | | "Mapped full type name {SourceFullName} to {TargetFullName}", |
| 5 | 165 | | sourceFullName, |
| 5 | 166 | | results[0]); |
| 5 | 167 | | } |
| | 168 | |
|
| 7 | 169 | | return results; |
| 8 | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// Checks if a type name should be auto-mapped based on configuration |
| | 174 | | /// </summary> |
| | 175 | | /// <param name="typeName">The type name to check</param> |
| | 176 | | /// <returns>True if the type should be auto-mapped, false otherwise</returns> |
| | 177 | | public bool ShouldAutoMapType(string typeName) |
| 6 | 178 | | { |
| 6 | 179 | | if (!_configuration.AutoMapSameNameTypes) |
| 3 | 180 | | { |
| 3 | 181 | | return false; |
| | 182 | | } |
| | 183 | |
|
| 3 | 184 | | if (string.IsNullOrEmpty(typeName)) |
| 0 | 185 | | { |
| 0 | 186 | | return false; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | // Extract the simple type name (without namespace) |
| 3 | 190 | | int lastDotIndex = typeName.LastIndexOf('.'); |
| 3 | 191 | | if (lastDotIndex <= 0) |
| 0 | 192 | | { |
| | 193 | | // No namespace part or empty namespace |
| 0 | 194 | | return false; |
| | 195 | | } |
| | 196 | |
|
| 3 | 197 | | string simpleTypeName = typeName.Substring(lastDotIndex + 1); |
| | 198 | |
|
| | 199 | | // Don't auto-map generic type definitions with backticks |
| 3 | 200 | | if (simpleTypeName.Contains('`')) |
| 1 | 201 | | { |
| 1 | 202 | | return false; |
| | 203 | | } |
| | 204 | |
|
| 2 | 205 | | return true; |
| 6 | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Combines namespace parts with proper handling of empty namespaces |
| | 210 | | /// </summary> |
| | 211 | | /// <param name="namespacePart">First namespace part</param> |
| | 212 | | /// <param name="suffix">Second namespace part</param> |
| | 213 | | /// <returns>Combined namespace</returns> |
| | 214 | | private string CombineNamespaceParts(string namespacePart, string suffix) |
| 12 | 215 | | { |
| 12 | 216 | | return string.IsNullOrEmpty(namespacePart) ? suffix : $"{namespacePart}.{suffix}"; |
| 12 | 217 | | } |
| | 218 | | } |