Skip to content

Commit

Permalink
Merge pull request #897 from EdwardCooke/ec-net80-enumsasnumbers
Browse files Browse the repository at this point in the history
Enables #773 

Adds naming conventions for convert enums to/from scalars
Adds .NET8 binaries
Adds support for custom formatting of enums, easy to expose enums as integers or whatever else.
Quotes necessary enum strings (Null for example)

Breaking: For those that get impacted pass in NullNamingConvention.Instance to the EnumNamingConvetion arguments on the constructor
Breaking: Removed many of the redundant constructors for the classes, pass in the old default values to the new constructors

+semver:breaking
  • Loading branch information
EdwardCooke authored Jan 22, 2024
2 parents 6f8323e + dfb165e commit 88a9189
Show file tree
Hide file tree
Showing 35 changed files with 348 additions and 105 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS builder
RUN apt update && \
apt install -y \
apt-transport-https \
Expand All @@ -16,7 +16,7 @@ RUN apt install -y wget
RUN wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt update
RUN apt install -y dotnet-sdk-3.1 dotnet-sdk-6.0
RUN apt install -y dotnet-sdk-6.0

FROM builder AS build
WORKDIR /src
Expand All @@ -30,6 +30,8 @@ COPY YamlDotNet.Samples/YamlDotNet.Samples.csproj YamlDotNet.Samples/YamlDotNet.
COPY YamlDotNet.Test/YamlDotNet.Test.csproj YamlDotNet.Test/YamlDotNet.Test.csproj
COPY YamlDotNet.Analyzers.StaticGenerator/YamlDotNet.Analyzers.StaticGenerator.csproj YamlDotNet.Analyzers.StaticGenerator/YamlDotNet.Analyzers.StaticGenerator.csproj
COPY YamlDotNet.Core7AoTCompileTest/YamlDotNet.Core7AoTCompileTest.csproj YamlDotNet.Core7AoTCompileTest/YamlDotNet.Core7AoTCompileTest.csproj
COPY YamlDotNet.Core7AoTCompileTest.Model/YamlDotNet.Core7AoTCompileTest.Model.csproj YamlDotNet.Core7AoTCompileTest.Model/YamlDotNet.Core7AoTCompileTest.Model.csproj
COPY YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj

RUN dotnet restore YamlDotNet.sln

Expand All @@ -43,12 +45,13 @@ RUN dotnet build -c Release --framework netstandard2.0 YamlDotNet/YamlDotNet.csp
RUN dotnet build -c Release --framework netstandard2.1 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.1
RUN dotnet build -c Release --framework net60 YamlDotNet/YamlDotNet.csproj -o /output/net60
RUN dotnet build -c Release --framework net70 YamlDotNet/YamlDotNet.csproj -o /output/net70
RUN dotnet build -c Release --framework net80 YamlDotNet/YamlDotNet.csproj -o /output/net80

RUN dotnet pack -c Release YamlDotNet/YamlDotNet.csproj -o /output/package /p:Version=$PACKAGE_VERSION

RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net80 --logger:"trx;LogFileName=/output/tests.net80.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net70 --logger:"trx;LogFileName=/output/tests.net70.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net60 --logger:"trx;LogFileName=/output/tests.net60.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework netcoreapp3.1 --logger:"trx;LogFileName=/output/tests.netcoreapp3.1.trx" --logger:"console;Verbosity=detailed"

FROM alpine
VOLUME /output
Expand Down
8 changes: 6 additions & 2 deletions Dockerfile.NonEnglish
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS builder
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS builder
RUN apt update && \
apt install -y \
apt-transport-https \
Expand All @@ -17,7 +17,7 @@ RUN apt install -y wget
RUN wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt update
RUN apt install -y dotnet-sdk-3.1 dotnet-sdk-6.0
RUN apt install -y dotnet-sdk-6.0


