Skip to content

Commit

Permalink
Merge pull request #59 from hadashiA/ku/default-value-from-ctor2
Browse files Browse the repository at this point in the history
Fix default value handling
  • Loading branch information
hadashiA authored Nov 4, 2023
2 parents 3883264 + 9fcd256 commit b334da4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions VYaml.SourceGenerator.Roslyn3/VYamlSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,26 @@ static bool TryEmitDeserializeMethod(
codeWriter.AppendLine();
foreach (var memberMeta in typeMeta.MemberMetas)
{
codeWriter.AppendLine($"var __{memberMeta.Name}__ = default({memberMeta.FullTypeName});");
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);
}
}

using (codeWriter.BeginBlockScope("while (!parser.End && parser.CurrentEventType != ParseEventType.MappingEnd)"))
Expand Down Expand Up @@ -639,4 +658,4 @@ static bool TryGetConstructor(
constructedMembers = parameterMembers;
return !error;
}
}
}
Binary file not shown.

0 comments on commit b334da4

Please sign in to comment.