From c8c72ddf2b01c5ad2ce81d252c892c5a44ae19df Mon Sep 17 00:00:00 2001 From: neuecc Date: Mon, 7 Feb 2022 12:14:41 +0900 Subject: [PATCH] Guid generation uses `New`, Add Ulid support --- README.md | 3 ++- sandbox/ConsoleApp/ConsoleApp.csproj | 2 ++ sandbox/ConsoleApp/Program.cs | 6 ++++++ src/UnitGenerator/CodeTemplate.cs | 18 ++++++++++++++++++ src/UnitGenerator/CodeTemplate.tt | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c43498e..eb21058 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/sandbox/ConsoleApp/ConsoleApp.csproj b/sandbox/ConsoleApp/ConsoleApp.csproj index 77ff9a9..2391fc3 100644 --- a/sandbox/ConsoleApp/ConsoleApp.csproj +++ b/sandbox/ConsoleApp/ConsoleApp.csproj @@ -13,6 +13,8 @@ + + diff --git a/sandbox/ConsoleApp/Program.cs b/sandbox/ConsoleApp/Program.cs index ccf086b..f70ecd2 100644 --- a/sandbox/ConsoleApp/Program.cs +++ b/sandbox/ConsoleApp/Program.cs @@ -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 { @@ -34,6 +37,9 @@ void Foo() _ = FooId.NewFooId(); Guid.NewGuid(); + + + } } diff --git a/src/UnitGenerator/CodeTemplate.cs b/src/UnitGenerator/CodeTemplate.cs index 780bf6b..d73bce1 100644 --- a/src/UnitGenerator/CodeTemplate.cs +++ b/src/UnitGenerator/CodeTemplate.cs @@ -124,6 +124,10 @@ 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 "); @@ -131,6 +135,20 @@ public virtual string TransformText() 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)); diff --git a/src/UnitGenerator/CodeTemplate.tt b/src/UnitGenerator/CodeTemplate.tt index 045ef09..3a472cd 100644 --- a/src/UnitGenerator/CodeTemplate.tt +++ b/src/UnitGenerator/CodeTemplate.tt @@ -102,6 +102,11 @@ 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()); @@ -109,6 +114,19 @@ namespace <#= Namespace #> <# } #> +<# 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