Skip to content

Commit

Permalink
listener v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Nov 27, 2024
1 parent c003c76 commit c7c42dc
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 222 deletions.
4 changes: 2 additions & 2 deletions net/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageVersion Include="MessagePack" Version="2.5.192" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<!-- Examples -->
<PackageVersion Include="Sails.ClientGenerator" Version="0.0.1-beta.2" />
<PackageVersion Include="Sails.Net" Version="0.0.1-beta.2" />
<PackageVersion Include="Sails.ClientGenerator" Version="0.0.1-beta.3" />
<PackageVersion Include="Sails.Net" Version="0.0.1-beta.3" />
</ItemGroup>
</Project>
21 changes: 10 additions & 11 deletions net/rs/client-gen/src/ctor_generators.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{helpers::*, type_decl_generators::*};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::prelude::*;
use sails_idl_parser::{ast::visitor, ast::visitor::Visitor, ast::*};
Expand All @@ -22,20 +21,21 @@ impl<'a> CtorFactoryGenerator<'a> {
}

pub(crate) fn finalize(self) -> Tokens {
let class_name = format!("{}Factory", self.service_name);
let class_name = &format!("{}Factory", self.service_name);
let remoting = &csharp::import("global::Sails.Remoting.Abstractions.Core", "IRemoting");

quote! {
public interface I$(&class_name)$['\r']
public interface I$class_name$['\r']
{
$(self.interface_tokens)
}
$['\n']
public sealed partial class $(&class_name) : I$(&class_name)$['\r']
{$['\n']
public sealed partial class $class_name : I$class_name$['\r']
{
$['\n']
private readonly $remoting remoting;
$['\n']
public $(&class_name)($remoting remoting)$['\r']
public $class_name($remoting remoting)$['\r']
{
this.remoting = remoting;
}
Expand All @@ -53,13 +53,12 @@ impl<'a> Visitor<'a> for CtorFactoryGenerator<'a> {
}

fn visit_ctor_func(&mut self, func: &'a CtorFunc) {
let func_name_pascal = &func.name().to_case(Case::Pascal);
let func_name = func.name();

self.interface_tokens.push();
self.interface_tokens.append(summary_comment(func.docs()));
self.interface_tokens.push();

let route_bytes = &path_bytes(func.name()).0;
let args = &encoded_fn_args_comma_prefixed(func.params());
let args_with_type = &self.type_generator.fn_params_with_types(func.params());
let void_type = primitive_type_to_dotnet(PrimitiveType::Null);
Expand All @@ -68,14 +67,14 @@ impl<'a> Visitor<'a> for CtorFactoryGenerator<'a> {
let action = &csharp::import("global::Sails.Remoting", "RemotingAction");

quote_in! { self.interface_tokens =>
$activation $func_name_pascal($args_with_type);$['\r']
$activation $func_name($args_with_type);$['\r']
};

quote_in! { self.class_tokens =>
$(inheritdoc())
public $activation $func_name_pascal($args_with_type)
public $activation $func_name($args_with_type)
{
return new $action<$(void_type)>(this.remoting, [$route_bytes]$args);
return new $action<$(void_type)>(this.remoting, nameof($func_name), string.Empty $args);
}
$['\n']
};
Expand Down
18 changes: 11 additions & 7 deletions net/rs/client-gen/src/events_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ impl<'a> EventsGenerator<'a> {
let class_name = &format!("Enum{}Events", name);
let listener_name = &format!("{}Listener", name);

let core_listener = &csharp::import(
"global::Sails.Remoting.Abstractions.Core",
"IRemotingListener",
);
let remoting = &csharp::import("global::Sails.Remoting.Abstractions.Core", "IRemoting");
let task = &csharp::import("global::System.Threading.Tasks", "Task");
let cancellation_token = &csharp::import("global::System.Threading", "CancellationToken");
let async_enumerable =
Expand All @@ -53,7 +50,7 @@ impl<'a> EventsGenerator<'a> {
}
}
$['\n']
public static class $listener_name
public sealed partial class $listener_name
{
$['\n']
private const string ROUTE = $(quoted(name));
Expand All @@ -63,9 +60,16 @@ impl<'a> EventsGenerator<'a> {
$(self.event_routes_tokens)
];
$['\n']
public static async $task<$async_enumerable<($actor_id_type, $class_name)>> ListenAsync($core_listener remoting, $cancellation_token cancellationToken = default)
private readonly $remoting remoting;
$['\n']
public $listener_name($remoting remoting)
{
this.remoting = remoting;
}
$['\n']
public async $task<$async_enumerable<($actor_id_type, $class_name)>> ListenAsync($cancellation_token cancellationToken = default)
{$['\r']
var eventStream = await remoting.ListenAsync(cancellationToken);$['\r']
var eventStream = await this.remoting.ListenAsync(cancellationToken);$['\r']
return eventStream.SelectEvent<$class_name>(ROUTE, EventRoutes);$['\r']
}
}
Expand Down
17 changes: 0 additions & 17 deletions net/rs/client-gen/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,8 @@ use genco::{
lang::{csharp::Tokens, Csharp},
tokens::{FormatInto, ItemStr},
};
use parity_scale_codec::Encode;
use sails_idl_parser::ast::FuncParam;

pub(crate) fn path_bytes(path: &str) -> (String, usize) {
if path.is_empty() {
(String::new(), 0)
} else {
let service_path_bytes = path.encode();
let service_path_encoded_length = service_path_bytes.len();
let service_path_bytes = service_path_bytes
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(", ");

(service_path_bytes, service_path_encoded_length)
}
}

pub(crate) fn encoded_fn_args_comma_prefixed(params: &[FuncParam]) -> String {
params
.iter()
Expand Down
14 changes: 6 additions & 8 deletions net/rs/client-gen/src/service_generators.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{helpers::*, type_decl_generators::*};
use convert_case::{Case, Casing};
use csharp::Tokens;
use genco::prelude::*;
use sails_idl_parser::{ast::visitor, ast::visitor::Visitor, ast::*};
Expand All @@ -23,7 +22,7 @@ impl<'a> ServiceClientGenerator<'a> {
}

pub(crate) fn finalize(self) -> Tokens {
let name = &self.service_name.to_case(Case::Pascal);
let name = &self.service_name;
let remoting = &csharp::import("global::Sails.Remoting.Abstractions.Core", "IRemoting");

quote! {
Expand All @@ -34,6 +33,9 @@ impl<'a> ServiceClientGenerator<'a> {
$['\n']
public sealed partial class $name : I$name$['\r']
{
$['\n']
private const string ROUTE = nameof($name);
$['\n']
private readonly $remoting remoting;
$['\n']
public $name($remoting remoting)
Expand All @@ -55,11 +57,7 @@ impl<'a> Visitor<'a> for ServiceClientGenerator<'a> {
}

fn visit_service_func(&mut self, func: &'a ServiceFunc) {
let func_name_pascal = &func.name().to_case(Case::Pascal);

let service_route_bytes = path_bytes(self.service_name.as_str()).0;
let func_route_bytes = path_bytes(func.name()).0;
let route_bytes = [service_route_bytes, func_route_bytes].join(", ");
let func_name_pascal = func.name();

let args = &encoded_fn_args_comma_prefixed(func.params());
let args_with_type = &self.type_generator.fn_params_with_types(func.params());
Expand All @@ -78,7 +76,7 @@ impl<'a> Visitor<'a> for ServiceClientGenerator<'a> {
$(inheritdoc())
public $return_type<$func_return_type> $func_name_pascal($args_with_type)
{
return new $action<$func_return_type>(this.remoting, [$(&route_bytes)]$args);
return new $action<$func_return_type>(this.remoting, ROUTE, nameof($func_name_pascal) $args);
}
};
}
Expand Down
10 changes: 7 additions & 3 deletions net/rs/client-gen/tests/snapshots/generator__basic_works.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ ICall<global::Substrate.NetApi.Model.Types.Primitive.U8> DoThat(global::Substrat
}

public sealed partial class Basic : IBasic
{ private readonly IRemoting remoting;
{

private const string ROUTE = nameof(Basic);

private readonly IRemoting remoting;

public Basic(IRemoting remoting) { this.remoting = remoting; }

/// <inheritdoc/>
public ICall<global::Substrate.NetApi.Model.Types.Primitive.U16> DoThis(global::Substrate.NetApi.Model.Types.Primitive.U32 p1, MyParam p2) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U16>(this.remoting, [20, 66, 97, 115, 105, 99, 24, 68, 111, 84, 104, 105, 115], p1, p2); }
public ICall<global::Substrate.NetApi.Model.Types.Primitive.U16> DoThis(global::Substrate.NetApi.Model.Types.Primitive.U32 p1, MyParam p2) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U16>(this.remoting, ROUTE, nameof(DoThis) , p1, p2); }
/// <inheritdoc/>
public ICall<global::Substrate.NetApi.Model.Types.Primitive.U8> DoThat(global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.U8, global::Substrate.NetApi.Model.Types.Primitive.U32> p1) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U8>(this.remoting, [20, 66, 97, 115, 105, 99, 24, 68, 111, 84, 104, 97, 116], p1); } }
public ICall<global::Substrate.NetApi.Model.Types.Primitive.U8> DoThat(global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.U8, global::Substrate.NetApi.Model.Types.Primitive.U32> p1) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U8>(this.remoting, ROUTE, nameof(DoThat) , p1); } }

public sealed partial class MyParam : global::Substrate.NetApi.Model.Types.Base.BaseType {
public global::Substrate.NetApi.Model.Types.Primitive.U32 F1 { get; init; } = new();
Expand Down
18 changes: 13 additions & 5 deletions net/rs/client-gen/tests/snapshots/generator__events_works.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ public interface IServiceWithEvents
}

public sealed partial class ServiceWithEvents : IServiceWithEvents
{ private readonly IRemoting remoting;
{

private const string ROUTE = nameof(ServiceWithEvents);

private readonly IRemoting remoting;

public ServiceWithEvents(IRemoting remoting) { this.remoting = remoting; }

/// <inheritdoc/>
public ICall<global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU64> DoThis(global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU256 p1, MyParam p2) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU64>(this.remoting, [68, 83, 101, 114, 118, 105, 99, 101, 87, 105, 116, 104, 69, 118, 101, 110, 116, 115, 24, 68, 111, 84, 104, 105, 115], p1, p2); } }
public ICall<global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU64> DoThis(global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU256 p1, MyParam p2) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Primitive.NonZeroU64>(this.remoting, ROUTE, nameof(DoThis) , p1, p2); } }

public enum ServiceWithEventsEvents { One,
Two,
Expand All @@ -41,14 +45,18 @@ this.AddTypeDecoder<MyParam>(ServiceWithEventsEvents.Three);
this.AddTypeDecoder<global::Substrate.NetApi.Model.Types.Base.BaseVoid>(ServiceWithEventsEvents.Reset);
} }

