Files
spark/src/lib/api/service_compiler/templates/excel/excel.pb.cs
T

169 lines
6.8 KiB
C#

#region Generated code
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Reactive.Linq;
using bts.api;
using bts.api.md;
using bts.api.core;
using bts.api.orderman;
using bts.co2;
using bts.excel;
using bts.excel.rtd;
using ExcelDna.Integration;
using ExcelDna.Integration.Rtd;
using ExcelDna.Registration;
using ExcelDna.Registration.Utils;
namespace {{package}}
{
{{#each service}}
// {{comment}}
public partial class {{name}}_Excel : IDisposable
{
readonly static object _lock = new object();
readonly static Lazy<{{name}}> _service = new Lazy<{{name}}>(() => bts.api.Locator.locate<{{name}}>(), true);
public void Dispose()
{
if (_service.IsValueCreated)
_service.Value.Dispose();
}
{{#each method}}
{{#if server_streaming}}
[ExcelFunction(Category = "BTS", Description = "Subscribe to BTS Spark {{../lname}} {{lname}} datastream")]
public static object spark_{{../lname}}_subscribe_{{lname}}(
[ExcelArgument("OPTIONAL. A filter - \"user = 'bts' and order_id = 3\"")] string where_expression = null,
[ExcelArgument("OPTIONAL. A sort clause - \"time,price\"")] string orderby_expression = null)
{
return ObservableRtdUtil.Observe(nameof(spark_{{../lname}}_subscribe_{{lname}}),
new object[]{where_expression, orderby_expression},
() => observable_{{name}}(where_expression, orderby_expression));
}
static IObservable<object> observable_{{lname}}(string where_expression = null, string orderby_expression = null)
{
Func<{{output_type_full}}, bool> wdel = (x => true);
if (!string.IsNullOrEmpty(where_expression))
{
var wexp = System.Linq.Dynamic.DynamicExpression.ParseLambda(typeof({{output_type_full}}), typeof(bool), where_expression);
wdel = (Func<{{output_type_full}}, bool>) wexp.Compile();
}
Func<{{output_type_full}}, object> odel = null;
if (!string.IsNullOrEmpty(orderby_expression))
{
var oexp = System.Linq.Dynamic.DynamicExpression.ParseLambda(typeof({{output_type_full}}), typeof(object), orderby_expression);
odel = (Func<{{output_type_full}}, object>) oexp.Compile();
}
var sub = _service.Value.subscribe_{{name}}().Select(x => x.all.Where(xx => wdel(xx)));
if(odel != null)
sub = sub.Select(x => x.OrderBy(xx => odel(xx)).Take(x.Count()));
var obj = sub.Select(x => { return new JsonFormatter(x).format(true); });
return obj;
}
{{else}}
{{#unless skip_excel_generation}}
[ExcelFunction(Category = "BTS", Description = "Call BTS Spark {{../lname}} API Method {{lname}} with parameter {{input_type_full}}")]
async public static void spark_{{../lname}}_{{lname}}_async (object[,] parameters, ExcelAsyncHandle handle, bool show_headers = false, bool display_vertically = false)
{
try
{
Google.Protobuf.IMessage req = null;
{{input_type_full}} casted_req = null;
try
{
if(parameters.Rank == 2 && parameters.GetLength(0) == 1 && parameters.GetLength(1) == 1)
{
if(parameters[0,0] == ExcelMissing.Value)
req = (Google.Protobuf.IMessage) Activator.CreateInstance(typeof({{input_type_full}}));
else if(parameters[0,0] is string json)
req = APIDispatch.generate_imessage(typeof({{input_type_full}}), json);
}
if(req == null)
{
string json = JsonFormatter.parse_json(typeof({{input_type_full}}), parameters);
req = APIDispatch.generate_imessage(typeof({{input_type_full}}), json);
}
casted_req = ({{input_type_full}}) req;
}
catch
{
var members = bts.excel.extensions.ReflectionExtensions
.get_fields_or_properties(typeof({{input_type_full}}))
.Where(x => !DataParser.excluded_member(x));
handle.SetResult(
"Error in calling method {{lname}}( {{input_type_full}} ), input members are: " +
string.Join(",", members.Select(x => x.Name).ToArray())
);
}
var res = await _service.Value.{{name}}_async( casted_req );
object res_res = res.value;
object[,] scattered = new JsonFormatter(res_res).format(show_headers);
if(display_vertically)
scattered = bts.excel.Utilities.transpose_keep_dimension(scattered);
handle.SetResult(scattered);
}
catch(Exception ex)
{
Trace.TraceError(ex.ToString());
handle.SetException(ex);
}
}
[ExcelFunction(Category = "BTS", Description = "Call BTS Spark {{../lname}} API Method {{lname}} with parameter {{input_type_full}}")]
public static object spark_{{../lname}}_{{lname}} (object[,] parameters, bool show_headers = false, bool display_vertically = false)
{
try
{
Google.Protobuf.IMessage req = null;
if(parameters.Rank == 2 && parameters.GetLength(0) == 1 && parameters.GetLength(1) == 1)
{
if(parameters[0,0] == ExcelMissing.Value)
req = (Google.Protobuf.IMessage) Activator.CreateInstance(typeof({{input_type_full}}));
else if(parameters[0,0] is string json)
req = APIDispatch.generate_imessage(typeof({{input_type_full}}), json);
}
if(req == null)
{
string json = JsonFormatter.parse_json(typeof({{input_type_full}}), parameters);
req = APIDispatch.generate_imessage(typeof({{input_type_full}}), json);
}
object res = _service.Value.{{name}}( ({{input_type_full}}) req );
object[,] scattered = new JsonFormatter(res).format(show_headers);
if(display_vertically)
scattered = bts.excel.Utilities.transpose_keep_dimension(scattered);
return scattered;
}
catch(Exception ex)
{
Trace.TraceError(ex.ToString());
}
var members = bts.excel.extensions.ReflectionExtensions
.get_fields_or_properties(typeof({{input_type_full}}))
.Where(x => !DataParser.excluded_member(x));
return "Error in calling method {{lname}}( {{input_type_full}} ), input members are: " +
string.Join(",", members.Select(x => x.Name).ToArray());
}
{{/unless}}
{{/if}}
{{/each}}
}
{{/each}}
}
#endregion