Skip to content

Commit 107de83

Browse files
committed
0.8.0
1 parent e28cf8e commit 107de83

22 files changed

+850
-28
lines changed

CHANGELOG.CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.8.0
2+
- 可区分联合生成器现在支持生成 JsonConverter
3+
14
# 0.7.0
25
- 添加了解引用生成器
36

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.8.0
2+
- Union Generator not support gen JsonConverter
3+
14
# 0.7.0
25
- Add Deref Generator
36

LibSugar.CodeGen/LibSugar.CodeGen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.7.3</Version>
5+
<Version>0.8.0</Version>
66
<Nullable>enable</Nullable>
77
<LangVersion>11.0</LangVersion>
88
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>

LibSugar.CodeGen/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"LibSugar.CodeGen": {
44
"commandName": "DebugRoslynComponent",
5-
"targetProject": "..\\TestGen\\TestGen.csproj"
5+
"targetProject": "..\\TestGen2\\TestGen2.csproj"
66
}
77
}
88
}

LibSugar.CodeGen/UnionGenerator.cs

Lines changed: 401 additions & 14 deletions
Large diffs are not rendered by default.

LibSugar.CodeGen/Utils.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,15 @@ public static void LogWarning(this SourceProductionContext context, string msg,
449449
context.ReportDiagnostic(Diagnostic.Create(new DiagnosticDescriptor("Sugar", "", msg, "", DiagnosticSeverity.Warning, true), loc, DiagnosticSeverity.Warning));
450450
}
451451
}
452+
453+
public static class UtilsClass
454+
{
455+
public static V? TryGet<K, V>(this Dictionary<K, V> self, K key) where V : class where K : notnull =>
456+
self.TryGetValue(key, out var val) ? val : null;
457+
}
458+
459+
public static class UtilsStruct
460+
{
461+
public static V? TryGet<K, V>(this Dictionary<K, V> self, K key) where V : struct where K : notnull =>
462+
self.TryGetValue(key, out var val) ? val : null;
463+
}

LibSugar.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibSugar.CodeGen", "LibSugar.CodeGen\LibSugar.CodeGen.csproj", "{C53AEA48-A9EE-4A2E-B294-B5B681A9D616}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestGen", "TestGen\TestGen.csproj", "{AA832EEF-9ED1-479A-88C6-561D13AFD5E4}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestGen", "TestGen\TestGen.csproj", "{AA832EEF-9ED1-479A-88C6-561D13AFD5E4}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestGen2", "TestGen2\TestGen2.csproj", "{C7BFB8AF-A751-402D-B488-30C190E8F160}"
1315
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -42,6 +44,12 @@ Global
4244
{AA832EEF-9ED1-479A-88C6-561D13AFD5E4}.Release|Any CPU.Build.0 = Release|Any CPU
4345
{AA832EEF-9ED1-479A-88C6-561D13AFD5E4}.Unity|Any CPU.ActiveCfg = Debug|Any CPU
4446
{AA832EEF-9ED1-479A-88C6-561D13AFD5E4}.Unity|Any CPU.Build.0 = Debug|Any CPU
47+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Unity|Any CPU.ActiveCfg = Debug|Any CPU
52+
{C7BFB8AF-A751-402D-B488-30C190E8F160}.Unity|Any CPU.Build.0 = Debug|Any CPU
4553
EndGlobalSection
4654
GlobalSection(SolutionProperties) = preSolution
4755
HideSolutionNode = FALSE

LibSugar/EqBy.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LibSugar;
6+
7+
public static partial class Sugar
8+
{
9+
public static EqByEqualityComparer<T, D> EqBy<T, D>(this Func<T, D> f) => new(f);
10+
public static EqByEqualityComparer<T, D> EqBy<T, D>(this Func<T, D> f, IEqualityComparer<D> equality) => new(f, equality);
11+
public static EqByEqualityComparer<T, D> EqBy<T, D>(this IEqualityComparer<D> equality, Func<T, D> f) => new(f, equality);
12+
}
13+
14+
public record EqByEqualityComparer<T, D>(Func<T, D> F, IEqualityComparer<D> Equality) : IEqualityComparer<T>
15+
{
16+
public EqByEqualityComparer(Func<T, D> f) : this(f, EqualityComparer<D>.Default) { }
17+
18+
public bool Equals(T? x, T? y) => Equality.Equals(F(x!), F(y!));
19+
20+
public int GetHashCode(T obj) => Equality.GetHashCode(F(obj)!);
21+
}

