Skip to content

Commit

Permalink
Bump YamlDotNet from 8.1.2 to 10.0.0
Browse files Browse the repository at this point in the history
Bumps [YamlDotNet](https://github.com/aaubry/YamlDotNet) from 8.1.2 to 10.0.0.
- [Release notes](https://github.com/aaubry/YamlDotNet/releases)
- [Commits](aaubry/YamlDotNet@v8.1.2...v10.0.0)

Signed-off-by: dependabot[bot] <support@github.com>
  • Loading branch information
dependabot[bot] authored and mergify[bot] committed Mar 29, 2021
1 parent 7f95f38 commit e1c4778
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageVersion_LibGit2Sharp_NativeBinaries>2.0.312</PackageVersion_LibGit2Sharp_NativeBinaries>

<PackageVersion_JetBrainsAnnotations>2020.3.0</PackageVersion_JetBrainsAnnotations>
<PackageVersion_YamlDotNet>8.1.2</PackageVersion_YamlDotNet>
<PackageVersion_YamlDotNet>10.0.0</PackageVersion_YamlDotNet>
<PackageVersion_MicrosoftExtensions>5.0.0</PackageVersion_MicrosoftExtensions>
<PackageVersion_MicrosoftJson>5.0.0</PackageVersion_MicrosoftJson>
<PackageVersion_MicrosoftTextEncoding>5.0.0</PackageVersion_MicrosoftTextEncoding>
Expand Down
66 changes: 66 additions & 0 deletions src/GitVersion.Core/Configuration/ConfigSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.IO;
using GitVersion.Model.Configuration;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

Expand All @@ -11,6 +14,7 @@ public static Config Read(TextReader reader)
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(HyphenatedNamingConvention.Instance)
.WithTypeConverter(new YamlNullableEnumTypeConverter())
.Build();
var deserialize = deserializer.Deserialize<Config>(reader);
return deserialize ?? new Config();
Expand All @@ -21,8 +25,70 @@ public static void Write(Config config, TextWriter writer)
var serializer = new SerializerBuilder()
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
.WithNamingConvention(HyphenatedNamingConvention.Instance)
.WithTypeConverter(new YamlNullableEnumTypeConverter())
.Build();
serializer.Serialize(writer, config);
}
}

public class YamlNullableEnumTypeConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return Nullable.GetUnderlyingType(type)?.IsEnum ?? false;
}

public object ReadYaml(IParser parser, Type type)
{
type = Nullable.GetUnderlyingType(type) ?? throw new ArgumentException("Expected nullable enum type for ReadYaml");

if (parser.Accept<NodeEvent>(out var @event))
{
if (NodeIsNull(@event))
{
parser.SkipThisAndNestedEvents();
return null;
}
}

var scalar = parser.Consume<Scalar>();
try
{
return Enum.Parse(type, scalar.Value, ignoreCase: true);
}
catch (Exception ex)
{
throw new Exception($"Invalid value: \"{scalar.Value}\" for {type.Name}", ex);
}
}

public void WriteYaml(IEmitter emitter, object value, Type type)
{
type = Nullable.GetUnderlyingType(type) ?? throw new ArgumentException("Expected nullable enum type for WriteYaml");

if (value != null)
{
var toWrite = Enum.GetName(type, value) ?? throw new InvalidOperationException($"Invalid value {value} for enum: {type}");
emitter.Emit(new Scalar(null, null, toWrite, ScalarStyle.Any, true, false));
}
}

private static bool NodeIsNull(NodeEvent nodeEvent)
{
// http://yaml.org/type/null.html

if (nodeEvent.Tag == "tag:yaml.org,2002:null")
{
return true;
}

if (nodeEvent is Scalar scalar && scalar.Style == ScalarStyle.Plain)
{
var value = scalar.Value;
return value is "" or "~" or "null" or "Null" or "NULL";
}

return false;
}
}
}

0 comments on commit e1c4778

Please sign in to comment.