-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProxyClassMapper.cs
More file actions
81 lines (69 loc) · 2.92 KB
/
ProxyClassMapper.cs
File metadata and controls
81 lines (69 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using DataverseProxyGenerator.Core.Domain;
using DataverseProxyGenerator.Core.Generation.Utilities;
namespace DataverseProxyGenerator.Core.Generation.Mappers;
public static class ProxyClassMapper
{
/// <summary>
/// The list is limited to properties where collisions have been identified.
/// </summary>
private static readonly HashSet<string> RestrictedAttributeNames = new(StringComparer.Ordinal)
{
"Attributes", // Collision on SdkMessageProcessingStepImage
};
public static object MapToTemplateModel((TableModel Table, IReadOnlyList<string> Interfaces) input, GenerationContext context)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(input.Table);
var (table, interfaces) = input;
var processedColumns = ProcessColumnsWithNameConflictResolution(table.Columns, table.SchemaName);
return new
{
SchemaName = table.SchemaName,
Columns = processedColumns,
Relationships = table.Relationships.Select(r => r with
{
SchemaName = GenerationUtilities.SanitizeName(r.SchemaName),
}),
Keys = table.Keys,
LogicalName = table.LogicalName,
DisplayName = table.DisplayName,
Description = table.Description,
EntityTypeCode = table.EntityTypeCode,
PrimaryNameAttribute = table.PrimaryNameAttribute,
PrimaryIdAttribute = table.PrimaryIdAttribute,
IsIntersect = table.IsIntersect,
InterfacesList = interfaces ?? new List<string>(),
};
}
private static IEnumerable<ColumnModel> ProcessColumnsWithNameConflictResolution(IEnumerable<ColumnModel> columns, string className)
{
var usedNames = new HashSet<string>(RestrictedAttributeNames, StringComparer.Ordinal);
usedNames.Add(className);
return columns.Select(c =>
{
var sanitizedColumn = c switch
{
EnumColumnModel enumCol => enumCol with
{
SchemaName = GenerationUtilities.SanitizeName(enumCol.SchemaName),
OptionsetName = GenerationUtilities.SanitizeName(enumCol.OptionsetName),
},
_ => c with
{
SchemaName = GenerationUtilities.SanitizeName(c.SchemaName),
},
};
var defaultName = sanitizedColumn.SchemaName;
// Ensure the final name is unique (handle edge case where _1 suffix also conflicts)
var candidateName = defaultName;
var suffix = 0;
while (usedNames.Contains(candidateName))
{
suffix++;
candidateName = $"{defaultName}_{suffix}";
}
usedNames.Add(candidateName);
return sanitizedColumn with { SchemaName = candidateName };
});
}
}