Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support explicit default value from ctor #58

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions VYaml.SourceGenerator.Roslyn3/MemberMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ class MemberMeta
public string Name { get; }
public string FullTypeName { get; }
public ITypeSymbol MemberType { get; }
public INamedTypeSymbol? CustomFormatter { get; }
public string? CustomFormatterName { get; }
public bool IsField { get; }
public bool IsProperty { get; }
public bool IsSettable { get; }
public bool IsConstructorParameter { get; }
public int Order { get; }
public bool HasExplicitOrder { get; }
public bool HasKeyNameAlias { get; }

public bool IsConstructorParameter { get; set; }
public bool HasExplicitDefaultValueFromConstructor { get; set; }
public object? ExplicitDefaultValueFromConstructor { get; set; }

public INamedTypeSymbol? CustomFormatter { get; }
public string? CustomFormatterName { get; }

public string KeyName => keyName ??= KeyNameHelper.ToCamelCase(Name);
public byte[] KeyNameUtf8Bytes => keyNameUtf8Bytes ??= System.Text.Encoding.UTF8.GetBytes(KeyName);

Expand Down
20 changes: 19 additions & 1 deletion VYaml.SourceGenerator.Roslyn3/VYamlSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,19 @@ static bool TryEmitDeserializeMethod(
codeWriter.Append("return new ");
if (selectedConstructor != null)
{
var parameters = string.Join(",", constructedMembers.Select(x => $"__{x.Name}__"));
var parameters = string.Join(",", constructedMembers.Select(x =>
{
if (x.HasExplicitDefaultValueFromConstructor)
{
return x.ExplicitDefaultValueFromConstructor switch
{
null => $"__{x.Name}__ = null",
string stringValue => $"__{x.Name}__ = \"{stringValue}\"",
{ } anyValue => $"__{x.Name}__ = {anyValue}"
};
}
return $"__{x.Name}__";
}));
codeWriter.Append($"{typeMeta.TypeName}({parameters})", false);
}
else
Expand Down Expand Up @@ -603,6 +615,12 @@ static bool TryGetConstructor(
.FirstOrDefault(member => parameter.Name.Equals(member.Name, StringComparison.OrdinalIgnoreCase));
if (matchedMember != null)
{
matchedMember.IsConstructorParameter = true;
if (parameter.HasExplicitDefaultValue)
{
matchedMember.HasExplicitDefaultValueFromConstructor = true;
matchedMember.ExplicitDefaultValueFromConstructor = parameter.ExplicitDefaultValue;
}
parameterMembers.Add(matchedMember);
}
else
Expand Down
10 changes: 10 additions & 0 deletions VYaml.Tests/Serialization/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
using NUnit.Framework;
using VYaml.Internal;
using VYaml.Serialization;
using VYaml.Tests.TypeDeclarations;

namespace VYaml.Tests.Serialization
{
[TestFixture]
public class SerializerTest
{
[Test]
public void Deserialize_ExplicitDefaultValueFromConstructor()
{
var yamlBytes = StringEncoding.Utf8.GetBytes("valueSet: 22");
var value = YamlSerializer.Deserialize<WithDefaultValue>(yamlBytes);
Assert.That(value.Value, Is.EqualTo(12));
Assert.That(value.ValueSet, Is.EqualTo(22));
}

[Test]
public void DeserializeMultipleDocuments()
{
Expand Down
14 changes: 14 additions & 0 deletions VYaml.Tests/TypeDeclarations/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public partial class WithValueTuple
public ValueTuple<int, int, int, int, int, int, int> Seven { get; set; }
}

[YamlObject]
public partial class WithDefaultValue
{
public readonly int Value;
public readonly int ValueSet;

[YamlConstructor]
public WithDefaultValue(int valueSet, int value = 12)
{
ValueSet = valueSet;
Value = value;
}
}

public enum SimpleEnum
{
A,
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
using NUnit.Framework;
using VYaml.Internal;
using VYaml.Serialization;
using VYaml.Tests.TypeDeclarations;

namespace VYaml.Tests.Serialization
{
[TestFixture]
public class SerializerTest
{
[Test]
public void Deserialize_ExplicitDefaultValueFromConstructor()
{
var yamlBytes = StringEncoding.Utf8.GetBytes("valueSet: 22");
var value = YamlSerializer.Deserialize<WithDefaultValue>(yamlBytes);
Assert.That(value.Value, Is.EqualTo(12));
Assert.That(value.ValueSet, Is.EqualTo(22));
}

[Test]
public void DeserializeMultipleDocuments()
{
Expand Down
14 changes: 14 additions & 0 deletions VYaml.Unity/Assets/VYaml/Tests/TypeDeclarations/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public partial class WithValueTuple
public ValueTuple<int, int, int, int, int, int, int> Seven { get; set; }
}

[YamlObject]
public partial class WithDefaultValue
{
public readonly int Value;
public readonly int ValueSet;

[YamlConstructor]
public WithDefaultValue(int valueSet, int value = 12)
{
ValueSet = valueSet;
Value = value;
}
}

public enum SimpleEnum
{
A,
Expand Down