-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYamlConfigParser.cs
More file actions
205 lines (171 loc) · 6.59 KB
/
YamlConfigParser.cs
File metadata and controls
205 lines (171 loc) · 6.59 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConfigMerger.Parsers
{
public interface IConfigParser
{
Dictionary<string, string> ParseConfig(string content);
string GenerateMergedConfig(Dictionary<string, string> merged, string originalContent);
}
public class YamlConfigParser : IConfigParser
{
public Dictionary<string, string> ParseConfig(string content)
{
var config = new Dictionary<string, string>();
var lines = content.Split('\n');
var currentPath = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
var trimmedLine = line.Trim();
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("#"))
continue;
var indentLevel = GetIndentLevel(line);
if (trimmedLine.Contains(":"))
{
var parts = trimmedLine.Split(new[] { ':' }, 2);
var key = parts[0].Trim();
var value = parts.Length > 1 ? parts[1].Trim() : "";
UpdateCurrentPath(currentPath, indentLevel, key);
if (!string.IsNullOrEmpty(value))
{
var fullKey = string.Join(".", currentPath);
config[fullKey] = ProcessValue(value);
}
else
{
var multiLineValue = ProcessMultiLineValue(lines, ref i, indentLevel);
if (!string.IsNullOrEmpty(multiLineValue))
{
var fullKey = string.Join(".", currentPath);
config[fullKey] = multiLineValue;
}
}
}
else if (trimmedLine.StartsWith("-"))
{
var arrayValue = trimmedLine.Substring(1).Trim();
var arrayIndex = GetArrayIndex(config, currentPath);
var fullKey = string.Join(".", currentPath) + $"[{arrayIndex}]";
config[fullKey] = ProcessValue(arrayValue);
}
}
return config;
}
private int GetIndentLevel(string line)
{
int spaces = 0;
foreach (char c in line)
{
if (c == ' ')
spaces++;
else if (c == '\t')
spaces += 4;
else
break;
}
return spaces / 2;
}
private void UpdateCurrentPath(List<string> currentPath, int indentLevel, string key)
{
while (currentPath.Count > indentLevel)
{
currentPath.RemoveAt(currentPath.Count - 1);
}
currentPath.Add(key);
}
private string ProcessValue(string value)
{
if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
(value.StartsWith("'") && value.EndsWith("'")))
{
return value.Substring(1, value.Length - 2);
}
return value;
}
private string ProcessMultiLineValue(string[] lines, ref int currentIndex, int baseIndentLevel)
{
var multiLineValue = "";
var nextLineIndex = currentIndex + 1;
while (nextLineIndex < lines.Length)
{
var nextLine = lines[nextLineIndex];
var nextTrimmed = nextLine.Trim();
if (string.IsNullOrEmpty(nextTrimmed) || nextTrimmed.StartsWith("#"))
{
nextLineIndex++;
continue;
}
var nextIndentLevel = GetIndentLevel(nextLine);
if (nextIndentLevel <= baseIndentLevel)
break;
if (!string.IsNullOrEmpty(multiLineValue))
multiLineValue += "\n";
multiLineValue += nextTrimmed;
nextLineIndex++;
}
currentIndex = nextLineIndex - 1;
return multiLineValue;
}
private int GetArrayIndex(Dictionary<string, string> config, List<string> currentPath)
{
var basePath = string.Join(".", currentPath);
return config.Keys.Count(k => k.StartsWith(basePath + "["));
}
public string GenerateMergedConfig(Dictionary<string, string> merged, string originalContent)
{
var result = new List<string>();
var processedPaths = new HashSet<string>();
var groupedKeys = GroupKeysByLevel(merged.Keys);
foreach (var levelGroup in groupedKeys.OrderBy(g => g.Key))
{
foreach (var key in levelGroup.Value.OrderBy(k => k))
{
if (!processedPaths.Contains(key))
{
GenerateYamlLine(key, merged[key], result, 0);
processedPaths.Add(key);
}
}
}
return string.Join("\n", result);
}
private Dictionary<int, List<string>> GroupKeysByLevel(IEnumerable<string> keys)
{
var grouped = new Dictionary<int, List<string>>();
foreach (var key in keys)
{
var level = key.Count(c => c == '.');
if (!grouped.ContainsKey(level))
grouped[level] = new List<string>();
grouped[level].Add(key);
}
return grouped;
}
private void GenerateYamlLine(string key, string value, List<string> result, int indentLevel)
{
var indent = new string(' ', indentLevel * 2);
var keyParts = key.Split('.');
var currentKey = keyParts.Last();
if (currentKey.Contains("[") && currentKey.Contains("]"))
{
result.Add($"{indent}- {value}");
}
else
{
var formattedValue = FormatYamlValue(value);
result.Add($"{indent}{currentKey}: {formattedValue}");
}
}
private string FormatYamlValue(string value)
{
if (value.Contains(":") || value.Contains("#") || value.Contains("'") ||
value.Contains("\"") || value.Contains("\n"))
{
return $"\"{value.Replace("\"", "\\\"")}\"";
}
return value;
}
}
}