FROM builder AS build
Expand All @@ -32,6 +32,8 @@ COPY YamlDotNet.Samples/YamlDotNet.Samples.csproj YamlDotNet.Samples/YamlDotNet.
COPY YamlDotNet.Test/YamlDotNet.Test.csproj YamlDotNet.Test/YamlDotNet.Test.csproj
COPY YamlDotNet.Analyzers.StaticGenerator/YamlDotNet.Analyzers.StaticGenerator.csproj YamlDotNet.Analyzers.StaticGenerator/YamlDotNet.Analyzers.StaticGenerator.csproj
COPY YamlDotNet.Core7AoTCompileTest/YamlDotNet.Core7AoTCompileTest.csproj YamlDotNet.Core7AoTCompileTest/YamlDotNet.Core7AoTCompileTest.csproj
COPY YamlDotNet.Core7AoTCompileTest.Model/YamlDotNet.Core7AoTCompileTest.Model.csproj YamlDotNet.Core7AoTCompileTest.Model/YamlDotNet.Core7AoTCompileTest.Model.csproj
COPY YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.csproj YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.csproj

RUN dotnet restore YamlDotNet.sln

Expand All @@ -45,6 +47,7 @@ RUN dotnet build -c Release --framework netstandard2.0 YamlDotNet/YamlDotNet.csp
RUN dotnet build -c Release --framework netstandard2.1 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.1
RUN dotnet build -c Release --framework net60 YamlDotNet/YamlDotNet.csproj -o /output/net60
RUN dotnet build -c Release --framework net70 YamlDotNet/YamlDotNet.csproj -o /output/net70
RUN dotnet build -c Release --framework net80 YamlDotNet/YamlDotNet.csproj -o /output/net80

RUN dotnet pack -c Release YamlDotNet/YamlDotNet.csproj -o /output/package /p:Version=$PACKAGE_VERSION

Expand All @@ -62,6 +65,7 @@ RUN echo -n "${LC_ALL}" > /etc/locale.gen && \
apt install -y locales && \
locale-gen

RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net80 --logger:"trx;LogFileName=/output/tests.net80.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net70 --logger:"trx;LogFileName=/output/tests.net70.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net60 --logger:"trx;LogFileName=/output/tests.net60.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework netcoreapp3.1 --logger:"trx;LogFileName=/output/tests.netcoreapp3.1.trx" --logger:"console;Verbosity=detailed"
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net80</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet.Samples/YamlDotNet.Samples.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net60</TargetFramework>
<TargetFramework>net80</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
21 changes: 21 additions & 0 deletions YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

using System.Collections.Generic;
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.Callbacks;
using YamlDotNet.Serialization.NamingConventions;

namespace YamlDotNet.Test.Analyzers.StaticGenerator
{
Expand Down Expand Up @@ -120,6 +122,25 @@ public void CallbacksAreExecuted()
Assert.Equal(1, test.OnSerializingCallCount);
}

[Fact]
public void NamingConventionAppliedToEnum()
{
var serializer = new StaticSerializerBuilder(new StaticContext()).WithEnumNamingConvention(CamelCaseNamingConvention.Instance).Build();
ScalarStyle style = ScalarStyle.Plain;
var serialized = serializer.Serialize(style);
Assert.Equal("plain", serialized.TrimNewLines());
}

[Fact]
public void NamingConventionAppliedToEnumWhenDeserializing()
{
var serializer = new StaticDeserializerBuilder(new StaticContext()).WithEnumNamingConvention(UnderscoredNamingConvention.Instance).Build();
var yaml = "Double_Quoted";
ScalarStyle expected = ScalarStyle.DoubleQuoted;
var actual = serializer.Deserialize<ScalarStyle>(yaml);
Assert.Equal(expected, actual);
}

[YamlSerializable]
public class TestState
{
Expand Down
48 changes: 48 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using System.Text.RegularExpressions;
using FakeItEasy;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
Expand Down Expand Up @@ -2407,6 +2408,53 @@ public void SerializeStateMethodsGetCalledOnce()
Assert.Equal(1, test.OnSerializingCallCount);
}

