2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

Nullable Attributes

This commit is contained in:
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));
}
}