Skip to content

Commit

Permalink
Merge pull request #102 from hadashiA/ku/fix-ctor-default-value
Browse files Browse the repository at this point in the history
Fix generated default value from ctor
  • Loading branch information
hadashiA authored Apr 17, 2024
2 parents 9ec3d60 + 84ca192 commit d5a0ad6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 53 deletions.
28 changes: 2 additions & 26 deletions VYaml.SourceGenerator.Roslyn3/VYamlSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,7 @@ static bool TryEmitDeserializeMethod(
codeWriter.AppendLine();
foreach (var memberMeta in typeMeta.MemberMetas)
{
codeWriter.Append($"var __{memberMeta.Name}__ = ");
if (memberMeta.HasExplicitDefaultValueFromConstructor)
{
switch (memberMeta.ExplicitDefaultValueFromConstructor)
{
case null:
codeWriter.AppendLine("null;", false);
break;
case string stringValue:
codeWriter.AppendLine($"\"{stringValue}\";", false);
break;
case {} anyValue:
codeWriter.AppendLine($"{anyValue};", false);
break;
}
}
else
{
codeWriter.AppendLine($"default({memberMeta.FullTypeName});", false);
}
codeWriter.Append($"var __{memberMeta.Name}__ = {memberMeta.EmitDefaultValue()};");
}

using (codeWriter.BeginBlockScope("while (!parser.End && parser.CurrentEventType != ParseEventType.MappingEnd)"))
Expand Down Expand Up @@ -505,12 +486,7 @@ static bool TryEmitDeserializeMethod(
{
if (x.HasExplicitDefaultValueFromConstructor)
{
return x.ExplicitDefaultValueFromConstructor switch
{
null => $"__{x.Name}__ = null",
string stringValue => $"__{x.Name}__ = \"{stringValue}\"",
{ } anyValue => $"__{x.Name}__ = {anyValue}"
};
return $"__{x.Name}__ = {x.EmitDefaultValue()}";
}
return $"__{x.Name}__";
}));
Expand Down
19 changes: 19 additions & 0 deletions VYaml.SourceGenerator/MemberMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ public Location GetLocation(TypeDeclarationSyntax fallback)
var location = Symbol.Locations.FirstOrDefault() ?? fallback.Identifier.GetLocation();
return location;
}

public string EmitDefaultValue()
{
if (!HasExplicitDefaultValueFromConstructor)
{
return $"default({FullTypeName})";
}

return ExplicitDefaultValueFromConstructor switch
{
null => $"default({FullTypeName})",
string x => $"\"{x}\"",
float x => $"{x}f",
double x => $"{x}d",
decimal x => $"{x}m",
bool x => x ? "true" : "false",
_ => ExplicitDefaultValueFromConstructor.ToString()
};
}
}
2 changes: 1 addition & 1 deletion VYaml.SourceGenerator/VYaml.SourceGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="$(Configuration) == 'Release'">
<Copy SourceFiles="$(TargetPath)"
DestinationFiles="$(UnityAssetsRoot)\Plugins\$(TargetFileName)"
DestinationFiles="$(UnityAssetsRoot)\$(TargetFileName)"
SkipUnchangedFiles="true" />
</Target>
</Project>
28 changes: 2 additions & 26 deletions VYaml.SourceGenerator/VYamlIncrementalSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,7 @@ static bool TryEmitDeserializeMethod(
codeWriter.AppendLine();
foreach (var memberMeta in typeMeta.MemberMetas)
{
codeWriter.Append($"var __{memberMeta.Name}__ = ");
if (memberMeta.HasExplicitDefaultValueFromConstructor)
{
switch (memberMeta.ExplicitDefaultValueFromConstructor)
{
case null:
codeWriter.AppendLine("null;", false);
break;
case string stringValue:
codeWriter.AppendLine($"\"{stringValue}\";", false);
break;
case {} anyValue:
codeWriter.AppendLine($"{anyValue};", false);
break;
}
}
else
{
codeWriter.AppendLine($"default({memberMeta.FullTypeName});", false);
}
codeWriter.Append($"var __{memberMeta.Name}__ = {memberMeta.EmitDefaultValue()};");
}

using (codeWriter.BeginBlockScope("while (!parser.End && parser.CurrentEventType != ParseEventType.MappingEnd)"))
Expand Down Expand Up @@ -517,12 +498,7 @@ static bool TryEmitDeserializeMethod(
{
if (x.HasExplicitDefaultValueFromConstructor)
{
return x.ExplicitDefaultValueFromConstructor switch
{
null => $"__{x.Name}__ = null",
string stringValue => $"__{x.Name}__ = \"{stringValue}\"",
{ } anyValue => $"__{x.Name}__ = {anyValue}"
};
return $"__{x.Name}__ = {x.EmitDefaultValue()}";
}
return $"__{x.Name}__";
}));
Expand Down
9 changes: 9 additions & 0 deletions VYaml.Tests/Serialization/GeneratedFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,14 @@ public void Deserialize_CustomConstructorWithSetter()
Assert.That(result.Bar, Is.EqualTo("aaa"));
Assert.That(result.Hoge, Is.EqualTo("bbb"));
}

[Test]
public void Deserialize_CustomCOnstructorWithDefaultValue()
{
var result = Deserialize<WithCustomConstructor3>("{}");
Assert.That(result.X, Is.EqualTo(100));
Assert.That(result.Y, Is.EqualTo(222m));
Assert.That(result.Z, Is.EqualTo(true));
}
}
}
15 changes: 15 additions & 0 deletions VYaml.Tests/TypeDeclarations/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public WithCustomConstructor2(int foo, string bar)
}
}

[YamlObject]
public partial record WithCustomConstructor3
{
public int X { get; }
public decimal Y { get; }
public bool Z { get; }

[YamlConstructor]
public WithCustomConstructor3(int x = 100, decimal y = 222m, bool z = true)
{
X = x;
Y = y;
Z = z;
}
}

[YamlObject]
public partial class WithCustomConstructorAndOtherProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,14 @@ public void Deserialize_CustomConstructorWithSetter()
Assert.That(result.Bar, Is.EqualTo("aaa"));
Assert.That(result.Hoge, Is.EqualTo("bbb"));
}

[Test]
public void Deserialize_CustomCOnstructorWithDefaultValue()
{
var result = Deserialize<WithCustomConstructor3>("{}");
Assert.That(result.X, Is.EqualTo(100));
Assert.That(result.Y, Is.EqualTo(222m));
Assert.That(result.Z, Is.EqualTo(true));
}
}
}
15 changes: 15 additions & 0 deletions VYaml.Unity/Assets/Tests/TypeDeclarations/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ public WithCustomConstructor2(int foo, string bar)
}
}

[YamlObject]
public partial record WithCustomConstructor3
{
public int X { get; }
public decimal Y { get; }
public bool Z { get; }

[YamlConstructor]
public WithCustomConstructor3(int x = 100, decimal y = 222m, bool z = true)
{
X = x;
Y = y;
Z = z;
}
}

[YamlObject]
public partial class WithCustomConstructorAndOtherProps
Expand Down
Binary file modified VYaml.Unity/Assets/VYaml/Runtime/VYaml.SourceGenerator.dll
Binary file not shown.

0 comments on commit d5a0ad6

Please sign in to comment.