csharpdeeb25a2 | 1 | using Tomlyn; |
| 2 | using Tomlyn.Model; |
| 3 | |
| 4 | namespace CascadeIDE.Services; |
| 5 | |
| 6 | /// <summary>Глубокий merge TOML-документов: <paramref name="overlay"/> перекрывает <paramref name="baseToml"/> по ключам таблиц.</summary> |
| 7 | internal static class TomlTableMerge |
| 8 | { |
| 9 | public static string MergeTomlDocuments(string baseToml, string overlayToml) |
| 10 | { |
| 11 | var baseTable = TomlSerializer.Deserialize<TomlTable>(baseToml, CascadeTomlSerializer.Options) |
| 12 | ?? throw new InvalidOperationException("Base TOML did not deserialize to TomlTable."); |
| 13 | var overlayTable = TomlSerializer.Deserialize<TomlTable>(overlayToml, CascadeTomlSerializer.Options) |
| 14 | ?? throw new InvalidOperationException("Overlay TOML did not deserialize to TomlTable."); |
| 15 | MergeInto(baseTable, overlayTable); |
| 16 | return TomlSerializer.Serialize(baseTable, CascadeTomlSerializer.Options); |
| 17 | } |
| 18 | |
| 19 | private static void MergeInto(TomlTable target, TomlTable overlay) |
| 20 | { |
| 21 | foreach (var (key, value) in overlay) |
| 22 | { |
| 23 | if (value is TomlTable overlayChild |
| 24 | && target.TryGetValue(key, out var existing) |
| 25 | && existing is TomlTable targetChild) |
| 26 | { |
| 27 | MergeInto(targetChild, overlayChild); |
| 28 | continue; |
| 29 | } |
| 30 | |
| 31 | target[key] = value; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
View only · write via MCP/CIDE