-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathProprietableBlockExtensions.cs
61 lines (58 loc) · 5.11 KB
/
ProprietableBlockExtensions.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Kiota.Builder.CodeDOM;
namespace Kiota.Builder.Writers;
internal static class ProprietableBlockExtensions
{
private static readonly Func<CodeProperty, bool> isCustomProperty = x => x.Kind is CodePropertyKind.Custom;
private static readonly Func<CodeProperty, bool> isPrimaryErrorMessage = x => isCustomProperty(x) && x.IsPrimaryErrorMessage;
private static readonly Func<CodeMethod, bool> isGetterMethod = x => x.Kind is CodeMethodKind.Getter;
internal static string GetPrimaryMessageCodePath<TBlockKind, TBlockDeclaration>(this ProprietableBlock<TBlockKind, TBlockDeclaration> block,
Func<CodeProperty, string> propertyNameNormalization,
Func<CodeMethod, string> methodNameNormalization,
string pathSegment = ".",
HashSet<CodeElement>? visitedElements = default) where TBlockKind : Enum where TBlockDeclaration : ProprietableBlockDeclaration, new()
{
visitedElements ??= new();
if (visitedElements.Contains(block))
return string.Empty;
else
visitedElements.Add(block);
if (block is CodeInterface currentInterface)
{
if (currentInterface.Methods.FirstOrDefault(static x => isGetterMethod(x) && x.AccessedProperty is not null && isPrimaryErrorMessage(x.AccessedProperty)) is CodeMethod primaryErrorMessageMethod)
return methodNameNormalization(primaryErrorMessageMethod);
else if (currentInterface.Properties.FirstOrDefault(isPrimaryErrorMessage) is CodeProperty primaryErrorMessageProperty)
return propertyNameNormalization(primaryErrorMessageProperty);
else if (currentInterface.Methods
.Where(isGetterMethod)
.Select(x => new { Value = x.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface && codeInterface.GetPrimaryMessageCodePath(propertyNameNormalization, methodNameNormalization, pathSegment, visitedElements) is string segment && !string.IsNullOrEmpty(segment) ? $"{methodNameNormalization(x)}{pathSegment}{segment}" : string.Empty, IsMethod = true })
.Union(currentInterface.Properties
.Where(isCustomProperty)
.Select(x => new { Value = x.Type is CodeType codeType && codeType.TypeDefinition is CodeInterface codeInterface && codeInterface.GetPrimaryMessageCodePath(propertyNameNormalization, methodNameNormalization, pathSegment, visitedElements) is string segment && !string.IsNullOrEmpty(segment) ? $"{propertyNameNormalization(x)}{pathSegment}{segment}" : string.Empty, IsMethod = false }))
.OrderBy(static x => x.IsMethod)
.ThenBy(static x => x.Value, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Value)) is { } primaryMessageCodePath)
return primaryMessageCodePath.Value;
}
else if (block is CodeClass currentClass)
{
if (currentClass.Methods.FirstOrDefault(static x => isGetterMethod(x) && x.AccessedProperty is not null && isPrimaryErrorMessage(x.AccessedProperty)) is CodeMethod primaryErrorMessageMethod)
return methodNameNormalization(primaryErrorMessageMethod);
else if (currentClass.Properties.FirstOrDefault(isPrimaryErrorMessage) is CodeProperty primaryErrorMessageProperty)
return propertyNameNormalization(primaryErrorMessageProperty);
else if (currentClass.Methods
.Where(isGetterMethod)
.Select(x => new { Value = x.ReturnType is CodeType codeType && codeType.TypeDefinition is CodeClass codeClass && codeClass.GetPrimaryMessageCodePath(propertyNameNormalization, methodNameNormalization, pathSegment, visitedElements) is string segment && !string.IsNullOrEmpty(segment) ? $"{methodNameNormalization(x)}{pathSegment}{segment}" : string.Empty, IsMethod = true })
.Union(currentClass.Properties
.Where(isCustomProperty)
.Select(x => new { Value = x.Type is CodeType codeType && codeType.TypeDefinition is CodeClass codeClass && codeClass.GetPrimaryMessageCodePath(propertyNameNormalization, methodNameNormalization, pathSegment, visitedElements) is string segment && !string.IsNullOrEmpty(segment) ? $"{propertyNameNormalization(x)}{pathSegment}{segment}" : string.Empty, IsMethod = false }))
.OrderBy(static x => x.IsMethod)
.ThenBy(static x => x.Value, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Value)) is { } primaryMessageCodePath)
return primaryMessageCodePath.Value;
}
return string.Empty;
}
}