|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using KustoSchemaTools.Model; |
| 5 | + |
| 6 | +namespace KustoSchemaTools.Changes |
| 7 | +{ |
| 8 | + public static class StructuredChangeExtensions |
| 9 | + { |
| 10 | + public static StructuredChange ToStructuredChange(this IChange change) |
| 11 | + { |
| 12 | + if (change == null) |
| 13 | + { |
| 14 | + throw new ArgumentNullException(nameof(change)); |
| 15 | + } |
| 16 | + |
| 17 | + var structuredChange = new StructuredChange |
| 18 | + { |
| 19 | + EntityType = change.EntityType, |
| 20 | + Entity = change.Entity, |
| 21 | + Scripts = change.Scripts?.Select(CloneScript).ToList() ?? new List<DatabaseScriptContainer>(), |
| 22 | + Comment = StructuredComment.From(change.Comment) |
| 23 | + }; |
| 24 | + |
| 25 | + switch (change) |
| 26 | + { |
| 27 | + case Heading heading: |
| 28 | + structuredChange.ChangeType = "Heading"; |
| 29 | + structuredChange.HeadingText = heading.Entity; |
| 30 | + structuredChange.Scripts.Clear(); |
| 31 | + break; |
| 32 | + case DeletionChange deletion: |
| 33 | + structuredChange.ChangeType = "Delete"; |
| 34 | + structuredChange.DeletedEntities = new List<string> { deletion.Entity }; |
| 35 | + break; |
| 36 | + case ScriptCompareChange scriptCompare: |
| 37 | + structuredChange.ChangeType = scriptCompare.From == null ? "Create" : "Update"; |
| 38 | + structuredChange.ScriptComparison = scriptCompare.ToStructuredScriptComparison(); |
| 39 | + break; |
| 40 | + default: |
| 41 | + structuredChange.ChangeType = "Update"; |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + structuredChange.DeletedEntities ??= new List<string>(); |
| 46 | + |
| 47 | + return structuredChange; |
| 48 | + } |
| 49 | + |
| 50 | + private static StructuredScriptComparison? ToStructuredScriptComparison(this ScriptCompareChange change) |
| 51 | + { |
| 52 | + var comparison = new StructuredScriptComparison |
| 53 | + { |
| 54 | + NewScripts = change.Scripts?.Select(CloneScript).ToList() ?? new List<DatabaseScriptContainer>() |
| 55 | + }; |
| 56 | + |
| 57 | + if (change.From != null) |
| 58 | + { |
| 59 | + var previousScripts = change.From |
| 60 | + .CreateScripts(change.Entity, false) |
| 61 | + .GroupBy(script => script.Kind) |
| 62 | + .Select(group => group.First()) |
| 63 | + .ToDictionary(script => script.Kind, script => script); |
| 64 | + |
| 65 | + foreach (var script in comparison.NewScripts) |
| 66 | + { |
| 67 | + if (previousScripts.TryGetValue(script.Kind, out var previous)) |
| 68 | + { |
| 69 | + comparison.OldScripts.Add(CloneScript(previous)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + foreach (var script in comparison.OldScripts.Where(s => !s.IsValid.HasValue)) |
| 75 | + { |
| 76 | + script.IsValid = true; |
| 77 | + } |
| 78 | + |
| 79 | + return comparison; |
| 80 | + } |
| 81 | + |
| 82 | + private static DatabaseScriptContainer CloneScript(DatabaseScriptContainer source) |
| 83 | + { |
| 84 | + var clone = new DatabaseScriptContainer(new DatabaseScript(source.Script.Text, source.Script.Order), source.Kind, source.IsAsync) |
| 85 | + { |
| 86 | + IsValid = source.IsValid |
| 87 | + }; |
| 88 | + |
| 89 | + return clone; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments