Skip to content

Commit

Permalink
Add UnitGenerateOptions.WithoutComparisonOperator
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 2, 2022
1 parent bf8b101 commit 0ea6619
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ public static bool operator >=(in T x, in T y) => x.value >= y.value;
public static bool operator <=(in T x, in T y) => x.value <= y.value;
```

### WithoutComparisonOperator

Without implements `>`, `<`, `>=`, `<=` operators. For example, useful for Guid.

```csharpd
[UnitOf(typeof(Guid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator)]
public readonly partial struct FooId { }
```

### Validate

Implements `partial void Validate()` method that is called on constructor.
Expand Down
2 changes: 1 addition & 1 deletion sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public readonly partial struct NoNamespace
{
}

[UnitOf(typeof(Guid))]
[UnitOf(typeof(Guid), UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator)]
public readonly partial struct FooId { }

namespace Sample
Expand Down
8 changes: 6 additions & 2 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ public virtual string TransformText()
this.Write(" \r\n // UnitGenerateOptions.Comparable\r\n\r\n public int CompareTo(");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" other)\r\n {\r\n return value.CompareTo(other.value);\r\n }\r\n" +
" \r\n public static bool operator >(in ");
"\r\n");
if (!HasFlag(UnitGenerateOptions.WithoutComparisonOperator)) {
this.Write(" \r\n \r\n public static bool operator >(in ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" x, in ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
Expand All @@ -317,7 +319,9 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" x, in ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(" y)\r\n {\r\n return x.value <= y.value;\r\n }\r\n\r\n");
this.Write(" y)\r\n {\r\n return x.value <= y.value;\r\n }\r\n");
}
this.Write("\r\n");
}
this.Write("\r\n");
if (HasFlag(UnitGenerateOptions.JsonConverter)) {
Expand Down
3 changes: 3 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ namespace <#= Namespace #>
{
return value.CompareTo(other.value);
}

<# if (!HasFlag(UnitGenerateOptions.WithoutComparisonOperator)) { #>

public static bool operator >(in <#= Name #> x, in <#= Name #> y)
{
Expand All @@ -285,6 +287,7 @@ namespace <#= Namespace #>
{
return x.value <= y.value;
}
<# } #>

<# } #>

Expand Down
1 change: 1 addition & 0 deletions src/UnitGenerator/UnitGenerateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ internal enum UnitGenerateOptions
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048
}
}
14 changes: 7 additions & 7 deletions src/UnitGenerator/UnitOfAttributeTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
// ランタイム バージョン: 16.0.0.0
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// このファイルへの変更は、正しくない動作の原因になる可能性があり、
// コードが再生成されると失われます。
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
namespace UnitGenerator
Expand All @@ -17,15 +17,14 @@ namespace UnitGenerator
/// <summary>
/// Class to produce the template output
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public partial class UnitOfAttributeTemplate : UnitOfAttributeTemplateBase
{
/// <summary>
/// Create the template output
/// </summary>
public virtual string TransformText()
{
this.Write("");
this.Write(@"// <auto-generated>
// THIS (.cs) FILE IS GENERATED BY UnitGenerator. DO NOT CHANGE IT.
// </auto-generated>
Expand Down Expand Up @@ -65,6 +64,7 @@ internal enum UnitGenerateOptions
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048
}
}");
return this.GenerationEnvironment.ToString();
Expand All @@ -74,7 +74,7 @@ internal enum UnitGenerateOptions
/// <summary>
/// Base class for this transformation
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public class UnitOfAttributeTemplateBase
{
#region Fields
Expand Down
1 change: 1 addition & 0 deletions src/UnitGenerator/UnitOfAttributeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ namespace UnitGenerator
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048
}
}

0 comments on commit 0ea6619

Please sign in to comment.