LibSugar/LibSugar.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Configurations>Debug;Release;Unity</Configurations>
55
<TargetFrameworks>netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
6-
<Version>0.7.3</Version>
6+
<Version>0.8.0</Version>
77
<Nullable>enable</Nullable>
88
<LangVersion>11.0</LangVersion>
99
<PackageProjectUrl>https://github.com/libsugar/SugarCs</PackageProjectUrl>

LibSugar/Seq.cs

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Runtime.CompilerServices;
6-
using System.Text;
7-
using System.Threading.Tasks;
86

97
namespace LibSugar;
108

@@ -17,6 +15,7 @@ public static partial class Sugar
1715
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1816
public static IEnumerable<T> Repeat<T>(this T v, int count) => Enumerable.Repeat(v, count);
1917

18+
2019
public static string JoinStr<T>(this IEnumerable<T> iter) => string.Join(", ", iter);
2120
public static string JoinStr<T>(this IEnumerable<T> iter, char separator) => string.Join(separator, iter);
2221
public static string JoinStr<T>(this IEnumerable<T> iter, string separator) => string.Join(separator, iter);
@@ -27,10 +26,12 @@ public static partial class Sugar
2726
public static string JoinStr(this string[] arr, char separator) => string.Join(separator, arr);
2827
public static string JoinStr(this string[] arr, string separator) => string.Join(separator, arr);
2928

29+
3030
public static IEnumerable<(T a, int i)> Indexed<T>(this IEnumerable<T> iter) => iter.Select((a, b) => (a, b));
3131

3232
public static ParallelQuery<(T a, int i)> Indexed<T>(this ParallelQuery<T> iter) => iter.Select((a, b) => (a, b));
3333

34+
3435
public static IEnumerable<R> WhereSelect<T, R>(this IEnumerable<T> iter, Func<T, Option<R>> f)
3536
=> iter.Select(f).Where(a => a.Has).Select(a => a.Value);
3637

@@ -51,6 +52,7 @@ public static IEnumerable<R> WhereCast<R>(this IEnumerable iter)
5152
public static ParallelQuery<R> WhereCast<T, R>(this ParallelQuery<T> iter)
5253
=> iter.Where(a => a is R).Cast<R>();
5354

