Skip to content

Commit

Permalink
Merge pull request #31 from Cysharp/hadashiA/normalize
Browse files Browse the repository at this point in the history
Add `UnitGenerateOptions.Normalize`
  • Loading branch information
hadashiA authored Aug 17, 2023
2 parents df0421f + 1b9307c commit 3deabf1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 52 deletions.
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@ enum UnitGenerateOptions
{
None = 0,
ImplicitOperator = 1,
ParseMethod = 2,
MinMaxMethod = 4,
ArithmeticOperator = 8,
ValueArithmeticOperator = 16,
Comparable = 32,
Validate = 64,
JsonConverter = 128,
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048,
JsonConverterDictionaryKeySupport = 4096
ParseMethod = 1 << 1,
MinMaxMethod = 1 << 2,
ArithmeticOperator = 1 << 3,
ValueArithmeticOperator = 1 << 4,
Comparable = 1 << 5,
Validate = 1 << 6,
JsonConverter = 1 << 7,
MessagePackFormatter = 1 << 8,
DapperTypeHandler = 1 << 9,
EntityFrameworkValueConverter = 1 << 10,
WithoutComparisonOperator = 1 << 11,
JsonConverterDictionaryKeySupport = 1 << 12,
Normalize = 1 << 13,
}
```

Expand Down Expand Up @@ -172,6 +173,7 @@ public readonly partial struct UserId
- [Comparable](#comparable)
- [WithoutComparisonOperator](#withoutcomparisonoperator)
- [Validate](#validate)
- [Normalize](#normalize)
- [JsonConverter](#jsonconverter)
- [JsonConverterDictionaryKeySupport](#jsonconverterdictionarykeysupport)
- [MessagePackFormatter](#messagepackformatter)
Expand Down Expand Up @@ -382,6 +384,32 @@ public T(int value)
private partial void Validate();
```

### Normalize

Implements `partial void Normalize(ref T value)` method that is called on constructor.

```csharp
// You can implement this custom normalize method to change value during initialization
[UnitOf(typeof(int), UnitGenerateOptions.Normalize)]
public readonly partial struct SampleValidate
{
// impl here.
private partial void Normalize(ref int value)
{
value = Math.Max(value, 9999);
}
}

// Source generator generate this codes.
public T(int value)
{
this.value = value;
this.Normalize(ref this.value);
}

private partial void Normalize(ref int value);
```

### JsonConverter

Implements `System.Text.Json`'s `JsonConverter`. It will be used `JsonSerializer` automatically.
Expand Down
7 changes: 6 additions & 1 deletion sandbox/ConsoleApp/AllPrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

namespace ConsoleApp
{
[UnitOf(typeof(int), UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)]
[UnitOf(typeof(int), UnitGenerateOptions.Normalize | UnitGenerateOptions.ParseMethod | UnitGenerateOptions.MinMaxMethod | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.Validate | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.DapperTypeHandler | UnitGenerateOptions.EntityFrameworkValueConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport)]
public readonly partial struct A
{
private partial void Normalize(ref int value)
{
value = Math.Clamp(value, 0, 100);
}

private partial void Validate()
{
_ = AsPrimitive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override bool Equals(object obj)
return value.Equals((int)obj);
}

return value.Equals(obj);
return value.Equals(obj);
}

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ internal enum UnitGenerateOptions
{
None = 0,
ImplicitOperator = 1,
ParseMethod = 2,
MinMaxMethod = 4,
ArithmeticOperator = 8,
ValueArithmeticOperator = 16,
Comparable = 32,
Validate = 64,
JsonConverter = 128,
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048,
JsonConverterDictionaryKeySupport = 4096
ParseMethod = 1 << 1,
MinMaxMethod = 1 << 2,
ArithmeticOperator = 1 << 3,
ValueArithmeticOperator = 1 << 4,
Comparable = 1 << 5,
Validate = 1 << 6,
JsonConverter = 1 << 7,
MessagePackFormatter = 1 << 8,
DapperTypeHandler = 1 << 9,
EntityFrameworkValueConverter = 1 << 10,
WithoutComparisonOperator = 1 << 11,
JsonConverterDictionaryKeySupport = 1 << 12,
Normalize = 1 << 13,
}
}
40 changes: 27 additions & 13 deletions src/UnitGenerator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,19 @@ internal enum UnitGenerateOptions
{
None = 0,
ImplicitOperator = 1,
ParseMethod = 2,
MinMaxMethod = 4,
ArithmeticOperator = 8,
ValueArithmeticOperator = 16,
Comparable = 32,
Validate = 64,
JsonConverter = 128,
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048,
JsonConverterDictionaryKeySupport = 4096
ParseMethod = 1 << 1,
MinMaxMethod = 1 << 2,
ArithmeticOperator = 1 << 3,
ValueArithmeticOperator = 1 << 4,
Comparable = 1 << 5,
Validate = 1 << 6,
JsonConverter = 1 << 7,
MessagePackFormatter = 1 << 8,
DapperTypeHandler = 1 << 9,
EntityFrameworkValueConverter = 1 << 10,
WithoutComparisonOperator = 1 << 11,
JsonConverterDictionaryKeySupport = 1 << 12,
Normalize = 1 << 13,
}
}
""";
Expand Down Expand Up @@ -218,6 +219,12 @@ namespace {{ns}}
{
this.value = value;
""");
if (prop.HasFlag(UnitGenerateOptions.Normalize))
{
sb.AppendLine("""
this.Normalize(ref this.value);
""");
}
if (prop.HasFlag(UnitGenerateOptions.Validate))
{
sb.AppendLine("""
Expand All @@ -228,7 +235,14 @@ namespace {{ns}}
}
""");

if (prop.HasFlag(UnitGenerateOptions.Normalize))
{
sb.AppendLine($$"""
private partial void Normalize(ref {{innerTypeName}} value);
""");

}
if (prop.HasFlag(UnitGenerateOptions.Validate))
{
sb.AppendLine($$"""
Expand Down
27 changes: 14 additions & 13 deletions src/UnitGenerator/UnitGenerateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ internal enum UnitGenerateOptions
{
None = 0,
ImplicitOperator = 1,
ParseMethod = 2,
MinMaxMethod = 4,
ArithmeticOperator = 8,
ValueArithmeticOperator = 16,
Comparable = 32,
Validate = 64,
JsonConverter = 128,
MessagePackFormatter = 256,
DapperTypeHandler = 512,
EntityFrameworkValueConverter = 1024,
WithoutComparisonOperator = 2048,
JsonConverterDictionaryKeySupport = 4096
ParseMethod = 1 << 1,
MinMaxMethod = 1 << 2,
ArithmeticOperator = 1 << 3,
ValueArithmeticOperator = 1 << 4,
Comparable = 1 << 5,
Validate = 1 << 6,
JsonConverter = 1 << 7,
MessagePackFormatter = 1 << 8,
DapperTypeHandler = 1 << 9,
EntityFrameworkValueConverter = 1 << 10,
WithoutComparisonOperator = 1 << 11,
JsonConverterDictionaryKeySupport = 1 << 12,
Normalize = 1 << 13,
}
}
}

0 comments on commit 3deabf1

Please sign in to comment.