public static class ServiceWithEventsListener {
public sealed partial class ServiceWithEventsListener {

private const string ROUTE = "ServiceWithEvents";

private static readonly string[] EventRoutes = [ "One","Two","Three","Reset", ];

public static async Task<IAsyncEnumerable<(global::Substrate.Gear.Api.Generated.Model.gprimitives.ActorId, EnumServiceWithEventsEvents)>> ListenAsync(IRemotingListener remoting, CancellationToken cancellationToken = default) {
var eventStream = await remoting.ListenAsync(cancellationToken);
private readonly IRemoting remoting;

public ServiceWithEventsListener(IRemoting remoting) { this.remoting = remoting; }

public async Task<IAsyncEnumerable<(global::Substrate.Gear.Api.Generated.Model.gprimitives.ActorId, EnumServiceWithEventsEvents)>> ListenAsync(CancellationToken cancellationToken = default) {
var eventStream = await this.remoting.ListenAsync(cancellationToken);
return eventStream.SelectEvent<EnumServiceWithEventsEvents>(ROUTE, EventRoutes);
} }

Expand Down
26 changes: 17 additions & 9 deletions net/rs/client-gen/tests/snapshots/generator__full.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ IActivation New(global::Substrate.NetApi.Model.Types.Primitive.U32 a);
{ this.remoting = remoting; }

/// <inheritdoc/>
public IActivation New(global::Substrate.NetApi.Model.Types.Primitive.U32 a) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Base.BaseVoid>(this.remoting, [12, 78, 101, 119], a); }
public IActivation New(global::Substrate.NetApi.Model.Types.Primitive.U32 a) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Base.BaseVoid>(this.remoting, nameof(New), string.Empty , a); }

}

