Skip to content

Commit

Permalink
Add static New*** method when type is Guid
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 2, 2022
1 parent e2e050e commit bf8b101
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ public static bool operator false(Foo x) => !x.value;
public static bool operator !(Foo x) => !x.value;
```

> When type is Guid, also implements `New***` static operator.

```csharp
public static GroupId NewGroupId();
```

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 $`{0}`.
Expand Down
7 changes: 7 additions & 0 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public readonly partial struct NoNamespace
{
}

[UnitOf(typeof(Guid))]
public readonly partial struct FooId { }

namespace Sample
{

Expand All @@ -27,6 +30,10 @@ void Foo()
{
_ = this.AsPrimitive();
_ = this.ToString();

_ = FooId.NewFooId();
Guid.NewGuid();

}

}
Expand Down
10 changes: 10 additions & 0 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ public virtual string TransformText()
this.Write(" x, in ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" y)\r\n {\r\n return !x.value.Equals(y.value);\r\n }\r\n\r\n");
if (Type == "Guid" || Type == "System.Guid") {
this.Write(" public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" New");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("()\r\n {\r\n return new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("(Guid.NewGuid());\r\n }\r\n\r\n");
}
this.Write("\r\n");
if (HasFlag(UnitGenerateOptions.ParseMethod)) {
this.Write(" \r\n // UnitGenerateOptions.ParseMethod\r\n\r\n public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
Expand Down
8 changes: 8 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ namespace <#= Namespace #>
return !x.value.Equals(y.value);
}

<# if (Type == "Guid" || Type == "System.Guid") { #>
public static <#= Name #> New<#= Name #>()
{
return new <#= Name #>(Guid.NewGuid());
}

<# } #>

<# if (HasFlag(UnitGenerateOptions.ParseMethod)) { #>
// UnitGenerateOptions.ParseMethod

Expand Down

0 comments on commit bf8b101

Please sign in to comment.