55+
5456
/// <summary>ForEach for <see cref="IEnumerable&lt;T&gt;"/></summary>
5557
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5658
public static void ForEach<T>(this IEnumerable<T> iter, Action<T> f)
@@ -91,4 +93,112 @@ public static Option<R> ForEach<T, R>(this IEnumerable<T> iter, Func<T, Option<R
9193
}
9294
return new();
9395
}
96+
97+
#region IEnumerable ByX
98+
99+
#if !NET6_0_OR_GREATER
100+
101+
public static IEnumerable<T> DistinctBy<T, D>(this IEnumerable<T> iter, Func<T, D> f)
102+
=> iter.Distinct(EqBy(f));
103+
104+
public static IEnumerable<T> DistinctBy<T, D>(this IEnumerable<T> iter, IEqualityComparer<D> equality, Func<T, D> f)
105+
=> iter.Distinct(EqBy(f, equality));
106+
107+
public static IEnumerable<T> DistinctBy<T, D>(this IEnumerable<T> iter, Func<T, D> f, IEqualityComparer<D> equality)
108+
=> iter.Distinct(EqBy(f, equality));
109+
110+
111+
public static IEnumerable<T> UnionBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f)
112+
=> iter.Union(other, EqBy(f));
113+
114+
public static IEnumerable<T> UnionBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, IEqualityComparer<D> equality, Func<T, D> f)
115+
=> iter.Union(other, EqBy(f, equality));
116+
117+
public static IEnumerable<T> UnionBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f, IEqualityComparer<D> equality)
118+
=> iter.Union(other, EqBy(f, equality));
119+
120+
#else
121+
122+
public static IEnumerable<T> DistinctBy<T, D>(this IEnumerable<T> iter, IEqualityComparer<D> equality, Func<T, D> f)
123+
=> iter.DistinctBy(f, equality);
124+
125+
126+
public static IEnumerable<T> ExceptBy<T, D>(this IEnumerable<T> iter, IEnumerable<D> other, IEqualityComparer<D> equality, Func<T, D> f)
127+
=> iter.ExceptBy(other, f, equality);
128+
129+
130+
public static IEnumerable<T> IntersectBy<T, D>(this IEnumerable<T> iter, IEnumerable<D> other, IEqualityComparer<D> equality, Func<T, D> f)
131+
=> iter.IntersectBy(other, f, equality);
132+
133+
134+
public static IEnumerable<T> UnionBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, IEqualityComparer<D> equality, Func<T, D> f)
135+
=> iter.UnionBy(other, f, equality);
136+
137+
#endif
138+
139+
public static IEnumerable<T> ExceptBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f)
140+
=> iter.Except(other, EqBy(f));
141+
142+
public static IEnumerable<T> ExceptBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, IEqualityComparer<D> equality, Func<T, D> f)
143+
=> iter.Except(other, EqBy(f, equality));
144+
145+
public static IEnumerable<T> ExceptBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f, IEqualityComparer<D> equality)
146+
=> iter.Except(other, EqBy(f, equality));
147+
148+
149+
public static IEnumerable<T> IntersectBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f)
150+
=> iter.Intersect(other, EqBy(f));
151+
152+
public static IEnumerable<T> IntersectBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, IEqualityComparer<D> equality, Func<T, D> f)
153+
=> iter.Intersect(other, EqBy(f, equality));
154+
155+
public static IEnumerable<T> IntersectBy<T, D>(this IEnumerable<T> iter, IEnumerable<T> other, Func<T, D> f, IEqualityComparer<D> equality)
156+
=> iter.Intersect(other, EqBy(f, equality));
157+
158+
#endregion
159+
160+
161+
#region ParallelQuery ByX
162+
163+
public static ParallelQuery<T> DistinctBy<T, D>(this ParallelQuery<T> iter, Func<T, D> f)
164+
=> iter.Distinct(EqBy(f));
165+
166+
public static ParallelQuery<T> DistinctBy<T, D>(this ParallelQuery<T> iter, IEqualityComparer<D> equality, Func<T, D> f)
167+
=> iter.Distinct(EqBy(f, equality));
168+
169+
public static ParallelQuery<T> DistinctBy<T, D>(this ParallelQuery<T> iter, Func<T, D> f, IEqualityComparer<D> equality)
170+
=> iter.Distinct(EqBy(f, equality));
171+
172+
173+
public static ParallelQuery<T> ExceptBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f)
174+
=> iter.Except(other, EqBy(f));
175+
176+
public static ParallelQuery<T> ExceptBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, IEqualityComparer<D> equality, Func<T, D> f)
177+
=> iter.Except(other, EqBy(f, equality));
178+
179+
public static ParallelQuery<T> ExceptBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f, IEqualityComparer<D> equality)
180+
=> iter.Except(other, EqBy(f, equality));
181+
182+
183+
public static ParallelQuery<T> IntersectBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f)
184+
=> iter.Intersect(other, EqBy(f));
185+
186+
public static ParallelQuery<T> IntersectBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, IEqualityComparer<D> equality, Func<T, D> f)
187+
=> iter.Intersect(other, EqBy(f, equality));
188+
189+
public static ParallelQuery<T> IntersectBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f, IEqualityComparer<D> equality)
190+
=> iter.Intersect(other, EqBy(f, equality));
191+
192+
193+
public static ParallelQuery<T> UnionBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f)
194+
=> iter.Union(other, EqBy(f));
195+
196+
public static ParallelQuery<T> UnionBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, IEqualityComparer<D> equality, Func<T, D> f)
197+
=> iter.Union(other, EqBy(f, equality));
198+
199+
public static ParallelQuery<T> UnionBy<T, D>(this ParallelQuery<T> iter, ParallelQuery<T> other, Func<T, D> f, IEqualityComparer<D> equality)
200+
=> iter.Union(other, EqBy(f, equality));
201+
202+
#endregion
203+
94204
}

LibSugar/UResult.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ public static UResult<T, A> MakeOk<T>(T v) where T : unmanaged
2222
}
2323

