Skip to content

Commit 3deabf1

Browse files
authored
Merge pull request #31 from Cysharp/hadashiA/normalize
Add `UnitGenerateOptions.Normalize`
2 parents df0421f + 1b9307c commit 3deabf1

File tree

6 files changed

+101
-52
lines changed

6 files changed

+101
-52
lines changed

README.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,19 @@ enum UnitGenerateOptions
115115
{
116116
None = 0,
117117
ImplicitOperator = 1,
118-
ParseMethod = 2,
119-
MinMaxMethod = 4,
120-
ArithmeticOperator = 8,
121-
ValueArithmeticOperator = 16,
122-
Comparable = 32,
123-
Validate = 64,
124-
JsonConverter = 128,
125-
MessagePackFormatter = 256,
126-
DapperTypeHandler = 512,
127-
EntityFrameworkValueConverter = 1024,
128-
WithoutComparisonOperator = 2048,
129-
JsonConverterDictionaryKeySupport = 4096
118+
ParseMethod = 1 << 1,
119+
MinMaxMethod = 1 << 2,
120+
ArithmeticOperator = 1 << 3,
121+
ValueArithmeticOperator = 1 << 4,
122+
Comparable = 1 << 5,
123+
Validate = 1 << 6,
124+
JsonConverter = 1 << 7,
125+
MessagePackFormatter = 1 << 8,
126+
DapperTypeHandler = 1 << 9,
127+
EntityFrameworkValueConverter = 1 << 10,
128+
WithoutComparisonOperator = 1 << 11,
129+
JsonConverterDictionaryKeySupport = 1 << 12,
130+
Normalize = 1 << 13,
130131
}
131132
```
132133

@@ -172,6 +173,7 @@ public readonly partial struct UserId
172173
- [Comparable](#comparable)
173174
- [WithoutComparisonOperator](#withoutcomparisonoperator)
174175
- [Validate](#validate)
176+
- [Normalize](#normalize)
175177
- [JsonConverter](#jsonconverter)
176178
- [JsonConverterDictionaryKeySupport](#jsonconverterdictionarykeysupport)
177179
- [MessagePackFormatter](#messagepackformatter)
@@ -382,6 +384,32 @@ public T(int value)
382384
private partial void Validate();
383385
```
384386

387+
### Normalize
388+
389+
Implements `partial void Normalize(ref T value)` method that is called on constructor.
390+
391+
```csharp
392+
// You can implement this custom normalize method to change value during initialization
393+
[UnitOf(typeof(int), UnitGenerateOptions.Normalize)]
394+
public readonly partial struct SampleValidate
395+
{
396+
// impl here.
397+
private partial void Normalize(ref int value)
398+
{
399+
value = Math.Max(value, 9999);
400+
}
401+
}
402+
403+
// Source generator generate this codes.
404+
public T(int value)
405+
{
406+
this.value = value;
407+
this.Normalize(ref this.value);
408+
}
409+
410+
private partial void Normalize(ref int value);
411+
```
412+
385413
### JsonConverter
386414

387415
Implements `System.Text.Json`'s `JsonConverter`. It will be used `JsonSerializer` automatically.

sandbox/ConsoleApp/AllPrimitives.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99

1010
namespace ConsoleApp
1111
{
12-
[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)]
12+
[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)]
1313
public readonly partial struct A
1414
{
15+
private partial void Normalize(ref int value)
16+
{
17+
value = Math.Clamp(value, 0, 100);
18+
}
19+
1520
private partial void Validate()
1621
{
1722
_ = AsPrimitive();

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.A.Generated.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override bool Equals(object obj)
4545
return value.Equals((int)obj);
4646
}
4747

48-
return value.Equals(obj);
48+
return value.Equals(obj);
4949
}
5050

