-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathUtilites.cs
139 lines (110 loc) · 4.78 KB
/
Utilites.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Collections;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Tomlyn.Model;
namespace Imageflow.Server.Configuration.Utilities;
internal partial class Utilities{
internal static void ToStringRecursive(object obj, StringBuilder sb, string indent)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
object? value = property.GetValue(obj);
string valueString = value?.ToString() ?? "null";
sb.AppendLine($"{indent}{property.Name}: {valueString}");
if (value != null && !value.GetType().IsPrimitive)
{
ToStringRecursive(value, sb, indent + " ");
}
}
}
/// <summary>
/// Only recurses types from Imageflow namespace (and collections), everything else is ToString
/// </summary>
/// <param name="obj"></param>
/// <param name="dict"></param>
/// <param name="parentPath"></param>
internal static void AddToDictionaryRecursive(object? obj, Dictionary<string,string> dict, string parentPath = ""){
if (obj is TomlPropertiesMetadata){
return; // Skip that property - not that this is used with raw configs anyway
}
if (obj is IDictionary dictionary){
foreach (var key in dictionary.Keys){
AddToDictionaryRecursive(dictionary[key], dict, parentPath + $"[{key}]");
}
return;
}
if (obj is ICollection collection){
int i = 0;
foreach (var item in collection){
AddToDictionaryRecursive(item, dict, parentPath + $"[{i++}].");
}
return;
}
Type? type = obj?.GetType();
if (type?.Namespace?.StartsWith("Imageflow") == true){
var parentDot = parentPath.Length > 0 ? parentPath + "." : "";
PropertyInfo[] properties = type.GetProperties( BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo property in properties)
{
object? value = property.GetValue(obj);
AddToDictionaryRecursive(value, dict, parentDot + property.Name );
}
PropertyInfo[] fields = type.GetProperties( BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (PropertyInfo field in fields)
{
// exclude names matching properties, case insensitive
if (properties.Any(p => p.Name.Equals(field.Name, StringComparison.OrdinalIgnoreCase))) continue;
object? value = field.GetValue(obj);
AddToDictionaryRecursive(value, dict, parentDot + field.Name );
}
}else{
// leaf nodes
if (dict.ContainsKey(parentPath)){
AddToDictionaryRecursive(obj, dict, parentPath + "`");
}else{
dict.Add(parentPath, obj?.ToString() ?? "null");
}
}
}
/// Escape strings using C# rules, permit """ if escapes required. Includes quotes
internal static string EscapeStringCSharp(string value){
var hasNewlines = value.Contains('\n');
if (!hasNewlines && !invalidForDoubleQuoteString.IsMatch(value)){
return "\"" + value + "\"";
}
if (!hasNewlines && !invalidForAtLiteral.IsMatch(value)){
return "@" + "\"" + value + "\"";
}
// Use triple quotes
if (!value.Contains("\"\"\"")){
return "\"\"\"" + value + "\"\"\"";
}
if (!value.Contains("\"\"\"\"")){
return "\"\"\"\"" + value + "\"\"\"\"";
}
throw new NotImplementedException("Haven't implemented escaping for 5 quotes yet");
}
/// <summary>
/// Returns a string of the given dictionary in C# syntax
/// </summary>
/// <param name="dict"></param>
/// <returns></returns>
internal static string DictionaryToCSharp(string varName, Dictionary<string,string> dict){
var sb = new StringBuilder();
sb.AppendLine($"Dictionary<string,string> {varName} = new (){{");
foreach (var kvp in dict){
sb.AppendLine($" {{{EscapeStringCSharp(kvp.Key)}, {EscapeStringCSharp(kvp.Value)}}},");
}
sb.AppendLine("};");
return sb.ToString();
}
static Regex invalidForDoubleQuoteString = InvalidForDoubleQuoteString();
[GeneratedRegex("[\0-\b\v\f\u000e-\u001f\\\\\"]")]
private static partial Regex InvalidForDoubleQuoteString();
static Regex invalidForAtLiteral = InvalidForAtLiteral();
[GeneratedRegex("\"(?!\")")]
private static partial Regex InvalidForAtLiteral();
}