Skip to content

Commit

Permalink
ToString generate only value in default
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 28, 2021
1 parent 266d105 commit e2e050e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public readonly partial struct UserId : IEquatable<UserId>
public bool Equals(UserId other) => value.Equals(other.value);
public override bool Equals(object? obj) => // snip...
public override int GetHashCode() => value.GetHashCode();
public override string ToString() => "UserId(" + value + ")";
public override string ToString() => value.ToString();
public static bool operator ==(in UserId x, in UserId y) => x.value.Equals(y.value);
public static bool operator !=(in UserId x, in UserId y) => !x.value.Equals(y.value);

Expand Down Expand Up @@ -75,7 +75,7 @@ public readonly partial struct Hp : IEquatable<Hp> , IComparable<Hp>
public bool Equals(Hp other) => value.Equals(other.value);
public override bool Equals(object? obj) => // snip...
public override int GetHashCode() => value.GetHashCode();
public override string ToString() => "Hp(" + value + ")";
public override string ToString() => value.ToString();
public static bool operator ==(in Hp x, in Hp y) => x.value.Equals(y.value);
public static bool operator !=(in Hp x, in Hp y) => !x.value.Equals(y.value);
private class HpTypeConverter : System.ComponentModel.TypeConverter { /* snip... */ }
Expand Down Expand Up @@ -228,7 +228,7 @@ public static bool operator !(Foo x) => !x.value;

Second parameter `UnitGenerateOptions options` can configure which method to implement, default is `None`.

Third parameter `strign toStringFormat` can configure `ToString` format. Default is null and output as $`TypeName({0})`.
Third parameter `strign toStringFormat` can configure `ToString` format. Default is null and output as $`{0}`.

## UnitGenerateOptions

Expand Down
2 changes: 2 additions & 0 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@



Console.WriteLine(new Sample.Hp(100).ToString());

[UnitOf(typeof(int))]
public readonly partial struct NoNamespace
Expand All @@ -25,6 +26,7 @@ public readonly partial struct Hp
void Foo()
{
_ = this.AsPrimitive();
_ = this.ToString();
}

}
Expand Down
4 changes: 1 addition & 3 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ public virtual string TransformText()
" public override int GetHashCode()\r\n {\r\n return value.GetHa" +
"shCode();\r\n }\r\n\r\n public override string ToString()\r\n {\r\n");
if (ToStringFormat == null) {
this.Write(" return \"");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("(\" + value + \")\";\r\n");
this.Write(" return value.ToString();\r\n");
} else {
this.Write(" return string.Format(\"");
this.Write(this.ToStringHelper.ToStringWithCulture(ToStringFormat));
Expand Down
2 changes: 1 addition & 1 deletion src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace <#= Namespace #>
public override string ToString()
{
<# if (ToStringFormat == null) { #>
return "<#= Name #>(" + value + ")";
return value.ToString();
<# } else { #>
return string.Format("<#= ToStringFormat #>", value);
<# } #>
Expand Down

0 comments on commit e2e050e

Please sign in to comment.