2424
[Union, For("T", "E")]
25+
#if !NETSTANDARD
26+
[UnionJson]
27+
#endif
2528
public enum UResultKind : byte
2629
{
27-
[Of("E")]
30+
[Of("E"), UnionJsonName("err")]
2831
Err,
29-
[Of("T")]
32+
[Of("T"), UnionJsonName("ok")]
3033
Ok,
3134
}
3235

LibSugar/UnionAttribute.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
24

35
namespace LibSugar;
46

@@ -18,3 +20,97 @@ public UnionAttribute() { }
1820

1921
public UnionAttribute(string name) => Name = name;
2022
}
23+
24+
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class | AttributeTargets.Struct)]
25+
public class UnionJsonAttribute : Attribute
26+
{
27+
#if NETSTANDARD
28+
/// <summary>
29+
/// Generate System.Text.Json.Serialization.JsonConverter
30+
/// </summary>
31+
#else
32+
/// <summary>
33+
/// Generate <see cref="System.Text.Json.Serialization.JsonConverter{T}"/>
34+
/// </summary>
35+
#endif
36+
public bool WithSystemText { get; set; } = true;
37+
38+
/// <summary>
39+
/// Converter class name for System.Text.Json
40+
/// </summary>
41+
public string? SystemTextClassName { get; set; }
42+
43+
/// <summary>
44+
/// How to serialize and deserialize json
45+
/// </summary>
46+
public UnionJsonMode JsonMode { get; set; } = UnionJsonMode.External;
47+
48+
/// <summary>
49+
/// Tag name when <see cref="JsonMode"/> is <see cref="UnionJsonMode.Adjacent"/>
50+
/// </summary>
51+
public string Tag { get; set; } = "t";
52+
/// <summary>
53+
/// Content name when <see cref="JsonMode"/> is <see cref="UnionJsonMode.Adjacent"/>
54+
/// </summary>
55+
public string Content { get; set; } = "c";
56+
57+
/// <summary>
58+
/// Use enum value as tag
59+
/// </summary>
60+
public bool NumberTag { get; set; } = false;
61+
}
62+
63+
/// <summary>
64+
/// How to serialize and deserialize json
65+
/// </summary>
66+
public enum UnionJsonMode
67+
{
68+
/// <summary>
69+
/// <code>
70+
/// { "Tag": { "a": 1 } }
71+
/// </code>
72+
/// </summary>
73+
External,
74+
/// <summary>
75+
/// <code>
76+
/// ["Tag", { "a": 1 }]
77+
/// </code>
78+
/// </summary>
79+
Tuple,
80+
/// <summary>
81+
/// <code>
82+
/// { "t": "Tag", "c": { "a": 1 } }
83+
/// </code>
84+
/// <para>
85+
/// For System.Text.Json: <br/>
86+
/// This object needs to be ordered, "t" must come before "c"
87+
/// </para>
88+
/// </summary>
89+
Adjacent,
90+
}
91+
92+
public record struct UnionMeta<K> where K : Enum
93+
{
94+
public UnionMeta(Dictionary<string, K> nameToKind, Dictionary<K, string> kindToName, Dictionary<K, Type> variantTypes, Dictionary<string, K> jsonNameToKind)
95+
{
96+
NameToKind = new(nameToKind);
97+
KindToName = new(kindToName);
98+
VariantTypes = new(variantTypes);
99+
JsonNameToKind = new(jsonNameToKind);
100+
}
101+
public ReadOnlyDictionary<string, K> NameToKind { get; }
102+
public ReadOnlyDictionary<K, string> KindToName { get; }
103+
public ReadOnlyDictionary<K, Type> VariantTypes { get; }
104+
public ReadOnlyDictionary<string, K> JsonNameToKind { get; }
105+
}
106+
107+
[AttributeUsage(AttributeTargets.Field)]
108+
public class UnionJsonNameAttribute : Attribute
109+
{
110+
public string Name { get; }
111+
112+
public UnionJsonNameAttribute(string name)
113+
{
114+
Name = name;
115+
}
116+
}

Runtime/LibSugar.CodeGen.dll

20.5 KB
Binary file not shown.

Runtime/LibSugar.CodeGen.pdb

2.48 KB
Binary file not shown.

Runtime/LibSugar.dll

8.5 KB
Binary file not shown.

Runtime/LibSugar.pdb

1.84 KB
Binary file not shown.

0 commit comments

Comments
 (0)