Skip to content

Commit f5a68a7

Browse files
authored
Add an easy way to register proxies (#321)
We already have a mechanism for selecting proxies -- SerdeTypeOptions or SerdeMemberOptions. However, there are two downsides. First, the proxy is meant to match the type of the member exactly. This can get very verbose if the only thing that's interesting in the proxy is nested inside a type substitution. Second, there's no way to automatically use a proxy for all members in a type. The new `UseProxy` feature adds an easy way to pick out individual proxies and reduce boilerplate. Thanks to @Deficuet for the initial implementation here.
2 parents 3bcde47 + 3730d57 commit f5a68a7

33 files changed

+1138
-64
lines changed

src/generator/DeserializeImplGen.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
25
using System;
36
using System.Collections.Generic;
47
using System.Collections.Immutable;
58
using System.Diagnostics;
69
using System.Linq;
710
using System.Text;
8-
using Microsoft.CodeAnalysis;
9-
using Microsoft.CodeAnalysis.CSharp;
10-
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1212
using static Serde.Diagnostics;
1313
using static Serde.SerdeImplRoslynGenerator;
@@ -200,6 +200,8 @@ private static SourceBuilder GenerateCustomDeserializeMethod(
200200
{
201201
Debug.Assert(type.TypeKind != TypeKind.Enum);
202202

203+
var classScopeProxyMap = ProxyMap.FromSymbol(type);
204+
203205
var members = SymbolUtilities.GetDataMembers(type, SerdeUsage.Both);
204206
var typeFqn = typeSyntax.ToString();
205207
var assignedVarType = members.Count switch {
@@ -267,7 +269,10 @@ private static SourceBuilder GenerateCustomDeserializeMethod(
267269
? "ReadValue"
268270
: "ReadBoxedValue";
269271
string readValueCall;
270-
if (Proxies.TryGetExplicitWrapper(m, context, SerdeUsage.Deserialize, inProgress) is { } explicitWrap)
272+
273+
var proxyContext = ProxyContext.Create(classScopeProxyMap, ProxyMap.FromSymbol(m.Symbol));
274+
275+
if (Proxies.TryGetExplicitWrapper(m, context, SerdeUsage.Deserialize, inProgress, proxyContext) is { } explicitWrap)
271276
{
272277
readValueCall = $"{readMethodName}<{memberType}, {explicitWrap}>";
273278
}
@@ -279,7 +284,7 @@ private static SourceBuilder GenerateCustomDeserializeMethod(
279284
{
280285
readValueCall = $"Read{primitiveName}";
281286
}
282-
else if (Proxies.TryGetImplicitWrapper(m.Type, context, SerdeUsage.Deserialize, inProgress) is { Proxy: { } wrap })
287+
else if (Proxies.TryGetImplicitWrapper(m.Type, context, SerdeUsage.Deserialize, inProgress, proxyContext) is { Proxy: { } wrap })
283288
{
284289
if (wrap == "global::Serde.GuidProxy")
285290
{

0 commit comments

Comments
 (0)