Skip to content

Commit

Permalink
Add Empty genereation for Guid and Ulid
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 7, 2022
1 parent 69f0e96 commit b11711a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
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 or [Ulid](https://github.com/Cysharp/Ulid), also implements `New()` and `New***()` static operator.
> When type is Guid or [Ulid](https://github.com/Cysharp/Ulid), also implements `Empty, New()` and `New***()` static operator.
```csharp
public static readonly GroupId Empty;
public static GroupId New();
public static GroupId NewGroupId();
```
Expand Down
15 changes: 9 additions & 6 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@



Console.WriteLine(new Sample.Hp(100).ToString());
Console.WriteLine(BarId.New());
var id1 = FooId.Empty;
var id2 = new FooId(Guid.Empty);


Console.WriteLine(id1 == id2);

[UnitOf(typeof(int))]
public readonly partial struct NoNamespace
Expand Down Expand Up @@ -37,10 +40,13 @@ void Foo()

_ = FooId.NewFooId();
Guid.NewGuid();
//public static readonly Guid Empty;
//Guid.Empty

// public static readonly Ulid Empty = default(Ulid);
// Ulid.Empty



}

}
Expand Down Expand Up @@ -88,10 +94,7 @@ static void Foo(short x)

static void Main(string[] args)
{
new SampleValidate(99999);


default(StreetAddress).AsPrimitive();

}
}
Expand Down
12 changes: 10 additions & 2 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public virtual string TransformText()
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(" public static readonly ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" Empty = default(");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(");\r\n\r\n public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" New()\r\n {\r\n return new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
Expand All @@ -136,7 +140,11 @@ public virtual string TransformText()
}
this.Write("\r\n");
if (Type == "Ulid" || Type == "System.Ulid") {
this.Write(" public static ");
this.Write(" public static readonly ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" Empty = default(");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(");\r\n \r\n public static ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" New()\r\n {\r\n return new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
Expand Down
4 changes: 4 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace <#= Namespace #>
}

<# if (Type == "Guid" || Type == "System.Guid") { #>
public static readonly <#= Name #> Empty = default(<#= Name #>);

public static <#= Name #> New()
{
return new <#= Name #>(Guid.NewGuid());
Expand All @@ -115,6 +117,8 @@ namespace <#= Namespace #>
<# } #>

<# if (Type == "Ulid" || Type == "System.Ulid") { #>
public static readonly <#= Name #> Empty = default(<#= Name #>);

public static <#= Name #> New()
{
return new <#= Name #>(Ulid.NewUlid());
Expand Down

0 comments on commit b11711a

Please sign in to comment.