style: Clean up pb.cs templates (generated code)

This commit is contained in:
John Colvin committed 2022-05-24 12:25:16 -04:00
1 parent 896af55e79
commit 377be5030f
2 files changed
+253 -242

No files matched your search

@@ -1,71 +1,71 @@
#region Generated code
using System;
using System.Threading.Tasks;
using bts.api.core;
namespace {{package}} {
{{#each service}}
// {{comment}}
public sealed class {{name}} : IApiClient, IDisposable {
private readonly ClientChannel _channel;
private {{name}}(ClientChannel channel) {
_channel = channel;
}
public void Dispose() {
_channel.Dispose();
}
{{#each method}}
{{#if server_streaming}}
// {{comment}}
public IObservable<Update<{{output_type_full}},{{key_type.cstype}}>> subscribe_{{name}}(string qualifier=null) {
return _channel.subscribe<{{output_type_full}}, {{output_type_full}}>({{@index}}, "{{name}}", qualifier, {{output_type_full}}.Parser, a=>a).reconstitute(key);
}
public IObservable<Update<{{output_type_full}}Immutable,{{key_type.cstype}}>> subscribei_{{name}}(string qualifier=null) {
return _channel.subscribe<{{output_type_full}}, {{output_type_full}}Immutable>({{@index}}, "{{name}}", qualifier, {{output_type_full}}.Parser, a=>({{output_type_full}}Immutable)a).reconstitute(key);
}
private static {{key_type.cstype}} key({{output_type_full}} row) {
return row.{{camel key_field}};
}
{{#if key_type.ismessage}}
private static {{key_type.cstype}} key({{output_type_full}}Immutable row) {
return row.{{camel key_field}}.CloneToMutable();
}
{{else}}
private static {{key_type.cstype}} key({{output_type_full}}Immutable row) {
return row.{{camel key_field}};
}
{{/if}}
{{else}}
// {{comment}}
public Task<bts.api.core.Result<{{output_type_full}}>> {{name}}_async({{input_type_full}} request) {
return _channel.send<{{input_type_full}},{{output_type_full}}>({{@index}}, request);
}
internal Task<bts.api.core.Result<{{output_type_full}}>> {{name}}_async({{input_type_full}}Immutable request) {
return _channel.send<{{input_type_full}}Immutable,{{output_type_full}}>({{@index}}, request);
}
// {{comment}}
public {{output_type_full}} {{name}}({{input_type_full}} request) {
if (_channel.is_sync_call_ok()) {
return {{name}}_async(request).Result.value;
}
else {
throw new bts.api.core.ApiException("API blocking call cannot be made on this thread. Please use {{name}}_async instead.");
}
}
internal {{output_type_full}} {{name}}({{input_type_full}}Immutable request) {
if (_channel.is_sync_call_ok()) {
return {{name}}_async(request).Result.value;
}
else {
throw new bts.api.core.ApiException("API blocking call cannot be made on this thread. Please use {{name}}_async instead.");
}
}
{{/if}}
{{/each}}
}
{{/each}}
}
#endregion
#region Generated code
namespace {{package}} {
using bts.api.core;
using System;
using System.Threading.Tasks;
{{#each service}}
// {{comment}}
public sealed class {{name}} : IApiClient, IDisposable {
readonly ClientChannel _channel;
{{name}}(ClientChannel channel) =>
_channel = channel;
public void Dispose() =>
_channel.Dispose();
{{#each method}}
{{#if server_streaming}}
// {{comment}}
public IObservable<Update<{{output_type_full}}, {{key_type.cstype}}>> subscribe_{{name}}(string qualifier = null) =>
_channel.subscribe<{{output_type_full}}, {{output_type_full}}>(
{{@index}}, "{{name}}", qualifier, {{output_type_full}}.Parser, a => a)
.reconstitute(_key);
public IObservable<Update<{{output_type_full}}Immutable, {{key_type.cstype}}>> subscribei_{{name}}(string qualifier = null) =>
_channel.subscribe<{{output_type_full}}, {{output_type_full}}Immutable>(
{{@index}}, "{{name}}", qualifier, {{output_type_full}}.Parser, a => ({{output_type_full}}Immutable)a)
.reconstitute(_key);
static {{key_type.cstype}} _key({{output_type_full}} row) =>
row.{{camel key_field}};
{{#if key_type.ismessage}}
static {{key_type.cstype}} _key({{output_type_full}}Immutable row) =>
row.{{camel key_field}}.CloneToMutable();
{{else}}
static {{key_type.cstype}} _key({{output_type_full}}Immutable row) =>
row.{{camel key_field}};
{{/if}}
{{else}}
// {{comment}}
public Task<Result<{{output_type_full}}>> {{name}}_async({{input_type_full}} request) =>
_channel.send<{{input_type_full}}, {{output_type_full}}>({{@index}}, request);
internal Task<Result<{{output_type_full}}>> {{name}}_async({{input_type_full}}Immutable request) =>
_channel.send<{{input_type_full}}Immutable, {{output_type_full}}>({{@index}}, request);
// {{comment}}
public {{output_type_full}} {{name}}({{input_type_full}} request) {
if (_channel.is_sync_call_ok()) {
return {{name}}_async(request).Result.value;
}
throw new ApiException(
"API blocking call cannot be made on this thread. Please use {{name}}_async instead.");
}
internal {{output_type_full}} {{name}}({{input_type_full}}Immutable request) {
if (_channel.is_sync_call_ok()) {
return {{name}}_async(request).Result.value;
}
throw new ApiException(
"API blocking call cannot be made on this thread. Please use {{name}}_async instead.");
}
{{/if}}
{{/each}}
}
{{/each}}
}
#endregion
@@ -1,160 +1,40 @@
#region Generated code
namespace {{package}} {
using bts.api;
using bts.api.core;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using bts.api.core;
using System.Linq;
using Google.Protobuf;
using System.Runtime.InteropServices;
using bts.api;
using Google.Protobuf;
namespace {{package}}
{
{{#each service}}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
{{#each service}}
// {{comment}}
internal abstract class Authenticated{{name}}Base : ApiServer, IDisposable {
readonly ServerChannel _channel;
public Authenticated{{name}}Base(ServiceContext context, string qualifier="") {
_channel = context.publish(this, "{{name}}", qualifier);
}
// {{comment}}
abstract class Authenticated{{name}}Base : ApiServer, IDisposable {
readonly ServerChannel _channel;
public virtual void Dispose() {
_channel.Dispose();
}
public Authenticated{{name}}Base(ServiceContext context, string qualifier = "") =>
_channel = context.publish(this, "{{name}}", qualifier);
{{#each method}}
{{#if server_streaming}}
protected struct {{cap name}}Publisher {
readonly IntPtr _table;
{{#unless key_type.natural_key}}
int _next_key;
Dictionary<{{key_type.cstype}}, int> _keys;
int hash_key({{key_type.cstype}} key) {
lock(_keys) {
int intkey;
if (_keys.TryGetValue(key, out intkey)) {
return intkey;
}
intkey = ++_next_key;
_keys[key] = intkey;
return intkey;
enum ServiceMethods {
SYS_PING = -1,
{{#each method}}
{{upper name}} = {{@index}},
{{/each}}
}
public override void dispatch(
RequestHeader header, Stream request_stream, Action<byte[]> on_response) {
switch((ServiceMethods)header.Method) {
case ServiceMethods.SYS_PING: {
var msg = PingPong.Parser.ParseFrom(request_stream);
respond(header.RequestId, Task.FromResult(msg), on_response);
break;
}
}
{{/unless}}
public {{cap name}}Publisher(IntPtr table) {
_table = table;
{{#unless key_type.natural_key}}
_next_key = 0;
_keys = new Dictionary<{{key_type.cstype}}, int>();
{{/unless}}
}
ApiNativeMethods.StreamUpdate pack({{output_type_full}} item, bool is_change, List<GCHandle> pinned_buffers) {
var data = Google.Protobuf.MessageExtensions.ToByteArray(item);
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
pinned_buffers.Add(handle);
return new ApiNativeMethods.StreamUpdate() {
type = is_change ? ApiNativeMethods.ChangeType.REPLACE : ApiNativeMethods.ChangeType.CREATE,
{{#unless key_type.natural_key}}
key = hash_key({{name}}_key(item)),
{{else}}
key = (int){{name}}_key(item),
{{/unless}}
buffer_len = data.Length,
buffer = handle.AddrOfPinnedObject()
};
}
public void publish(
List<{{output_type_full}}> added=null,
HashSet<{{key_type.cstype}}> removed=null,
List<{{output_type_full}}> changed=null) {
int add_count = added == null ? 0 : added.Count;
int change_count = changed == null ? 0 : changed.Count;
int remove_count = removed == null ? 0 : removed.Count;
var changes = new ApiNativeMethods.StreamUpdate[add_count + change_count + remove_count];
int change_index = 0;
var pinned_buffers = new List<GCHandle>(add_count + change_count);
try {
// Added
if (added != null) {
foreach (var a in added) {
var key = {{name}}_key(a);
var is_change = removed == null ? false : removed.Contains(key);
if (is_change) { removed.Remove(key); }
changes[change_index++] = pack(a, is_change, pinned_buffers);
}
}
// Changed
if (changed != null) {
foreach(var ch in changed) {
changes[change_index++] = pack(ch, true, pinned_buffers);
}
}
// Removed
if (removed != null) {
foreach(var r in removed) {
changes[change_index++] = new ApiNativeMethods.StreamUpdate() {
type = ApiNativeMethods.ChangeType.REMOVE,
{{#unless key_type.natural_key}}
key = hash_key(r),
{{else}}
key = (int)r,
{{/unless}}
buffer_len = 0
};
}
}
ApiNativeMethods.api_server_update_table(_table, changes, change_index);
}
finally {
foreach (var handle in pinned_buffers) {
handle.Free();
}
}
}
public void append({{output_type_full}} single) {
publish(added: new List<{{output_type_full}}> { single });
}
}
//{{comment}}
protected {{cap name}}Publisher make_{{name}}_publisher(string qualifier=null) {
IntPtr table = _channel.create_table("{{name}}",qualifier);
return new {{cap name}}Publisher(table);
}
protected static {{key_type.cstype}} {{name}}_key({{output_type_full}} row) {
return row.{{camel key_field}};
}
public virtual Task<StreamAccessResponse> allow_{{name}}(bts.api.AuthTokenImmutable? token, string qualifier) {
var r = new StreamAccessResponse() { Authorized = true };
return Task.FromResult(r);
}
{{else}}
//{{comment}}
public abstract Task<{{output_type_full}}> {{name}}(bts.api.AuthTokenImmutable? token, {{input_type_full}} request);
{{/if}}
{{/each}}
enum ServiceMethods {
SYS_PING = -1,
{{#each method}}
{{upper name}} = {{@index}},
{{/each}}
}
public override void dispatch(RequestHeader header, System.IO.Stream request_stream, Action<byte[]> on_response) {
switch((ServiceMethods)header.Method) {
case ServiceMethods.SYS_PING: {
var msg = PingPong.Parser.ParseFrom(request_stream);
respond(header.RequestId, Task.FromResult(msg), on_response);
break;
}
{{#each method}}
case ServiceMethods.{{upper name}}: {
{{#if server_streaming}}
@@ -169,30 +49,161 @@ internal abstract class Authenticated{{name}}Base : ApiServer, IDisposable {
break;
}
{{/each}}
default:
var buffer = new ResponseHeader(){
RequestId = header.RequestId,
Code = ApiError.MissingMethod
}.serialize();
on_response(buffer); break;
}
}
}
default: {
var buffer = new ResponseHeader() {
RequestId = header.RequestId,
Code = ApiError.MissingMethod
}.serialize();
on_response(buffer);
break;
}
}
}
// {{comment}}
internal abstract class {{name}}Base : Authenticated{{name}}Base , IDisposable {
public {{name}}Base(ServiceContext context, string qualifier="") : base(context, qualifier) { }
{{#each method}}
public virtual void Dispose() =>
_channel.Dispose();
{{#each method}}
{{#if server_streaming}}
public virtual Task<StreamAccessResponse> allow_{{name}}(
AuthTokenImmutable? token, string qualifier) {
var r = new StreamAccessResponse() { Authorized = true };
return Task.FromResult(r);
}
protected static {{key_type.cstype}} {{name}}_key({{output_type_full}} row) =>
row.{{camel key_field}};
//{{comment}}
protected {{cap name}}Publisher make_{{name}}_publisher(string qualifier = null) {
var table = _channel.create_table("{{name}}", qualifier);
return new {{cap name}}Publisher(table);
}
protected struct {{cap name}}Publisher {
readonly IntPtr _table;
{{#unless key_type.natural_key}}
int _next_key;
Dictionary<{{key_type.cstype}}, int> _keys;
{{/unless}}
public {{cap name}}Publisher(IntPtr table) {
_table = table;
{{#unless key_type.natural_key}}
_next_key = 0;
_keys = new();
{{/unless}}
}
public void publish(
List<{{output_type_full}}> added = null,
HashSet<{{key_type.cstype}}> removed = null,
List<{{output_type_full}}> changed = null) {
int add_count = added is null ? 0 : added.Count;
int change_count = changed is null ? 0 : changed.Count;
int remove_count = removed is null ? 0 : removed.Count;
var changes = new ApiNativeMethods.StreamUpdate[add_count + change_count + remove_count];
int change_index = 0;
var pinned_buffers = new List<GCHandle>(add_count + change_count);
try {
// Added
if (added is not null) {
foreach (var a in added) {
var key = {{name}}_key(a);
var is_change = removed == null ? false : removed.Contains(key);
if (is_change) { removed.Remove(key); }
changes[change_index++] = _pack(a, is_change, pinned_buffers);
}
}
// Changed
if (changed is not null) {
foreach(var ch in changed) {
changes[change_index++] = _pack(ch, true, pinned_buffers);
}
}
// Removed
if (removed is not null) {
foreach(var r in removed) {
changes[change_index++] = new() {
type = ApiNativeMethods.ChangeType.REMOVE,
{{#unless key_type.natural_key}}
key = _hash_key(r),
{{else}}
key = (int)r,
{{/unless}}
buffer_len = 0
};
}
}
ApiNativeMethods.api_server_update_table(_table, changes, change_index);
} finally {
foreach (var handle in pinned_buffers) {
handle.Free();
}
}
}
public void append({{output_type_full}} single) =>
publish(added: new List<{{output_type_full}}> { single });
ApiNativeMethods.StreamUpdate _pack({{output_type_full}} item, bool is_change, List<GCHandle> pinned_buffers) {
var data = MessageExtensions.ToByteArray(item);
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
pinned_buffers.Add(handle);
return new() {
type = is_change ? ApiNativeMethods.ChangeType.REPLACE : ApiNativeMethods.ChangeType.CREATE,
{{#unless key_type.natural_key}}
key = _hash_key({{name}}_key(item)),
{{else}}
key = (int){{name}}_key(item),
{{/unless}}
buffer_len = data.Length,
buffer = handle.AddrOfPinnedObject()
};
}
{{#unless key_type.natural_key}}
int _hash_key({{key_type.cstype}} key) {
lock(_keys) {
if (!_keys.TryGetValue(key, out var int_key)) {
int_key = ++_next_key;
_keys[key] = int_key;
}
return int_key;
}
}
{{/unless}}
}
{{else}}
//{{comment}}
public abstract Task<{{output_type_full}}> {{name}}(AuthTokenImmutable? token, {{input_type_full}} request);
{{/if}}
{{/each}}
}
// {{comment}}
abstract class {{name}}Base : Authenticated{{name}}Base , IDisposable {
public {{name}}Base(ServiceContext context, string qualifier="")
: base(context, qualifier) { }
{{#each method}}
{{#unless server_streaming}}
public override Task<{{output_type_full}}> {{name}}(bts.api.AuthTokenImmutable? token, {{input_type_full}} request) {
return {{name}}(request);
}
// {{comment}}
public abstract Task<{{output_type_full}}> {{name}}({{input_type_full}} request);
{{/unless}}
{{/each}}
}
public override Task<{{output_type_full}}> {{name}}(
bts.api.AuthTokenImmutable? token, {{input_type_full}} request) =>
{{name}}(request);
// {{comment}}
public abstract Task<{{output_type_full}}> {{name}}({{input_type_full}} request);
{{/unless}}
{{/each}}
}
{{/each}}
}
#endregion