Skip to content

Commit

Permalink
Guid generation uses New, Add Ulid support
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 7, 2022
1 parent e810d72 commit c8c72dd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ 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.
> When type is Guid or [Ulid](https://github.com/Cysharp/Ulid), also implements `New()` and `New***()` static operator.
```csharp
public static GroupId New();
public static GroupId NewGroupId();
```

Expand Down
2 changes: 2 additions & 0 deletions sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="MessagePack" Version="2.2.85" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />
<PackageReference Include="Ulid" Version="1.2.6" />
<PackageReference Include="Ulid.MessagePack" Version="1.2.6" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public readonly partial struct NoNamespace
[UnitOf(typeof(Guid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator)]
public readonly partial struct FooId { }

[UnitOf(typeof(Ulid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator | UnitGenerateOptions.MessagePackFormatter)]
public readonly partial struct BarId { }

namespace Sample
{

Expand All @@ -34,6 +37,9 @@ void Foo()
_ = FooId.NewFooId();
Guid.NewGuid();




}

}
Expand Down
18 changes: 18 additions & 0 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,31 @@ public virtual string TransformText()
if (Type == "Guid" || Type == "System.Guid") {
this.Write(" public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" New()\r\n {\r\n return new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("(Guid.NewGuid());\r\n }\r\n\r\n 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 (Type == "Ulid" || Type == "System.Ulid") {
this.Write(" public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" New()\r\n {\r\n return new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("(Ulid.NewGuid());\r\n }\r\n\r\n 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("(Ulid.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
18 changes: 18 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,31 @@ namespace <#= Namespace #>
}

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

public static <#= Name #> New<#= Name #>()
{
return new <#= Name #>(Guid.NewGuid());
}

<# } #>

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

public static <#= Name #> New<#= Name #>()
{
return new <#= Name #>(Ulid.NewGuid());
}

<# } #>

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

Expand Down

0 comments on commit c8c72dd

Please sign in to comment.