mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
Make...Template
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
using Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Data;
|
||||
using Esiur.Net.IIP;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -71,4 +74,115 @@ public class FunctionTemplate : MemberTemplate
|
||||
this.Annotation = annotation;
|
||||
this.IsStatic = isStatic;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static FunctionTemplate MakeFunctionTemplate(Type type, MethodInfo mi, byte index = 0, string customName = null, TypeTemplate typeTemplate = null)
|
||||
{
|
||||
|
||||
var genericRtType = mi.ReturnType.IsGenericType ? mi.ReturnType.GetGenericTypeDefinition() : null;
|
||||
|
||||
var rtType = genericRtType == typeof(AsyncReply<>) ?
|
||||
RepresentationType.FromType(mi.ReturnType.GetGenericArguments()[0]) :
|
||||
RepresentationType.FromType(mi.ReturnType);
|
||||
|
||||
if (rtType == null)
|
||||
throw new Exception($"Unsupported type `{mi.ReturnType}` in method `{type.Name}.{mi.Name}` return");
|
||||
|
||||
var annotationAttr = mi.GetCustomAttribute<AnnotationAttribute>(true);
|
||||
var nullableAttr = mi.GetCustomAttribute<NullableAttribute>(true);
|
||||
var nullableContextAttr = mi.GetCustomAttribute<NullableContextAttribute>(true);
|
||||
|
||||
var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
|
||||
|
||||
var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(NullableAttribute), true).FirstOrDefault() as NullableAttribute;
|
||||
var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
|
||||
.GetCustomAttributes(typeof(NullableContextAttribute), true)
|
||||
.FirstOrDefault() as NullableContextAttribute
|
||||
?? nullableContextAttr;
|
||||
|
||||
var rtFlags = rtNullableAttr?.Flags?.ToList() ?? new List<byte>();
|
||||
|
||||
if (rtFlags.Count > 0 && genericRtType == typeof(AsyncReply<>))
|
||||
rtFlags.RemoveAt(0);
|
||||
|
||||
if (rtNullableContextAttr?.Flag == 2)
|
||||
{
|
||||
if (rtFlags.Count == 1)
|
||||
rtType.SetNotNull(rtFlags.FirstOrDefault());
|
||||
else
|
||||
rtType.SetNotNull(rtFlags);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rtFlags.Count == 1)
|
||||
rtType.SetNull(rtFlags.FirstOrDefault());
|
||||
else
|
||||
rtType.SetNull(rtFlags);
|
||||
}
|
||||
|
||||
var args = mi.GetParameters();
|
||||
|
||||
if (args.Length > 0)
|
||||
{
|
||||
if (args.Last().ParameterType == typeof(DistributedConnection))
|
||||
args = args.Take(args.Count() - 1).ToArray();
|
||||
}
|
||||
|
||||
var arguments = args.Select(x =>
|
||||
{
|
||||
var argType = RepresentationType.FromType(x.ParameterType);
|
||||
|
||||
if (argType == null)
|
||||
throw new Exception($"Unsupported type `{x.ParameterType}` in method `{type.Name}.{mi.Name}` parameter `{x.Name}`");
|
||||
|
||||
|
||||
var argNullableAttr = x.GetCustomAttribute<NullableAttribute>(true);
|
||||
var argNullableContextAttr = x.GetCustomAttribute<NullableContextAttribute>(true) ?? nullableContextAttr;
|
||||
|
||||
var argFlags = argNullableAttr?.Flags?.ToList() ?? new List<byte>();
|
||||
|
||||
|
||||
if (argNullableContextAttr?.Flag == 2)
|
||||
{
|
||||
if (argFlags.Count == 1)
|
||||
argType.SetNotNull(argFlags.FirstOrDefault());
|
||||
else
|
||||
argType.SetNotNull(argFlags);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rtFlags.Count == 1)
|
||||
argType.SetNull(argFlags.FirstOrDefault());
|
||||
else
|
||||
argType.SetNull(argFlags);
|
||||
}
|
||||
|
||||
return new ArgumentTemplate()
|
||||
{
|
||||
Name = x.Name,
|
||||
Type = argType,
|
||||
ParameterInfo = x,
|
||||
Optional = x.IsOptional
|
||||
};
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
var fn = customName ?? mi.Name;
|
||||
|
||||
var ft = new FunctionTemplate(typeTemplate, index, fn, mi.DeclaringType != type,
|
||||
mi.IsStatic,
|
||||
arguments, rtType);
|
||||
|
||||
if (annotationAttr != null)
|
||||
ft.Annotation = annotationAttr.Annotation;
|
||||
else
|
||||
ft.Annotation = "(" + String.Join(",", mi.GetParameters().Where(x => x.ParameterType != typeof(DistributedConnection)).Select(x => "[" + x.ParameterType.Name + "] " + x.Name)) + ") -> " + mi.ReturnType.Name;
|
||||
|
||||
ft.MethodInfo = mi;
|
||||
// functions.Add(ft);
|
||||
|
||||
return ft;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user