-
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.
added method to detect if a yaml fragment is a sequence (#720)
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 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
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
42 changes: 42 additions & 0 deletions
42
src/Persistence/PaYaml/Serialization/YamlSequenceTesterConverter.cs
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Core.Events; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Microsoft.PowerPlatform.PowerApps.Persistence.PaYaml.Serialization; | ||
|
||
/// <summary> | ||
/// custom converter used to test if the input is a sequence of YAML items. | ||
/// </summary> | ||
internal sealed class YamlSequenceTesterConverter : IYamlTypeConverter | ||
{ | ||
public bool Accepts(Type type) | ||
{ | ||
return type == typeof(IEnumerable) || type.IsSubclassOf(typeof(IEnumerable)) || | ||
type == typeof(IEnumerable<object>) || type.IsSubclassOf(typeof(IEnumerable<object>)) || | ||
type == typeof(object[]); | ||
} | ||
|
||
public object? ReadYaml(IParser parser, Type type) | ||
{ | ||
if (parser.Current is not SequenceStart) | ||
throw new YamlException(parser.Current!.Start, parser.Current.End, $"Expected sequence start but got {parser.Current.GetType().Name}"); | ||
|
||
while (!parser.Accept<SequenceEnd>(out _)) | ||
{ | ||
parser.MoveNext(); | ||
} | ||
|
||
parser.MoveNext(); | ||
|
||
return Array.Empty<object>(); | ||
} | ||
|
||
public void WriteYaml(IEmitter emitter, object? value, Type type) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |