-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Convert a dictionary to and from yaml | ||
/// </summary> | ||
public static class YamlConverter | ||
{ | ||
/// <summary> | ||
/// Convert a dictionary to and from yaml | ||
/// </summary> | ||
public static class YamlConverter | ||
public static Dictionary<string, string> Read(TextReader reader, string filenameHint = null) | ||
{ | ||
public static Dictionary<string, string> Read(TextReader reader, string filenameHint = null) | ||
{ | ||
var properties = new Dictionary<string, string>(StringComparer.Ordinal); | ||
var properties = new Dictionary<string, string>(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<string, string> 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<string, string> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters