Skip to content

Commit 5c8bdfd

Browse files
committed
v0.9.0: extensibility via TonClient.RegisterAssembly
1 parent ddee4a2 commit 5c8bdfd

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using TonLibDotNet.Requests;
2+
using TonLibDotNet.Types;
3+
using TonLibDotNet.Utils;
4+
5+
namespace TonLibDotNet
6+
{
7+
[TLSchema("hello.world = hello.World")]
8+
public class ExtensibilityDemoRequest : RequestBase<Ok>
9+
{
10+
public string Continent { get; set; } = string.Empty;
11+
}
12+
}

TonLibDotNet.Demo/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public static async Task Main(string[] args)
3636
services.AddSingleton<ITonClient, TonClient>();
3737
});
3838

39+
/// Add types from current assembly (see <see cref="ExtensibilityDemoRequest"/> class and <see cref="RunExtensibilityDemo(ITonClient)" below />).
40+
TonClient.RegisterAssembly(typeof(Program).Assembly);
41+
3942
var app = builder.Build();
4043

4144
var logger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger(nameof(Program));
@@ -59,6 +62,8 @@ public static async Task Main(string[] args)
5962
await RunSendDemo(tonClient, logger, TestnetAccountToSendFromAddress, TestnetAccountToSendFromMnemonic);
6063
}
6164

65+
await RunExtensibilityDemo(tonClient, logger);
66+
6267
// Loggers need some time to flush data to screen/console.
6368
await Task.Delay(TimeSpan.FromSeconds(1));
6469
}
@@ -170,5 +175,21 @@ private static async Task RunSendDemo(ITonClient tonClient, ILogger logger, stri
170175
// Send it to network. You dont have TX id or something in respnse - just poll getTransactions() for your account and wait for new TX.
171176
_ = await tonClient.QuerySend(query.Id);
172177
}
178+
179+
private static async Task RunExtensibilityDemo(ITonClient tonClient, ILogger logger)
180+
{
181+
// This will fail because TonLib does not have this method.
182+
// This is just a demo how you can add new types/requests without waiting for new package release.
183+
// To make this happen in your app - call TonClient.RegisterAssembly() early
184+
try
185+
{
186+
await tonClient.Execute(new ExtensibilityDemoRequest() { Continent = "Antarctica" });
187+
}
188+
catch (TonClientException ex)
189+
{
190+
logger.LogWarning(ex, "Exception ignored");
191+
// Ignore
192+
}
193+
}
173194
}
174195
}

TonLibDotNet/TonClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public TonClient(ILogger<TonClient> logger, Microsoft.Extensions.Options.IOption
5050

5151
public OptionsInfo OptionsInfo { get; private set; }
5252

53+
/// <summary>
54+
/// Add assembly with additional <see cref="TypeBase"/> classes for LiteServer interaction.
55+
/// </summary>
56+
/// <param name="assembly">Assembly to add</param>
57+
public static void RegisterAssembly(System.Reflection.Assembly assembly)
58+
{
59+
TonLibDotNet.Utils.Json.TonTypeResolver.AdditionalAsseblies.Add(assembly);
60+
}
61+
5362
public async Task<OptionsInfo?> InitIfNeeded()
5463
{
5564
if (needReinit)

TonLibDotNet/TonLibDotNet.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>ton, tonlib, libtonlibjson</PackageTags>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
19-
<Version>0.8.0</Version>
20-
<PackageReleaseNotes>Added methods sending TON.</PackageReleaseNotes>
19+
<Version>0.9.0</Version>
20+
<PackageReleaseNotes>Extensibility: register new types/requests from other assemblies.</PackageReleaseNotes>
2121
<Description>TonLib (libtonlibjson) wrapper for accessing Telegram Open Network lite servers (nodes) via ADNL protocol.</Description>
2222
</PropertyGroup>
2323

TonLibDotNet/Utils/Json/TonTypeResolver.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace TonLibDotNet.Utils.Json
88
// Based on https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0#configure-polymorphism-with-the-contract-model
99
public class TonTypeResolver : DefaultJsonTypeInfoResolver
1010
{
11+
public static List<Assembly> AdditionalAsseblies { get; } = new List<Assembly>();
12+
1113
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
1214
{
1315
var jsonTypeInfo = base.GetTypeInfo(type, options);
@@ -29,13 +31,14 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
2931

3032
protected void AppendTypes(Type type, IList<JsonDerivedType> list)
3133
{
32-
var types = GetType().Assembly.GetExportedTypes()
33-
.Where(x => x.IsAssignableTo(type) && !x.IsAbstract)
34-
.Select(x => new
35-
{
36-
type = x,
37-
schema = x.GetCustomAttribute<TLSchemaAttribute>(false),
38-
});
34+
var types = AdditionalAsseblies.Append(GetType().Assembly)
35+
.SelectMany(a => a.GetExportedTypes()
36+
.Where(x => x.IsAssignableTo(type) && !x.IsAbstract)
37+
.Select(x => new
38+
{
39+
type = x,
40+
schema = x.GetCustomAttribute<TLSchemaAttribute>(false),
41+
}));
3942

4043
foreach (var item in types.Where(x => x.schema != null))
4144
{

0 commit comments

Comments
 (0)