Expand All @@ -46,18 +46,22 @@ IQuery<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::
}

public sealed partial class Service : IService
{ private readonly IRemoting remoting;
{

private const string ROUTE = nameof(Service);

private readonly IRemoting remoting;

public Service(IRemoting remoting) { this.remoting = remoting; }

/// <inheritdoc/>
public ICall<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>> DoThis(global::Substrate.NetApi.Model.Types.Primitive.U32 p1, global::Substrate.NetApi.Model.Types.Primitive.Str p2, global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Base.BaseOpt<global::Substrate.NetApi.Model.Types.Primitive.Str>, global::Substrate.NetApi.Model.Types.Primitive.U8> p3, ThisThatSvcAppTupleStruct p4) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>>(this.remoting, [28, 83, 101, 114, 118, 105, 99, 101, 24, 68, 111, 84, 104, 105, 115], p1, p2, p3, p4); }
public ICall<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>> DoThis(global::Substrate.NetApi.Model.Types.Primitive.U32 p1, global::Substrate.NetApi.Model.Types.Primitive.Str p2, global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Base.BaseOpt<global::Substrate.NetApi.Model.Types.Primitive.Str>, global::Substrate.NetApi.Model.Types.Primitive.U8> p3, ThisThatSvcAppTupleStruct p4) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>>(this.remoting, ROUTE, nameof(DoThis) , p1, p2, p3, p4); }
/// <inheritdoc/>
public ICall<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>, global::Substrate.NetApi.Model.Types.Primitive.Str>> DoThat(ThisThatSvcAppDoThatParam param) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>, global::Substrate.NetApi.Model.Types.Primitive.Str>>(this.remoting, [28, 83, 101, 114, 118, 105, 99, 101, 24, 68, 111, 84, 104, 97, 116], param); }
public ICall<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>, global::Substrate.NetApi.Model.Types.Primitive.Str>> DoThat(ThisThatSvcAppDoThatParam param) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Base.BaseTuple<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.U32>, global::Substrate.NetApi.Model.Types.Primitive.Str>>(this.remoting, ROUTE, nameof(DoThat) , param); }
/// <inheritdoc/>
public IQuery<global::Substrate.NetApi.Model.Types.Primitive.U32> This(global::Substrate.NetApi.Model.Types.Base.BaseVec<global::Substrate.NetApi.Model.Types.Primitive.U16> v1) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U32>(this.remoting, [28, 83, 101, 114, 118, 105, 99, 101, 16, 84, 104, 105, 115], v1); }
public IQuery<global::Substrate.NetApi.Model.Types.Primitive.U32> This(global::Substrate.NetApi.Model.Types.Base.BaseVec<global::Substrate.NetApi.Model.Types.Primitive.U16> v1) { return new RemotingAction<global::Substrate.NetApi.Model.Types.Primitive.U32>(this.remoting, ROUTE, nameof(This) , v1); }
/// <inheritdoc/>
public IQuery<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.Str>> That(global::Substrate.NetApi.Model.Types.Base.BaseVoid v1) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.Str>>(this.remoting, [28, 83, 101, 114, 118, 105, 99, 101, 16, 84, 104, 97, 116], v1); } }
public IQuery<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.Str>> That(global::Substrate.NetApi.Model.Types.Base.BaseVoid v1) { return new RemotingAction<global::Substrate.Gear.Client.NetApi.Model.Types.Base.BaseResult<global::Substrate.NetApi.Model.Types.Primitive.Str, global::Substrate.NetApi.Model.Types.Primitive.Str>>(this.remoting, ROUTE, nameof(That) , v1); } }

