Skip to content

Commit

Permalink
Merge pull request #35 from Cysharp/hadashiA/fix-tostring
Browse files Browse the repository at this point in the history
Fix ToString() implementations
  • Loading branch information
hadashiA authored Sep 28, 2023
2 parents 4e1d81d + bf48a80 commit 897b873
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/UnitGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,25 +391,39 @@ public override int GetHashCode()
}
""");
if (prop.ToStringFormat is { } format)
if (prop.IsString())
{
sb.AppendLine($$"""
public override string ToString()
{
return string.Format({{format}}, value);
}
if (prop.ToStringFormat is { } format)
{
sb.AppendLine($$"""
public override string ToString() => value == null ? "null" : string.Format("{{format}}", value);
""");
}
else
{
sb.AppendLine("""
public override string ToString() => value == null ? "null" : value.ToString();
""");
}
}
else
{
sb.AppendLine("""
public override string ToString()
{
return value.ToString();
}
if (prop.ToStringFormat is { } format)
{
sb.AppendLine($$"""
public override string ToString() => string.Format("{{format}}", value);
""");
}
else
{
sb.AppendLine("""
public override string ToString() => value.ToString();
""");
}
}

if (prop.IsGuid())
Expand Down

0 comments on commit 897b873

Please sign in to comment.