diff --git a/src/PAModel/PAConvert/Yaml/YamlConverter.cs b/src/PAModel/PAConvert/Yaml/YamlConverter.cs index 56e71e91..f452fdac 100644 --- a/src/PAModel/PAConvert/Yaml/YamlConverter.cs +++ b/src/PAModel/PAConvert/Yaml/YamlConverter.cs @@ -1,58 +1,53 @@ -using Microsoft.PowerPlatform.Formulas.Tools.Yaml; -using System; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -namespace Microsoft.PowerPlatform +namespace Microsoft.PowerPlatform.Formulas.Tools.Yaml; + +/// +/// Convert a dictionary to and from yaml +/// +public static class YamlConverter { - /// - /// Convert a dictionary to and from yaml - /// - public static class YamlConverter + public static Dictionary Read(TextReader reader, string filenameHint = null) { - public static Dictionary Read(TextReader reader, string filenameHint = null) - { - var properties = new Dictionary(StringComparer.Ordinal); + var properties = new Dictionary(StringComparer.Ordinal); - var yaml = new YamlLexer(reader, filenameHint); + var yaml = new YamlLexer(reader, filenameHint); - while (true) + while (true) + { + var t = yaml.ReadNext(); + if (t.Kind == YamlTokenKind.EndOfFile) { - var t = yaml.ReadNext(); - if (t.Kind == YamlTokenKind.EndOfFile) - { - break; - } - - if (t.Kind != YamlTokenKind.Property) - { - // $$$ error - t = YamlToken.NewError(t.Span, "Only properties are supported in this context"); - - } - if (t.Kind == YamlTokenKind.Error) - { - // ToString will include source span and message. - throw new InvalidOperationException(t.ToString()); - } - - properties[t.Property] = t.Value; + break; } - return properties; - } - public static void Write(TextWriter writer, IDictionary properties) - { - var yaml = new YamlWriter(writer); + if (t.Kind != YamlTokenKind.Property) + { + // $$$ error + t = YamlToken.NewError(t.Span, "Only properties are supported in this context"); - // Sort by keys to enforce canonical format. - foreach (var kv in properties.OrderBy(x => x.Key)) + } + if (t.Kind == YamlTokenKind.Error) { - yaml.WriteProperty(kv.Key, kv.Value); + // ToString will include source span and message. + throw new InvalidOperationException(t.ToString()); } - writer.Flush(); + + properties[t.Property] = t.Value; + } + return properties; + } + + public static void Write(TextWriter writer, IDictionary properties) + { + var yaml = new YamlWriter(writer); + + // Sort by keys to enforce canonical format. + foreach (var kv in properties.OrderBy(x => x.Key)) + { + yaml.WriteProperty(kv.Key, kv.Value); } + writer.Flush(); } } diff --git a/src/PAModelTests/PublicSurfaceTests.cs b/src/PAModelTests/PublicSurfaceTests.cs index 15a453c7..106c77b2 100644 --- a/src/PAModelTests/PublicSurfaceTests.cs +++ b/src/PAModelTests/PublicSurfaceTests.cs @@ -3,51 +3,49 @@ using Microsoft.PowerPlatform.Formulas.Tools; using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; using System.Linq; using System.Text; -namespace PAModelTests +namespace PAModelTests; + +[TestClass] +public class PublicSurfaceTests { - [TestClass] - public class PublicSurfaceTests + [TestMethod] + public void Test() { - [TestMethod] - public void Test() - { - var asm = typeof(CanvasDocument).Assembly; + var asm = typeof(CanvasDocument).Assembly; - var ns = "Microsoft.PowerPlatform.Formulas.Tools"; - HashSet allowed = new HashSet() - { - $"{ns}.{nameof(CanvasDocument)}", - $"{ns}.{nameof(CanvasMerger)}", - $"{ns}.{nameof(ChecksumMaker)}", - $"{ns}.{nameof(ErrorContainer)}", - $"{ns}.{nameof(Error)}", - $"Microsoft.PowerPlatform.YamlConverter", - $"Microsoft.PowerPlatform.YamlPocoSerializer", - $"Microsoft.PowerPlatform.Formulas.Tools.Yaml.YamlWriter", - }; - - StringBuilder sb = new StringBuilder(); - foreach (var type in asm.GetTypes().Where(t => t.IsPublic)) + var ns = "Microsoft.PowerPlatform.Formulas.Tools"; + var allowed = new HashSet() + { + $"{ns}.{nameof(CanvasDocument)}", + $"{ns}.{nameof(CanvasMerger)}", + $"{ns}.{nameof(ChecksumMaker)}", + $"{ns}.{nameof(ErrorContainer)}", + $"{ns}.{nameof(Error)}", + $"{ns}.Yaml.YamlConverter", + $"{ns}.Yaml.YamlPocoSerializer", + $"{ns}.Yaml.YamlWriter", + }; + + var sb = new StringBuilder(); + foreach (var type in asm.GetTypes().Where(t => t.IsPublic)) + { + var name = type.FullName; + if (!allowed.Contains(name)) { - var name = type.FullName; - if (!allowed.Contains(name)) - { - sb.Append(name); - sb.Append("; "); - } - - allowed.Remove(name); + sb.Append(name); + sb.Append("; "); } - Assert.AreEqual(0, sb.Length, $"Unexpected public types: {sb}"); - - // Types we expect to be in the assembly aren't there. - Assert.AreEqual(0, allowed.Count); + allowed.Remove(name); } + + Assert.AreEqual(0, sb.Length, $"Unexpected public types: {sb}"); + + // Types we expect to be in the assembly aren't there. + Assert.AreEqual(0, allowed.Count); } } diff --git a/src/PAModelTests/YamlTest.cs b/src/PAModelTests/YamlTest.cs index 609fc792..7db032f5 100644 --- a/src/PAModelTests/YamlTest.cs +++ b/src/PAModelTests/YamlTest.cs @@ -460,7 +460,7 @@ public void ReadDict() { var reader = new StringReader(expectedYaml); - var props = Microsoft.PowerPlatform.YamlConverter.Read(reader); + var props = YamlConverter.Read(reader); Assert.AreEqual(props.Count, 2); Assert.AreEqual(props["P1"], "123"); Assert.AreEqual(props["P2"], " \"hello\" & \"world\""); @@ -475,7 +475,7 @@ public void ReadDictError() sub1: 123"); // error, not supported objects Assert.ThrowsException( - () => Microsoft.PowerPlatform.YamlConverter.Read(reader)); + () => YamlConverter.Read(reader)); } [TestMethod] @@ -489,7 +489,7 @@ public void WriteDict() {"P1", "123" } }; - Microsoft.PowerPlatform.YamlConverter.Write(writer, d); + YamlConverter.Write(writer, d); // order should be alphabetical Assert.AreEqual(expectedYaml, writer.ToString());