[Fact]
public void SerializeEnumAsNumber()
{
var serializer = new SerializerBuilder().WithYamlFormatter(new YamlFormatter
{
FormatEnum = (o, namingConvention) => ((int)o).ToString(),
PotentiallyQuoteEnums = (_) => false
}).Build();
var deserializer = DeserializerBuilder.Build();

var value = serializer.Serialize(TestEnumAsNumber.Test1);
Assert.Equal("1", value.TrimNewLines());
var v = deserializer.Deserialize<TestEnumAsNumber>(value);
Assert.Equal(TestEnumAsNumber.Test1, v);

value = serializer.Serialize(TestEnumAsNumber.Test1 | TestEnumAsNumber.Test2);
Assert.Equal("3", value.TrimNewLines());
v = deserializer.Deserialize<TestEnumAsNumber>(value);
Assert.Equal(TestEnumAsNumber.Test1 | TestEnumAsNumber.Test2, v);
}

[Flags]
private enum TestEnumAsNumber
{
Test1 = 1,
Test2 = 2
}

[Fact]
public void NamingConventionAppliedToEnum()
{
var serializer = new SerializerBuilder().WithEnumNamingConvention(CamelCaseNamingConvention.Instance).Build();
ScalarStyle style = ScalarStyle.Plain;
var serialized = serializer.Serialize(style);
Assert.Equal("plain", serialized.RemoveNewLines());
}

[Fact]
public void NamingConventionAppliedToEnumWhenDeserializing()
{
var serializer = new DeserializerBuilder().WithEnumNamingConvention(UnderscoredNamingConvention.Instance).Build();
var yaml = "Double_Quoted";
ScalarStyle expected = ScalarStyle.DoubleQuoted;
var actual = serializer.Deserialize<ScalarStyle>(yaml);
Assert.Equal(expected, actual);
}

public class TestState
{
public int OnSerializedCallCount { get; set; }
Expand Down
5 changes: 3 additions & 2 deletions YamlDotNet.Test/Serialization/TypeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// SOFTWARE.

using Xunit;
using YamlDotNet.Serialization.NamingConventions;
using YamlDotNet.Serialization.Utilities;

namespace YamlDotNet.Test.Serialization
Expand Down Expand Up @@ -60,15 +61,15 @@ public static explicit operator int(ExplicitConversionIntWrapper wrapper)
public void Implicit_conversion_operator_is_used()
{
var data = new ImplicitConversionIntWrapper(2);
var actual = TypeConverter.ChangeType<int>(data);
var actual = TypeConverter.ChangeType<int>(data, NullNamingConvention.Instance);
Assert.Equal(data.value, actual);
}

[Fact]
public void Explicit_conversion_operator_is_used()
{
var data = new ExplicitConversionIntWrapper(2);
var actual = TypeConverter.ChangeType<int>(data);
var actual = TypeConverter.ChangeType<int>(data, NullNamingConvention.Instance);
Assert.Equal(data.value, actual);
}
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet.Test/YamlDotNet.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net70;net60;net47;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net80;net70;net60;net47</TargetFrameworks>
<IsPackable>false</IsPackable>
<AssemblyOriginatorKeyFile>..\YamlDotNet.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
Expand Down Expand Up @@ -117,4 +117,4 @@ public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, o
return false;
}
}
}
}
12 changes: 12 additions & 0 deletions YamlDotNet/Serialization/BuilderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class BuilderSkeleton<TBuilder>
where TBuilder : BuilderSkeleton<TBuilder>
{
internal INamingConvention namingConvention = NullNamingConvention.Instance;
internal INamingConvention enumNamingConvention = NullNamingConvention.Instance;
internal ITypeResolver typeResolver;
internal readonly YamlAttributeOverrides overrides;
internal readonly LazyComponentRegistrationList<Nothing, IYamlTypeConverter> typeConverterFactories;
Expand Down Expand Up @@ -98,6 +99,17 @@ public TBuilder WithNamingConvention(INamingConvention namingConvention)
return Self;
}

/// <summary>
/// Sets the <see cref="INamingConvention"/> to use when handling enum's.
/// </summary>
/// <param name="enumNamingConvention">Naming convention to use when handling enum's</param>
/// <returns></returns>
public TBuilder WithEnumNamingConvention(INamingConvention enumNamingConvention)
{
this.enumNamingConvention = enumNamingConvention;
return Self;
}