5151
public override int GetHashCode()

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/UnitOfAttribute.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ internal enum UnitGenerateOptions
2727
{
2828
None = 0,
2929
ImplicitOperator = 1,
30-
ParseMethod = 2,
31-
MinMaxMethod = 4,
32-
ArithmeticOperator = 8,
33-
ValueArithmeticOperator = 16,
34-
Comparable = 32,
35-
Validate = 64,
36-
JsonConverter = 128,
37-
MessagePackFormatter = 256,
38-
DapperTypeHandler = 512,
39-
EntityFrameworkValueConverter = 1024,
40-
WithoutComparisonOperator = 2048,
41-
JsonConverterDictionaryKeySupport = 4096
30+
ParseMethod = 1 << 1,
31+
MinMaxMethod = 1 << 2,
32+
ArithmeticOperator = 1 << 3,
33+
ValueArithmeticOperator = 1 << 4,
34+
Comparable = 1 << 5,
35+
Validate = 1 << 6,
36+
JsonConverter = 1 << 7,
37+
MessagePackFormatter = 1 << 8,
38+
DapperTypeHandler = 1 << 9,
39+
EntityFrameworkValueConverter = 1 << 10,
40+
WithoutComparisonOperator = 1 << 11,
41+
JsonConverterDictionaryKeySupport = 1 << 12,
42+
Normalize = 1 << 13,
4243
}
4344
}

src/UnitGenerator/SourceGenerator.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,19 @@ internal enum UnitGenerateOptions
134134
{
135135
None = 0,
136136
ImplicitOperator = 1,
137-
ParseMethod = 2,
138-
MinMaxMethod = 4,
139-
ArithmeticOperator = 8,
140-
ValueArithmeticOperator = 16,
141-
Comparable = 32,
142-
Validate = 64,
143-
JsonConverter = 128,
144-
MessagePackFormatter = 256,
145-
DapperTypeHandler = 512,
146-
EntityFrameworkValueConverter = 1024,
147-
WithoutComparisonOperator = 2048,
148-
JsonConverterDictionaryKeySupport = 4096
137+
ParseMethod = 1 << 1,
138+
MinMaxMethod = 1 << 2,
139+
ArithmeticOperator = 1 << 3,
140+
ValueArithmeticOperator = 1 << 4,
141+
Comparable = 1 << 5,
142+
Validate = 1 << 6,
143+
JsonConverter = 1 << 7,
144+
MessagePackFormatter = 1 << 8,
145+
DapperTypeHandler = 1 << 9,
146+
EntityFrameworkValueConverter = 1 << 10,
147+
WithoutComparisonOperator = 1 << 11,
148+
JsonConverterDictionaryKeySupport = 1 << 12,
149+
Normalize = 1 << 13,
149150
}
150151
}
151152
""";
@@ -218,6 +219,12 @@ namespace {{ns}}
218219
{
219220
this.value = value;
220221
""");
222+
if (prop.HasFlag(UnitGenerateOptions.Normalize))
223+
{
224+
sb.AppendLine("""
225+
this.Normalize(ref this.value);
226+
""");
227+
}
221228
if (prop.HasFlag(UnitGenerateOptions.Validate))
222229
{
223230
sb.AppendLine("""
@@ -228,7 +235,14 @@ namespace {{ns}}
228235
}
229236
230237
""");
231-
238+
if (prop.HasFlag(UnitGenerateOptions.Normalize))
239+
{
240+
sb.AppendLine($$"""
241+
private partial void Normalize(ref {{innerTypeName}} value);
242+
243+
""");
244+
245+
}
232246
if (prop.HasFlag(UnitGenerateOptions.Validate))
233247
{
234248
sb.AppendLine($$"""

src/UnitGenerator/UnitGenerateOptions.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ internal enum UnitGenerateOptions
88
{
99
None = 0,
1010
ImplicitOperator = 1,
11-
ParseMethod = 2,
12-
MinMaxMethod = 4,
13-
ArithmeticOperator = 8,
14-
ValueArithmeticOperator = 16,
15-
Comparable = 32,
16-
Validate = 64,
17-
JsonConverter = 128,
18-
MessagePackFormatter = 256,
19-
DapperTypeHandler = 512,
20-
EntityFrameworkValueConverter = 1024,
21-
WithoutComparisonOperator = 2048,
22-
JsonConverterDictionaryKeySupport = 4096
11+
ParseMethod = 1 << 1,
12+
MinMaxMethod = 1 << 2,
13+
ArithmeticOperator = 1 << 3,
14+
ValueArithmeticOperator = 1 << 4,
15+
Comparable = 1 << 5,
16+
Validate = 1 << 6,
17+
JsonConverter = 1 << 7,
18+
MessagePackFormatter = 1 << 8,
19+
DapperTypeHandler = 1 << 9,
20+
EntityFrameworkValueConverter = 1 << 10,
21+
WithoutComparisonOperator = 1 << 11,
22+
JsonConverterDictionaryKeySupport = 1 << 12,
23+
Normalize = 1 << 13,
2324
}
24-
}
25+
}

0 commit comments

Comments
 (0)