2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 03:32:57 +00:00

Nullable Attributes

This commit is contained in:
Ahmed Zamil 2022-04-03 02:19:00 +03:00
parent 2809d389bd
commit 86db6864f1
5 changed files with 213 additions and 48 deletions

View File

@ -13,11 +13,12 @@
public sealed class NullableAttribute : Attribute
{
public readonly byte[] Flags;
public readonly byte Flag;
//public readonly byte Flag;
public NullableAttribute(byte flag)
{
Flag = flag;// new byte[] { flag };
//Flag = flag;
Flags = new byte[] { flag };
}
public NullableAttribute(byte[] flags)
{

View File

@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
namespace Esiur.Data
@ -50,6 +51,84 @@ namespace Esiur.Data
public class RepresentationType
{
static RepresentationTypeIdentifier[] refTypes = new RepresentationTypeIdentifier[]
{
RepresentationTypeIdentifier.Dynamic,
RepresentationTypeIdentifier.RawData,
RepresentationTypeIdentifier.String,
RepresentationTypeIdentifier.Resource,
RepresentationTypeIdentifier.Record,
RepresentationTypeIdentifier.Map,
RepresentationTypeIdentifier.List,
RepresentationTypeIdentifier.TypedList,
RepresentationTypeIdentifier.TypedMap,
RepresentationTypeIdentifier.Tuple2,
RepresentationTypeIdentifier.Tuple3,
RepresentationTypeIdentifier.Tuple4,
RepresentationTypeIdentifier.Tuple5,
RepresentationTypeIdentifier.Tuple6,
RepresentationTypeIdentifier.Tuple7,
RepresentationTypeIdentifier.TypedRecord,
RepresentationTypeIdentifier.TypedResource
};
public void SetNull(List<byte> flags)
{
if (refTypes.Contains(Identifier))
{
Nullable = (flags.FirstOrDefault() == 2);
if (flags.Count > 0)
flags.RemoveAt(0);
}
foreach (var st in SubTypes)
st.SetNull(flags);
}
public void SetNull(byte flag)
{
if (refTypes.Contains(Identifier))
{
Nullable = (flag == 2);
}
foreach (var st in SubTypes)
st.SetNull(flag);
}
public void SetNotNull(List<byte> flags)
{
if (refTypes.Contains(Identifier))
{
Nullable = (flags.FirstOrDefault() != 1);
if (flags.Count > 0)
flags.RemoveAt(0);
}
foreach (var st in SubTypes)
st.SetNotNull(flags);
}
public override string ToString()
{
if (SubTypes != null && SubTypes.Length > 0)
return Identifier.ToString() + "<" + String.Join(",", SubTypes.Select(x => x.ToString())) + ">" + (Nullable ? "?" : "");
return Identifier.ToString() + (Nullable ? "?" : "");
}
public void SetNotNull(byte flag)
{
if (refTypes.Contains(Identifier))
{
Nullable = (flag != 1);
}
foreach (var st in SubTypes)
st.SetNotNull(flag);
}
public Type? GetRuntimeType()
{
return Identifier switch
@ -89,21 +168,21 @@ namespace Esiur.Data
public RepresentationType?[] SubTypes = new RepresentationType[3];
public static RepresentationType? FromType(Type type, bool forceNullable = false)
public static RepresentationType? FromType(Type type)//, bool forceNullable = false)
{
var nullable = forceNullable;
var nullable = false;// = forceNullable;
if (!forceNullable)
//if (!forceNullable)
//{
var nullType = System.Nullable.GetUnderlyingType(type);
if (nullType != null)
{
var nullType = System.Nullable.GetUnderlyingType(type);
if (nullType != null)
{
type = nullType;
nullable = true;
}
type = nullType;
nullable = true;
}
//}
if (type.IsGenericType)
{
@ -167,7 +246,7 @@ namespace Esiur.Data
{
var args = type.GetGenericArguments();
var subTypes = new RepresentationType[args.Length];
for(var i = 0; i < args.Length; i++)
for (var i = 0; i < args.Length; i++)
{
subTypes[i] = FromType(args[i]);
if (subTypes[i] == null)

View File

@ -222,7 +222,7 @@ public struct TransmissionType
ulong cl = (ulong)(1 << (exp -1));
if (ends - offset < cl)
return (ends - offset - (uint)cl, null);
return (cl - (ends - offset), null);
//offset += (uint)cl;
@ -233,13 +233,16 @@ public struct TransmissionType
ulong cll = (ulong)(h >> 3) & 0x7;
if (ends - offset < cll)
return (ends - offset - (uint)cll, null);
return (cll - (ends - offset), null);
ulong cl = 0;
for (uint i = 0; i < cll; i++)
cl = cl << 8 | data[offset++];
if (ends - offset < cl)
return (cl - (ends - offset), null);
return (1 + cl + cll, new TransmissionType((TransmissionTypeIdentifier)(h & 0xC7), cls, h & 0x7, offset, cl));
}
}

View File

@ -6,7 +6,7 @@
<Copyright>Ahmed Kh. Zamil</Copyright>
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.2.1</Version>
<Version>2.2.2</Version>
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
<Authors>Ahmed Kh. Zamil</Authors>
<AssemblyVersion></AssemblyVersion>

View File

@ -239,8 +239,8 @@ public class TypeTemplate
foreach (var f in tmp.functions)
{
var functionReturnTypes = GetDistributedTypes(f.MethodInfo.ReturnType);
//.Select(x => Warehouse.GetTemplateByType(x))
//.Where(x => x != null && !bag.Contains(x))
//.Select(x => Warehouse.GetTemplateByType(x))
//.Where(x => x != null && !bag.Contains(x))
foreach (var functionReturnType in functionReturnTypes)
{
@ -394,7 +394,8 @@ public class TypeTemplate
bool classIsPublic = type.IsEnum || (type.GetCustomAttribute<PublicAttribute>() != null);
var addConstant = (FieldInfo ci, PublicAttribute publicAttr) => {
var addConstant = (FieldInfo ci, PublicAttribute publicAttr) =>
{
var annotationAttr = ci.GetCustomAttribute<AnnotationAttribute>(true);
var nullableAttr = ci.GetCustomAttribute<NullableAttribute>(true);
@ -416,16 +417,35 @@ public class TypeTemplate
var addProperty = (PropertyInfo pi, PublicAttribute publicAttr) =>
{
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true);
var propType = RepresentationType.FromType(pi.PropertyType);//, nullableAttr != null && nullableAttr.Flag == 2);
var attrType = RepresentationType.FromType(pi.PropertyType, nullableAttr != null && nullableAttr.Flag == 2);
if (attrType == null)
if (propType == null)
throw new Exception($"Unsupported type `{pi.PropertyType}` in property `{type.Name}.{pi.Name}`");
var pt = new PropertyTemplate(this, (byte)properties.Count, publicAttr?.Name ?? pi.Name, pi.DeclaringType != type, attrType);
var annotationAttr = pi.GetCustomAttribute<AnnotationAttribute>(true);
var storageAttr = pi.GetCustomAttribute<StorageAttribute>(true);
var nullableContextAttr = pi.GetCustomAttribute<NullableContextAttribute>(true);
var nullableAttr = pi.GetCustomAttribute<NullableAttribute>(true);
var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
if (nullableContextAttr?.Flag == 2)
{
if (flags.Count == 1)
propType.SetNotNull(flags.FirstOrDefault());
else
propType.SetNotNull(flags);
}
else
{
if (flags.Count == 1)
propType.SetNull(flags.FirstOrDefault());
else
propType.SetNull(flags);
}
var pt = new PropertyTemplate(this, (byte)properties.Count, publicAttr?.Name ?? pi.Name, pi.DeclaringType != type, propType);
if (storageAttr != null)
pt.Recordable = storageAttr.Mode == StorageMode.Recordable;
@ -436,28 +456,44 @@ public class TypeTemplate
pt.ReadExpansion = GetTypeAnnotationName(pi.PropertyType);
pt.PropertyInfo = pi;
properties.Add(pt);
};
var addEvent = (EventInfo ei, PublicAttribute publicAttr) =>
{
var argType = ei.EventHandlerType.GenericTypeArguments[0];
var evtType = RepresentationType.FromType(argType);//, argIsNull);
if (evtType == null)
throw new Exception($"Unsupported type `{argType}` in event `{type.Name}.{ei.Name}`");
var annotationAttr = ei.GetCustomAttribute<AnnotationAttribute>(true);
var listenableAttr = ei.GetCustomAttribute<ListenableAttribute>(true);
var nullableAttr = ei.GetCustomAttribute<NullableAttribute>(true);
var nullableContextAttr = ei.GetCustomAttribute<NullableContextAttribute>(true);
var argIsNull = nullableAttr != null &&
nullableAttr.Flags != null &&
nullableAttr.Flags.Length > 1 &&
nullableAttr.Flags[1] == 2;
var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
var argType = ei.EventHandlerType.GenericTypeArguments[0];
var evtType = RepresentationType.FromType(argType, argIsNull);
// skip the eventHandler class
if (flags.Count > 1)
flags = flags.Skip(1).ToList();
if (evtType == null)
throw new Exception($"Unsupported type `{argType}` in event `{type.Name}.{ei.Name}`");
if (nullableContextAttr?.Flag == 2)
{
if (flags.Count == 1)
evtType.SetNotNull(flags.FirstOrDefault());
else
evtType.SetNotNull(flags);
}
else
{
if (flags.Count == 1)
evtType.SetNull(flags.FirstOrDefault());
else
evtType.SetNull(flags);
}
var et = new EventTemplate(this, (byte)events.Count, publicAttr?.Name ?? ei.Name, ei.DeclaringType != type, evtType);
et.EventInfo = ei;
@ -471,7 +507,7 @@ public class TypeTemplate
events.Add(et);
};
var addAttribute = (PropertyInfo pi, AttributeAttribute attributeAttr)=>
var addAttribute = (PropertyInfo pi, AttributeAttribute attributeAttr) =>
{
var an = attributeAttr.Name ?? pi.Name;
var at = new AttributeTemplate(this, 0, an, pi.DeclaringType != type);
@ -482,17 +518,43 @@ public class TypeTemplate
var addFunction = (MethodInfo mi, PublicAttribute publicAttr) =>
{
var rtType = 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 contextIsNull = nullableContextAttr != null && nullableContextAttr.Flag == 2;
var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>();
var returnType = RepresentationType.FromType(mi.ReturnType,
nullableAttr != null ? nullableAttr.Flag == 2 : contextIsNull);
var rtNullableAttr = mi.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(NullableAttribute), true).FirstOrDefault() as NullableAttribute;
var rtNullableContextAttr = mi.ReturnTypeCustomAttributes
.GetCustomAttributes(typeof(NullableContextAttribute), true)
.FirstOrDefault() as NullableContextAttribute
?? nullableContextAttr;
if (returnType == null)
throw new Exception($"Unsupported type `{mi.ReturnType}` in method `{type.Name}.{mi.Name}` return");
var rtFlags = rtNullableAttr?.Flags?.ToList() ?? new List<byte>();
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();
@ -504,15 +566,33 @@ public class TypeTemplate
var arguments = args.Select(x =>
{
var xNullableAttr = x.GetCustomAttribute<NullableAttribute>(true);
var xNullableContextAttr = x.GetCustomAttribute<NullableContextAttribute>(true);
var argType = RepresentationType.FromType(x.ParameterType,
xNullableAttr != null ? xNullableAttr.Flag == 2 : contextIsNull);
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,
@ -525,7 +605,7 @@ public class TypeTemplate
var fn = publicAttr.Name ?? mi.Name;
var ft = new FunctionTemplate(this, (byte)functions.Count, fn, mi.DeclaringType != type, arguments, returnType);// mi.ReturnType == typeof(void));
var ft = new FunctionTemplate(this, (byte)functions.Count, fn, mi.DeclaringType != type, arguments, rtType);
if (annotationAttr != null)
ft.Expansion = annotationAttr.Annotation;
@ -535,6 +615,8 @@ public class TypeTemplate
ft.MethodInfo = mi;
functions.Add(ft);
Console.WriteLine(rtType.ToString() + " " + fn);
};
@ -583,7 +665,7 @@ public class TypeTemplate
if (privateAttr != null)
continue;
var publicAttr = ei.GetCustomAttribute<PublicAttribute>(true);
var publicAttr = ei.GetCustomAttribute<PublicAttribute>(true);
addEvent(ei, publicAttr);
}