/// <summary>
/// Sets the <see cref="ITypeResolver" /> that will be used by the (de)serializer.
/// </summary>
Expand Down
18 changes: 13 additions & 5 deletions YamlDotNet/Serialization/DeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,19 @@ public DeserializerBuilder()
{ typeof(YamlSerializableNodeDeserializer), _ => new YamlSerializableNodeDeserializer(objectFactory.Value) },
{ typeof(TypeConverterNodeDeserializer), _ => new TypeConverterNodeDeserializer(BuildTypeConverters()) },
{ typeof(NullNodeDeserializer), _ => new NullNodeDeserializer() },
{ typeof(ScalarNodeDeserializer), _ => new ScalarNodeDeserializer(attemptUnknownTypeDeserialization, typeConverter, yamlFormatter) },
{ typeof(ArrayNodeDeserializer), _ => new ArrayNodeDeserializer() },
{ typeof(ScalarNodeDeserializer), _ => new ScalarNodeDeserializer(attemptUnknownTypeDeserialization, typeConverter, yamlFormatter, enumNamingConvention) },
{ typeof(ArrayNodeDeserializer), _ => new ArrayNodeDeserializer(enumNamingConvention) },
{ typeof(DictionaryNodeDeserializer), _ => new DictionaryNodeDeserializer(objectFactory.Value, duplicateKeyChecking) },
{ typeof(CollectionNodeDeserializer), _ => new CollectionNodeDeserializer(objectFactory.Value) },
{ typeof(CollectionNodeDeserializer), _ => new CollectionNodeDeserializer(objectFactory.Value, enumNamingConvention) },
{ typeof(EnumerableNodeDeserializer), _ => new EnumerableNodeDeserializer() },
{ typeof(ObjectNodeDeserializer), _ => new ObjectNodeDeserializer(objectFactory.Value, BuildTypeInspector(), ignoreUnmatched, duplicateKeyChecking, typeConverter) }
{
typeof(ObjectNodeDeserializer), _ => new ObjectNodeDeserializer(objectFactory.Value,
BuildTypeInspector(),
ignoreUnmatched,
duplicateKeyChecking,
typeConverter,
enumNamingConvention)
}
};

nodeTypeResolverFactories = new LazyComponentRegistrationList<Nothing, INodeTypeResolver>
Expand Down Expand Up @@ -454,7 +461,8 @@ public IValueDeserializer BuildValueDeserializer()
new NodeValueDeserializer(
nodeDeserializerFactories.BuildComponentList(),
nodeTypeResolverFactories.BuildComponentList(),
typeConverter
typeConverter,
enumNamingConvention
)
);
}
Expand Down
14 changes: 6 additions & 8 deletions YamlDotNet/Serialization/EventEmitters/JsonEventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization.NamingConventions;

namespace YamlDotNet.Serialization.EventEmitters
{
public sealed class JsonEventEmitter : ChainedEventEmitter
{
private readonly YamlFormatter formatter;
private readonly INamingConvention enumNamingConvention;

public JsonEventEmitter(IEventEmitter nextEmitter, YamlFormatter formatter)
public JsonEventEmitter(IEventEmitter nextEmitter, YamlFormatter formatter, INamingConvention enumNamingConvention)
: base(nextEmitter)
{
this.formatter = formatter;
}

public JsonEventEmitter(IEventEmitter nextEmitter)
: this(nextEmitter, YamlFormatter.Default)
{
this.enumNamingConvention = enumNamingConvention;
}

public override void Emit(AliasEventInfo eventInfo, IEmitter emitter)
Expand Down Expand Up @@ -75,8 +73,8 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
var valueIsEnum = eventInfo.Source.Type.IsEnum();
if (valueIsEnum)
{
eventInfo.RenderedValue = value.ToString()!;
eventInfo.Style = ScalarStyle.DoubleQuoted;
eventInfo.RenderedValue = formatter.FormatEnum(value, enumNamingConvention);
eventInfo.Style = formatter.PotentiallyQuoteEnums(value) ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain;
break;
}

Expand Down
Loading

0 comments on commit 88a9189

Please sign in to comment.