public enum ServiceEvents {
/// <summary>
Expand All @@ -74,14 +78,18 @@ public enum ServiceEvents {
this.AddTypeDecoder<global::Substrate.NetApi.Model.Types.Primitive.Str>(ServiceEvents.ThatDone);
} }

public static class ServiceListener {
public sealed partial class ServiceListener {

private const string ROUTE = "Service";

private static readonly string[] EventRoutes = [ "ThisDone","ThatDone", ];

public static async Task<IAsyncEnumerable<(global::Substrate.Gear.Api.Generated.Model.gprimitives.ActorId, EnumServiceEvents)>> ListenAsync(IRemotingListener remoting, CancellationToken cancellationToken = default) {
var eventStream = await remoting.ListenAsync(cancellationToken);
private readonly IRemoting remoting;

public ServiceListener(IRemoting remoting) { this.remoting = remoting; }

public async Task<IAsyncEnumerable<(global::Substrate.Gear.Api.Generated.Model.gprimitives.ActorId, EnumServiceEvents)>> ListenAsync(CancellationToken cancellationToken = default) {
var eventStream = await this.remoting.ListenAsync(cancellationToken);
return eventStream.SelectEvent<EnumServiceEvents>(ROUTE, EventRoutes);
} }

Expand Down
Loading

0 comments on commit c7c42dc

Please sign in to comment.