Skip to content

Commit 00c2e3f

Browse files
committed
IncrementalGeneraotr and various fixes
1 parent 70c5af9 commit 00c2e3f

File tree

6 files changed

+604
-622
lines changed

6 files changed

+604
-622
lines changed

README.md

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ NuGet: [UnitGenerator](https://www.nuget.org/packages/UnitGenerator)
1010
Install-Package UnitGenerator
1111
```
1212

13+
Execute in Unity Game Engine is also supported, please see the [Unity](#use-for-unity) section for details.
14+
1315
## Introduction
1416

1517
For example, Identifier, UserId is comparable only to UserId, and cannot be assigned to any other type. Also, arithmetic operations are not allowed.
@@ -18,16 +20,15 @@ For example, Identifier, UserId is comparable only to UserId, and cannot be assi
1820
using UnitGenerator;
1921

2022
[UnitOf(typeof(int))]
21-
public readonly partial struct UserId { }
23+
public readonly partial struct UserId; { }
2224
```
2325

2426
or when using C#11 and NET7 you can use
2527

2628
```csharp
2729
using UnitGenerator;
2830

29-
[UnitOf<int>]
30-
public readonly partial struct UserId { }
31+
[UnitOf<int>] public readonly partial struct UserId;
3132
```
3233

3334
will generates
@@ -63,8 +64,8 @@ public readonly partial struct UserId : IEquatable<UserId>
6364
However, Hp in games, should not be allowed to be assigned to other types, but should support arithmetic operations with int. For example double heal = `target.Hp = Hp.Min(target.Hp * 2, target.MaxHp)`.
6465

6566
```csharp
66-
[UnitOf(typeof(int), UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod)]
67-
public readonly partial struct Hp { }
67+
[UnitOf<int>(UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod)]
68+
public readonly partial struct Hp;
6869

6970
// -- generates
7071
@@ -162,11 +163,11 @@ enum UnitGenerateOptions
162163

163164
UnitGenerateOptions has some serializer support. For example, a result like `Serialize(userId) => { Value = 1111 }` is awful. The value-object should be serialized natively, i.e. `Serialize(useId) => 1111`, and should be able to be added directly to a database, etc.
164165

165-
Currently UnitGenerator supports [MessagePack for C#](https://github.com/neuecc/MessagePack-CSharp), System.Text.Json(JsonSerializer), [Dapper](https://github.com/StackExchange/Dapper) and EntityFrameworkCore.
166+
Currently UnitGenerator supports [MessagePack for C#](https://github.com/MessagePack-CSharp/MessagePack-CSharp), System.Text.Json(JsonSerializer), [Dapper](https://github.com/StackExchange/Dapper) and EntityFrameworkCore.
166167

167168
```csharp
168-
[UnitOf(typeof(int), UnitGenerateOptions.MessagePackFormatter)]
169-
public readonly partial struct UserId { }
169+
[UnitOf<int>(UnitGenerateOptions.MessagePackFormatter)]
170+
public readonly partial struct UserId;
170171

171172
// -- generates
172173
@@ -556,41 +557,11 @@ builder.HasConversion(new UserId.UserIdValueConverter());
556557

557558
## Use for Unity
558559

559-
C# Source Generator feature is rely on C# 9.0. If you are using Unity 2021.2, that supports [Source Generators](https://docs.unity3d.com/2021.2/Documentation/Manual/roslyn-analyzers.html). Add the `UnitGenerator.dll` from the [releases page](https://github.com/Cysharp/UnitGenerator/releases), disable Any Platform, disable Include all platforms and set label as `RoslynAnalyzer`.
560-
561-
It works in Unity Editor however does not work on IDE because Unity does not generate analyzer reference to `.csproj`. We provides [CsprojModifer](https://github.com/Cysharp/CsprojModifier) to analyzer support, uses `Add analyzer references to generated .csproj` supports both IDE and Unity Editor.
562-
563-
Unity(2020) does not support C# 9.0 so can not use directly. However, C# Source Genertor supports output source as file.
564-
565-
1. Create `UnitSourceGen.csproj`.
566-
567-
```xml
568-
<Project Sdk="Microsoft.NET.Sdk">
569-
<PropertyGroup>
570-
<TargetFramework>net5.0</TargetFramework>
560+
Minimum supported Unity version is `2022.3.12f1`.
571561

572-
<!-- add this two lines and configure output path -->
573-
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
574-
<CompilerGeneratedFilesOutputPath>$(ProjectDir)..\Generated</CompilerGeneratedFilesOutputPath>
575-
</PropertyGroup>
576-
577-
<ItemGroup>
578-
<!-- reference UnitGenerator -->
579-
<PackageReference Include="UnitGenerator" Version="1.0.0" />
580-
581-
<!-- add target sources path from Unity -->
582-
<Compile Include="..\MyUnity\Assets\Scripts\Models\**\*.cs" />
583-
</ItemGroup>
584-
</Project>
585-
```
586-
587-
2. install [.NET SDK](https://dotnet.microsoft.com/download) and run this command.
588-
589-
```
590-
dotnet build UnitSourceGen.csproj
591-
```
562+
The easiest way is to install `UnitGenerator` from NuGet using [NuGetForUnity](https://github.com/GlitchEnzo/NuGetForUnity).
592563
593-
File will be generated under `UnitGenerator\UnitGenerator.SourceGenerator\*.Generated.cs`. `UnitOfAttribute` is also included in generated folder, so at first, run build command and get attribute to configure.
564+
Alternatively, you can download `UnitGenerator.dll` from the [releases page](https://github.com/Cysharp/UnitGenerator/releases) page and set it up as a `RoslynAnalyzer` to make it work.
594565
595566
License
596567
---

sandbox/ConsoleApp/AllPrimitives.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Globalization;
45
using System.Linq;
56
using System.Numerics;
@@ -11,14 +12,17 @@
1112

1213
namespace ConsoleApp
1314
{
15+
16+
17+
1418
[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)]
1519
public readonly partial struct A
1620
{
1721
private partial void Normalize(ref int value)
1822
{
1923
value = Math.Clamp(value, 0, 100);
2024
}
21-
25+
2226
private partial void Validate()
2327
{
2428
_ = AsPrimitive();

sandbox/ConsoleApp/Program.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using Sample;
1+
//using Sample;
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
67
using System.Text;
78
using System.Text.Json;
@@ -19,12 +20,40 @@
1920

2021
Console.WriteLine(json);
2122

23+
24+
25+
26+
27+
[UnitOf<int>] public readonly partial struct MyId;
28+
29+
30+
public readonly struct MyParsable : IParsable<MyParsable>
31+
{
32+
public static MyParsable Parse(string s)
33+
=> throw new NotImplementedException();
34+
35+
public static bool TryParse([NotNullWhen(true)] string? s, [MaybeNullWhen(false)] out MyParsable result)
36+
=> throw new NotImplementedException();
37+
38+
public static MyParsable Parse(string s, IFormatProvider? provider)
39+
=> throw new NotImplementedException();
40+
41+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out MyParsable result)
42+
=> throw new NotImplementedException();
43+
}
44+
45+
[UnitOf(typeof(MyParsable), UnitGenerateOptions.ParseMethod)]
46+
public readonly partial struct StructInOtherLib
47+
{
48+
public static void Test()
49+
=> StructInOtherLib.Parse("");
50+
}
51+
2252
[UnitOf(typeof(ulong), UnitGenerateOptions.ArithmeticOperator)]
2353
public readonly partial struct Money
2454
{
2555
}
2656

27-
2857
[UnitOf(typeof(int))]
2958
public readonly partial struct NoNamespace
3059
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace UnitGenerator;
4+
5+
public readonly struct IgnoreEquality<T>(T value) : IEquatable<IgnoreEquality<T>>
6+
{
7+
public readonly T Value => value;
8+
9+
public static implicit operator IgnoreEquality<T>(T value)
10+
{
11+
return new IgnoreEquality<T>(value);
12+
}
13+
14+
public static implicit operator T(IgnoreEquality<T> value)
15+
{
16+
return value.Value;
17+
}
18+
19+
public bool Equals(IgnoreEquality<T> other)
20+
{
21+
// always true to ignore equality check.
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)