diff --git a/Esiur/Data/DataDeserializer - Copy.cs b/Esiur/Data/DataDeserializer - Copy.cs deleted file mode 100644 index 6532f12..0000000 --- a/Esiur/Data/DataDeserializer - Copy.cs +++ /dev/null @@ -1,562 +0,0 @@ -using Esiur.Core; -using Esiur.Net.EP; -using Esiur.Resource; -using System; -using System.Collections.Generic; -using System.Text; -using Esiur.Data; -using Esiur.Resource.Template; -using System.Linq; -using Esiur.Misc; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace Esiur.Data; - -public static class DataDeserializer -{ - public static AsyncReply NullParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(null); - } - - public static AsyncReply BooleanTrueParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(true); - } - - public static AsyncReply BooleanFalseParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(false); - } - - public static AsyncReply NotModifiedParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(new NotModified()); - } - - public static AsyncReply ByteParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(data[offset]); - } - public static AsyncReply SByteParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply((sbyte)data[offset]); - } - public static unsafe AsyncReply Char16Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(char*)ptr); - } - - public static AsyncReply Char8Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply((char)data[offset]); - } - - - public static unsafe AsyncReply Int16Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(short*)ptr); - } - - public static unsafe AsyncReply UInt16Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(ushort*)ptr); - } - - public static unsafe AsyncReply Int32Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(int*)ptr); - } - - public static unsafe AsyncReply UInt32Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(uint*)ptr); - } - - public static unsafe AsyncReply Float32Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(float*)ptr); - } - - public static unsafe AsyncReply Float64Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(double*)ptr); - } - - public static unsafe AsyncReply Float128Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(decimal*)ptr); - } - - public static unsafe AsyncReply Int128Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(decimal*)ptr); - } - - - public static unsafe AsyncReply UInt128Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(decimal*)ptr); - } - - - public static unsafe AsyncReply Int64Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(long*)ptr); - } - - public static unsafe AsyncReply UInt64Parser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(*(ulong*)ptr); - } - - public static unsafe AsyncReply DateTimeParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return new AsyncReply(new DateTime(*(long*)ptr, DateTimeKind.Utc)); - - } - - - public static unsafe AsyncReply ResourceParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return connection.Fetch(*(uint*)ptr, requestSequence); - } - - public static unsafe AsyncReply LocalResourceParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - fixed (byte* ptr = &data[offset]) - return Warehouse.GetById(*(uint*)ptr); - } - - - public static unsafe AsyncReply RawDataParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(data.Clip(offset, length)); - } - - public static unsafe AsyncReply StringParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - return new AsyncReply(data.GetString(offset, length)); - } - - public static unsafe AsyncReply RecordParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - - var reply = new AsyncReply(); - - var classId = data.GetUUID(offset); - offset += 16; - length -= 16; - - - var template = Warehouse.GetTemplateByClassId(classId, TemplateType.Record); - - var initRecord = (TypeSchema template) => - { - ListParser(data, offset, length, connection, requestSequence).Then(r => - { - var ar = (object[])r; - - if (template == null) - { - // @TODO: add parse if no template settings - reply.TriggerError(new AsyncException(ErrorType.Management, (ushort)ExceptionCode.TemplateNotFound, - "Template not found for record.")); - } - else if (template.DefinedType != null) - { - var record = Activator.CreateInstance(template.DefinedType) as IRecord; - for (var i = 0; i < template.Properties.Length; i++) - { - try - { - //var v = Convert.ChangeType(ar[i], template.Properties[i].PropertyInfo.PropertyType); - var v = DC.CastConvert(ar[i], template.Properties[i].PropertyInfo.PropertyType); - template.Properties[i].PropertyInfo.SetValue(record, v); - } - catch (Exception ex) - { - Global.Log(ex); - } - } - - reply.Trigger(record); - } - else - { - var record = new Record(); - - for (var i = 0; i < template.Properties.Length; i++) - record.Add(template.Properties[i].Name, ar[i]); - - reply.Trigger(record); - } - - }); - }; - - if (template != null) - { - initRecord(template); - } - else if (connection != null) - { - // try to get the template from the other end - connection.GetTemplate(classId).Then(tmp => - { - initRecord(tmp); - }).Error(x => reply.TriggerError(x)); - } - else - { - initRecord(null); - } - - return reply; - } - - public static unsafe AsyncReply ConstantParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - throw new NotImplementedException(); - } - - public static unsafe AsyncReply EnumParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - - var classId = data.GetUUID(offset); - offset += 16; - var index = data[offset++]; - - var template = Warehouse.GetTemplateByClassId(classId, TemplateType.Enum); - - if (template != null) - { - return new AsyncReply(template.Constants[index].Value); - } - else - { - var reply = new AsyncReply(); - - connection.GetTemplate(classId).Then(tmp => - { - reply.Trigger(tmp.Constants[index].Value); - }).Error(x => reply.TriggerError(x)); - - return reply; - } - } - - - - public static AsyncReply RecordListParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - var rt = new AsyncBag(); - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - rt.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - rt.Seal(); - return rt; - } - - public static AsyncReply ResourceListParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - var rt = new AsyncBag(); - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - rt.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - rt.Seal(); - return rt; - } - - - public static AsyncBag ListParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - var rt = new AsyncBag(); - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - rt.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - rt.Seal(); - return rt; - } - - public static AsyncReply TypedMapParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - // get key type - var (keyCs, keyRepType) = RepresentationType.Parse(data, offset); - offset += keyCs; - length -= keyCs; - - var (valueCs, valueRepType) = RepresentationType.Parse(data, offset); - offset += valueCs; - length -= valueCs; - - var map = (IMap)Activator.CreateInstance(typeof(Map<,>).MakeGenericType(keyRepType.GetRuntimeType(), valueRepType.GetRuntimeType())); - - var rt = new AsyncReply(); - - var results = new AsyncBag(); - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - - results.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - results.Seal(); - - results.Then(ar => - { - for (var i = 0; i < ar.Length; i += 2) - map.Add(ar[i], ar[i + 1]); - - rt.Trigger(map); - }); - - - return rt; - - } - - public static AsyncReply TupleParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - var results = new AsyncBag(); - var rt = new AsyncReply(); - - var tupleSize = data[offset++]; - length--; - - var types = new List(); - - for (var i = 0; i < tupleSize; i++) - { - var (cs, rep) = RepresentationType.Parse(data, offset); - types.Add(rep.GetRuntimeType()); - offset += cs; - length -= cs; - } - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - results.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - results.Seal(); - - - results.Then(ar => - { - if (ar.Length == 2) - { - var type = typeof(ValueTuple<,>).MakeGenericType(types.ToArray()); - rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1])); - } - else if (ar.Length == 3) - { - var type = typeof(ValueTuple<,,>).MakeGenericType(types.ToArray()); - rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1], ar[2])); - } - else if (ar.Length == 4) - { - var type = typeof(ValueTuple<,,,>).MakeGenericType(types.ToArray()); - rt.Trigger(Activator.CreateInstance(type, ar[0], ar[1], ar[2], ar[3])); - } - }); - - return rt; - } - - public static AsyncReply TypedListParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence) - { - var rt = new AsyncBag(); - - // get the type - var (hdrCs, rep) = RepresentationType.Parse(data, offset); - - offset += hdrCs; - length -= hdrCs; - - var runtimeType = rep.GetRuntimeType(); - - rt.ArrayType = runtimeType; - - while (length > 0) - { - var (cs, reply) = Codec.Parse(data, offset, connection, requestSequence); - - rt.Add(reply); - - if (cs > 0) - { - offset += (uint)cs; - length -= (uint)cs; - } - else - throw new Exception("Error while parsing structured data"); - - } - - rt.Seal(); - return rt; - } - - - public static AsyncBag PropertyValueArrayParser(byte[] data, uint offset, uint length, EpConnection connection, uint[] requestSequence)//, bool ageIncluded = true) - { - var rt = new AsyncBag(); - - - ListParser(data, offset, length, connection, requestSequence).Then(x => - { - var ar = (object[])x; - var pvs = new List(); - - for (var i = 0; i < ar.Length; i += 3) - pvs.Add(new PropertyValue(ar[2], (ulong?)ar[0], (DateTime?)ar[1])); - - - rt.Trigger(pvs.ToArray()); - }); - - return rt; - - } - - public static (uint, AsyncReply) PropertyValueParser(byte[] data, uint offset, EpConnection connection, uint[] requestSequence)//, bool ageIncluded = true) - { - var reply = new AsyncReply(); - - var age = data.GetUInt64(offset, Endian.Little); - offset += 8; - - DateTime date = data.GetDateTime(offset, Endian.Little); - offset += 8; - - - var (valueSize, results) = Codec.Parse(data, offset, connection, requestSequence); - - results.Then(value => - { - reply.Trigger(new PropertyValue(value, age, date)); - }); - - return (16 + valueSize, reply); - } - - public static AsyncReply> HistoryParser(byte[] data, uint offset, uint length, IResource resource, EpConnection connection, uint[] requestSequence) - { - //var count = (int)toAge - (int)fromAge; - - var list = new KeyList(); - - var reply = new AsyncReply>(); - - var bagOfBags = new AsyncBag(); - - var ends = offset + length; - while (offset < ends) - { - var index = data[offset++]; - var pt = resource.Instance.Template.GetPropertyTemplateByIndex(index); - list.Add(pt, null); - var cs = data.GetUInt32(offset, Endian.Little); - offset += 4; - - var (len, pv) = PropertyValueParser(data, offset, connection, requestSequence); - - bagOfBags.Add(pv);// ParsePropertyValueArray(data, offset, cs, connection)); - offset += len; - } - - bagOfBags.Seal(); - - bagOfBags.Then(x => - { - for (var i = 0; i < list.Count; i++) - list[list.Keys.ElementAt(i)] = x[i]; - - reply.Trigger(list); - }); - - return reply; - - } - -} diff --git a/Esiur/Data/DataDeserializer.cs b/Esiur/Data/DataDeserializer.cs index 3629c78..79c5ab5 100644 --- a/Esiur/Data/DataDeserializer.cs +++ b/Esiur/Data/DataDeserializer.cs @@ -517,8 +517,8 @@ public static class DataDeserializer public static unsafe object RecordParser(ParsedTDU tdu, Warehouse warehouse) { - var classId = tdu.Metadata.GetUUID(0); - var typeDef = warehouse.GetTypeDefById(classId, TypeDefKind.Record); + var typeId = tdu.Metadata.GetUUID(0); + var typeDef = warehouse.GetTypeDefById(typeId, TypeDefKind.Record); if (typeDef == null) { diff --git a/Esiur/Data/Types/DefinitionDataType.cs b/Esiur/Data/Types/DefinitionDataType.cs index 0cdb457..5f28270 100644 --- a/Esiur/Data/Types/DefinitionDataType.cs +++ b/Esiur/Data/Types/DefinitionDataType.cs @@ -1,131 +1 @@ -//using Esiur.Data; -//using System; -//using System.Collections; -//using System.Collections.Generic; -//using System.Dynamic; -//using System.Linq; -//using System.Text; - -//namespace Esiur.Resource.Template; -//public struct TemplateDataType -//{ -// public DataType Type { get; set; } -// //public string TypeName { get; set; } -// public TypeTemplate TypeTemplate => TypeGuid == null ? null : Warehouse.GetTemplateByClassId((Guid)TypeGuid); - -// public Guid? TypeGuid { get; set; } - -// public bool IsNullable { get; set; } -// //public TemplateDataType(DataType type, string typeName) -// //{ -// // Type = type; -// // TypeName = typeName; -// //} - - - -// public static TemplateDataType FromType(Type type) -// { - - -// bool isList = typeof(ICollection).IsAssignableFrom(type); - -// var t = type switch -// { - -// { IsArray: true } => type.GetElementType(), -// { IsEnum: true } => type.GetEnumUnderlyingType(), -// _ when isList => Codec.GetGenericListType(type), -// (_) => type -// }; - -// DataType dt = t switch -// { -// _ when t == typeof(bool) => DataType.Bool, -// _ when t == typeof(char) => DataType.Char, -// _ when t == typeof(byte) => DataType.UInt8, -// _ when t == typeof(sbyte) => DataType.Int8, -// _ when t == typeof(short) => DataType.Int16, -// _ when t == typeof(ushort) => DataType.UInt16, -// _ when t == typeof(int) => DataType.Int32, -// _ when t == typeof(uint) => DataType.UInt32, -// _ when t == typeof(long) => DataType.Int64, -// _ when t == typeof(ulong) => DataType.UInt64, -// _ when t == typeof(float) => DataType.Float32, -// _ when t == typeof(double) => DataType.Float64, -// _ when t == typeof(decimal) => DataType.Decimal, -// _ when t == typeof(string) => DataType.String, -// _ when t == typeof(DateTime) => DataType.DateTime, -// _ when t == typeof(IResource) => DataType.Void, // Dynamic resource (unspecified type) -// _ when t == typeof(IRecord) => DataType.Void, // Dynamic record (unspecified type) -// _ when typeof(Structure).IsAssignableFrom(t) || t == typeof(ExpandoObject) => DataType.Structure, -// _ when Codec.ImplementsInterface(t, typeof(IResource)) => DataType.Resource, -// _ when Codec.ImplementsInterface(t, typeof(IRecord)) => DataType.Record, -// _ => DataType.Void -// }; - - -// Guid? typeGuid = null; - -// if (dt == DataType.Resource || dt == DataType.Record) -// typeGuid = TypeTemplate.GetTypeGuid(t); - -// if (type.IsArray || isList) -// dt = (DataType)((byte)dt | 0x80); - - -// return new TemplateDataType() -// { -// Type = dt, -// TypeGuid = typeGuid, -// IsNullable = Nullable.GetUnderlyingType(type) != null -// }; -// } - -// public byte[] Compose() -// { -// if (Type == DataType.Resource || -// Type == DataType.ResourceArray || -// Type == DataType.Record || -// Type == DataType.RecordArray) -// { -// var guid = DC.ToBytes((Guid)TypeGuid); -// if (IsNullable) -// { -// return new BinaryList() -// .AddUInt8((byte)((byte)Type | 0x40)) -// .AddUInt8Array(guid).ToArray(); -// } else -// { -// return new BinaryList() -// .AddUInt8((byte)Type) -// .AddUInt8Array(guid).ToArray(); -// } -// } -// else if (IsNullable) -// return new byte[] { (byte)((byte)Type | 0x40) }; -// else -// return new byte[] { (byte)Type }; -// } - -// public override string ToString() => Type.ToString() + (IsNullable ? "?":"" ) -// + TypeTemplate != null ? "<" + TypeTemplate.ClassName + ">" : ""; - - -// public static (uint, TemplateDataType) Parse(byte[] data, uint offset) -// { -// bool isNullable = (data[offset] & 0x40) > 0; -// var type = (DataType)(data[offset++] & 0xBF); - -// if (type == DataType.Resource || -// type == DataType.ResourceArray || -// type == DataType.Record || -// type == DataType.RecordArray) -// { -// var guid = data.GetGuid(offset); -// return (17, new TemplateDataType() { Type = type, TypeGuid = guid , IsNullable = isNullable}); -// } -// else -// return (1, new TemplateDataType() { Type = type, IsNullable = isNullable }); -// } -//} + \ No newline at end of file diff --git a/Esiur/Data/Types/EventDef.cs b/Esiur/Data/Types/EventDef.cs index 8642211..e1eb0a6 100644 --- a/Esiur/Data/Types/EventDef.cs +++ b/Esiur/Data/Types/EventDef.cs @@ -103,13 +103,6 @@ public class EventDef : MemberDef .ToArray(); } - //public EventTemplate(TypeSchema template, byte index, string name, bool inherited, TRU argumentType, Map annotations = null, bool subscribable = false) - // : base(template, index, name, inherited) - //{ - // this.Annotations = annotations; - // this.Subscribable = subscribable; - // this.ArgumentType = argumentType; - //} public static EventDef MakeEventDef(Type type, EventInfo ei, byte index, string name, TypeDef schema) { diff --git a/Esiur/Data/Types/FunctionDef.cs b/Esiur/Data/Types/FunctionDef.cs index 94145e1..0837a53 100644 --- a/Esiur/Data/Types/FunctionDef.cs +++ b/Esiur/Data/Types/FunctionDef.cs @@ -118,16 +118,6 @@ public class FunctionDef : MemberDef return bl.ToArray(); } - //public FunctionTemplate(TypeSchema template, byte index, string name, bool inherited, bool isStatic, ArgumentTemplate[] arguments, TRU returnType, Map annotations = null) - // : base(template, index, name, inherited) - //{ - // this.Arguments = arguments; - // this.ReturnType = returnType; - // this.Annotations = annotations; - // this.IsStatic = isStatic; - //} - - public static FunctionDef MakeFunctionDef(Type type, MethodInfo mi, byte index, string name, TypeDef schema) { diff --git a/Esiur/Data/Types/MemberDef.cs b/Esiur/Data/Types/MemberDef.cs index 99bd8eb..50ed7b1 100644 --- a/Esiur/Data/Types/MemberDef.cs +++ b/Esiur/Data/Types/MemberDef.cs @@ -13,20 +13,9 @@ public class MemberDef public string Name { get; set; } public bool Inherited { get; set; } public TypeDef Definition { get; set; } - - //public MemberTemplate() - //{ - // Template = template; - // Index = index; - // Name = name; - // Inherited = inherited; - //} + public string Fullname => Definition.Name + "." + Name; - //public virtual byte[] Compose() - //{ - // return DC.ToBytes(Name); - //} } diff --git a/Esiur/Data/Types/TypeDef.cs b/Esiur/Data/Types/TypeDef.cs index c72ca22..5d4f71c 100644 --- a/Esiur/Data/Types/TypeDef.cs +++ b/Esiur/Data/Types/TypeDef.cs @@ -14,11 +14,6 @@ using Esiur.Protocol; namespace Esiur.Data.Types; -//public enum TemplateType -//{ -// Resource, -// Record -//} public class TypeDef { @@ -43,8 +38,6 @@ public class TypeDef return typeName; } - // protected TemplateType - //bool isReady; protected byte[] content; @@ -61,17 +54,6 @@ public class TypeDef public Type DefinedType { get; set; } public Type ParentDefinedType { get; set; } - //public MemberTemplate GetMemberTemplate(MemberInfo member) - //{ - // if (member is MethodInfo) - // return GetFunctionTemplateByName(member.Name); - // else if (member is EventInfo) - // return GetEventTemplateByName(member.Name); - // else if (member is PropertyInfo) - // return GetPropertyTemplateByName(member.Name); - // else - // return null; - //} public EventDef GetEventDefByName(string eventName) { @@ -137,10 +119,6 @@ public class TypeDef get { return typeName; } } - //public MemberTemplate[] Methods - //{ - // get { return members.ToArray(); } - //} public FunctionDef[] Functions { @@ -166,9 +144,9 @@ public class TypeDef public static UUID GetTypeUUID(Type type) { - var attr = type.GetCustomAttribute(); + var attr = type.GetCustomAttribute(); if (attr != null) - return attr.ClassId; + return attr.Id; var tn = Encoding.UTF8.GetBytes(GetTypeName(type)); var hash = SHA256.Create().ComputeHash(tn).Clip(0, 16); @@ -245,11 +223,11 @@ public class TypeDef // Get parents while (parentType != null) { - var parentTemplate = warehouse.GetTypeDefByType(parentType); - if (parentTemplate != null) + var parentTypeDef = warehouse.GetTypeDefByType(parentType); + if (parentTypeDef != null) { - list.Add(parentTemplate); - parentType = parentTemplate.ParentDefinedType; + list.Add(parentTypeDef); + parentType = parentTypeDef.ParentDefinedType; } } @@ -257,18 +235,16 @@ public class TypeDef foreach (var f in sch.functions) { var functionReturnTypes = GetDistributedTypes(f.MethodInfo.ReturnType); - //.Select(x => Warehouse.GetTemplateByType(x)) - //.Where(x => x != null && !bag.Contains(x)) foreach (var functionReturnType in functionReturnTypes) { - var functionReturnTemplate = warehouse.GetTypeDefByType(functionReturnType); - if (functionReturnTemplate != null) + var functionReturnTypeDef = warehouse.GetTypeDefByType(functionReturnType); + if (functionReturnTypeDef != null) { - if (!bag.Contains(functionReturnTemplate)) + if (!bag.Contains(functionReturnTypeDef)) { - list.Add(functionReturnTemplate); - getDependenciesFunc(functionReturnTemplate, bag); + list.Add(functionReturnTypeDef); + getDependenciesFunc(functionReturnTypeDef, bag); } } } @@ -326,13 +302,13 @@ public class TypeDef foreach (var propertyType in propertyTypes) { - var propertyTemplate = warehouse.GetTypeDefByType(propertyType); - if (propertyTemplate != null) + var propertyTypeDef = warehouse.GetTypeDefByType(propertyType); + if (propertyTypeDef != null) { - if (!bag.Contains(propertyTemplate)) + if (!bag.Contains(propertyTypeDef)) { - bag.Add(propertyTemplate); - getDependenciesFunc(propertyTemplate, bag); + bag.Add(propertyTypeDef); + getDependenciesFunc(propertyTypeDef, bag); } } } @@ -345,14 +321,14 @@ public class TypeDef foreach (var eventType in eventTypes) { - var eventTemplate = warehouse.GetTypeDefByType(eventType); + var eventTypeDef = warehouse.GetTypeDefByType(eventType); - if (eventTemplate != null) + if (eventTypeDef != null) { - if (!bag.Contains(eventTemplate)) + if (!bag.Contains(eventTypeDef)) { - bag.Add(eventTemplate); - getDependenciesFunc(eventTemplate, bag); + bag.Add(eventTypeDef); + getDependenciesFunc(eventTypeDef, bag); } } } diff --git a/Esiur/Esiur.csproj b/Esiur/Esiur.csproj index b9f1eaf..61c43ed 100644 --- a/Esiur/Esiur.csproj +++ b/Esiur/Esiur.csproj @@ -58,14 +58,12 @@ - - diff --git a/Esiur/Protocol/EpConnectionProtocol.cs b/Esiur/Protocol/EpConnectionProtocol.cs index 6b90907..72dbca3 100644 --- a/Esiur/Protocol/EpConnectionProtocol.cs +++ b/Esiur/Protocol/EpConnectionProtocol.cs @@ -186,9 +186,9 @@ partial class EpConnection } - public AsyncReply StaticCall(UUID classId, byte index, object parameters) + public AsyncReply StaticCall(UUID typeId, byte index, object parameters) { - return SendRequest(EpPacketRequest.StaticCall, classId, index, parameters); + return SendRequest(EpPacketRequest.StaticCall, typeId, index, parameters); } public AsyncReply Call(string procedureCall, params object[] parameters) @@ -808,16 +808,16 @@ partial class EpConnection return; } - if (r.Instance.Applicable(session, ActionType.ViewTemplate, null) == Ruling.Denied) + if (r.Instance.Applicable(session, ActionType.ViewTypeDef, null) == Ruling.Denied) { SendError(ErrorType.Management, callback, (ushort)ExceptionCode.NotAllowed); return; } - var templates = TypeDef.GetDependencies(r.Instance.Definition, Instance.Warehouse); + var typeDefs = TypeDef.GetDependencies(r.Instance.Definition, Instance.Warehouse); // Send - SendReply(EpPacketReply.Completed, callback, templates.Select(x => x.Content).ToArray()); + SendReply(EpPacketReply.Completed, callback, typeDefs.Select(x => x.Content).ToArray()); }; @@ -851,9 +851,9 @@ partial class EpConnection var (_, value) = Codec.ParseSync(dataType, Instance.Warehouse); - var classId = (UUID)value; + var typeId = (UUID)value; - var t = Instance.Warehouse.GetTypeDefById(classId); + var t = Instance.Warehouse.GetTypeDefById(typeId); if (t != null) { @@ -1018,7 +1018,7 @@ partial class EpConnection // return; //} - InvokeFunction(call.Value.Template, callback, results, EpPacketRequest.ProcedureCall, call.Value.Delegate.Target); + InvokeFunction(call.Value.Definition, callback, results, EpPacketRequest.ProcedureCall, call.Value.Delegate.Target); }).Error(x => { @@ -1033,7 +1033,7 @@ partial class EpConnection this.Socket.Unhold(); // @TODO: Make managers for procedure calls - InvokeFunction(call.Value.Template, callback, parsed, EpPacketRequest.ProcedureCall, call.Value.Delegate.Target); + InvokeFunction(call.Value.Definition, callback, parsed, EpPacketRequest.ProcedureCall, call.Value.Delegate.Target); } } @@ -1042,29 +1042,29 @@ partial class EpConnection var (offset, length, args) = DataDeserializer.LimitedCountListParser(data, dataType.Offset, dataType.ContentLength, Instance.Warehouse, 2); - var classId = new UUID((byte[])args[0]); + var typeId = new UUID((byte[])args[0]); var index = (byte)args[1]; - var template = Instance.Warehouse.GetTypeDefById(classId); + var typeDef = Instance.Warehouse.GetTypeDefById(typeId); - if (template == null) + if (typeDef == null) { SendError(ErrorType.Management, callback, (ushort)ExceptionCode.TypeDefNotFound); return; } - var ft = template.GetFunctionDefByIndex(index); + var fd = typeDef.GetFunctionDefByIndex(index); - if (ft == null) + if (fd == null) { // no function at this index SendError(ErrorType.Management, callback, (ushort)ExceptionCode.MethodNotFound); return; } - var fi = ft.MethodInfo; + var fi = fd.MethodInfo; if (fi == null) { @@ -1093,7 +1093,7 @@ partial class EpConnection // return; //} - InvokeFunction(ft, callback, results, EpPacketRequest.StaticCall, null); + InvokeFunction(fd, callback, results, EpPacketRequest.StaticCall, null); }).Error(x => { @@ -1110,7 +1110,7 @@ partial class EpConnection // @TODO: Make managers for static calls - InvokeFunction(ft, callback, parsed, EpPacketRequest.StaticCall, null); + InvokeFunction(fd, callback, parsed, EpPacketRequest.StaticCall, null); } } @@ -1715,9 +1715,9 @@ partial class EpConnection /// - /// Get the TypeSchema for a given class Id. + /// Get the TypeSchema for a given type Id. /// - /// Class GUID. + /// Type UUID. /// TypeSchema. public AsyncReply GetTypeDefById(UUID typeId) { @@ -1749,9 +1749,9 @@ partial class EpConnection public AsyncReply GetTypeDefByName(string typeName) { - var template = typeDefs.Values.FirstOrDefault(x => x.Name == typeName); - if (template != null) - return new AsyncReply(template); + var typeDef = typeDefs.Values.FirstOrDefault(x => x.Name == typeName); + if (typeDef != null) + return new AsyncReply(typeDef); if (typeDefsByNameRequests.ContainsKey(typeName)) return typeDefsByNameRequests[typeName]; @@ -1844,8 +1844,7 @@ partial class EpConnection /// /// Fetch a resource from the other end /// - /// Class GUID - /// Resource IdGuid classId + /// Resource Id /// DistributedResource public AsyncReply Fetch(uint id, uint[] requestSequence) { @@ -1901,7 +1900,7 @@ partial class EpConnection return; } - // ClassId, Age, Link, Hops, PropertyValue[] + // TypeId, Age, Link, Hops, PropertyValue[] var args = (object[])result; var typeId = (UUID)args[0]; var age = Convert.ToUInt64(args[1]); @@ -1957,7 +1956,7 @@ partial class EpConnection { GetTypeDefById(typeId).Then((tmp) => { - // ClassId, ResourceAge, ResourceLink, Content + // typeId, ResourceAge, ResourceLink, Content if (resource == null) { dr.ResourceDefinition = tmp; @@ -2024,7 +2023,7 @@ partial class EpConnection /// Create a new resource. /// /// Resource path. - /// Type template. + /// Type definition. /// Values for the resource properties. /// Resource attributes. /// New resource instance @@ -2097,13 +2096,13 @@ partial class EpConnection { SendNotification(EpPacketNotification.PropertyModified, info.Resource.Instance.Id, - info.PropertyTemplate.Index, + info.PropertyDef.Index, info.Value); } private void Instance_CustomEventOccurred(CustomEventOccurredInfo info) { - if (info.EventTemplate.Subscribable) + if (info.EventDef.Subscribable) { lock (subscriptionsLock) { @@ -2111,7 +2110,7 @@ partial class EpConnection if (!subscriptions.ContainsKey(info.Resource)) return; - if (!subscriptions[info.Resource].Contains(info.EventTemplate.Index)) + if (!subscriptions[info.Resource].Contains(info.EventDef.Index)) return; } } @@ -2119,14 +2118,14 @@ partial class EpConnection if (!info.Receivers(this.session)) return; - if (info.Resource.Instance.Applicable(this.session, ActionType.ReceiveEvent, info.EventTemplate, info.Issuer) == Ruling.Denied) + if (info.Resource.Instance.Applicable(this.session, ActionType.ReceiveEvent, info.EventDef, info.Issuer) == Ruling.Denied) return; // compose the packet SendNotification(EpPacketNotification.EventOccurred, info.Resource.Instance.Id, - info.EventTemplate.Index, + info.EventDef.Index, info.Value); } diff --git a/Esiur/Protocol/EpResource.cs b/Esiur/Protocol/EpResource.cs index 4b5e643..a84004e 100644 --- a/Esiur/Protocol/EpResource.cs +++ b/Esiur/Protocol/EpResource.cs @@ -142,7 +142,6 @@ public class EpResource : DynamicObject, IResource, INotifyPropertyChanged, IDyn /// Create a new distributed resource. /// /// Connection responsible for the distributed resource. - /// Resource template. /// Instance Id given by the other end. /// Resource age. public EpResource(EpConnection connection, uint instanceId, ulong age, string link) @@ -206,7 +205,7 @@ public class EpResource : DynamicObject, IResource, INotifyPropertyChanged, IDyn var ft = Instance.Definition.GetFunctionDefByIndex(index); if (ft == null) - throw new Exception("Function template not found."); + throw new Exception("Function definition not found."); if (ft.IsStatic) return connection.StaticCall(Instance.Definition.Id, index, args); diff --git a/Esiur/Protocol/EpServer.cs b/Esiur/Protocol/EpServer.cs index 2c54614..14be78f 100644 --- a/Esiur/Protocol/EpServer.cs +++ b/Esiur/Protocol/EpServer.cs @@ -170,14 +170,14 @@ public class EpServer : NetworkServer, IResource public struct CallInfo { - public FunctionDef Template; + public FunctionDef Definition; public Delegate Delegate; } public EpServer MapCall(string call, Delegate handler) { - var ft = FunctionDef.MakeFunctionDef(null, handler.Method, 0, call, null); - Calls.Add(call, new CallInfo() { Delegate = handler, Template = ft }); + var fd = FunctionDef.MakeFunctionDef(null, handler.Method, 0, call, null); + Calls.Add(call, new CallInfo() { Delegate = handler, Definition = fd }); return this; } diff --git a/Esiur/Proxy/ResourceGenerator.cs b/Esiur/Proxy/ResourceGenerator.cs index 8750d0d..c401ed1 100644 --- a/Esiur/Proxy/ResourceGenerator.cs +++ b/Esiur/Proxy/ResourceGenerator.cs @@ -44,7 +44,7 @@ namespace Esiur.Proxy .Collect() .Select( (list, y) => MergePartials(list)); - // 4) Generate: A) remote templates (from ImportAttribute URLs) + // 4) Generate: A) remote TypeDefs (from ImportAttribute URLs) context.RegisterSourceOutput(importUrls, (spc, urls) => { if (urls.Length == 0) return; @@ -57,9 +57,9 @@ namespace Esiur.Proxy var parts = TypeDefGenerator.urlRegex.Split(path); var con = Warehouse.Default.Get($"{parts[1]}://{parts[2]}").Wait(20000); - var templates = con.GetLinkDefinitions(parts[3]).Wait(60000); + var typeDefs = con.GetLinkDefinitions(parts[3]).Wait(60000); - EmitTemplates(spc, templates); + EmitTypeDefs(spc, typeDefs); } catch (Exception ex) { @@ -222,26 +222,26 @@ $@" public partial class {ci.Name} : IResource {{ } // === Emission helpers (ported from your original generator) === - private static void EmitTemplates(SourceProductionContext spc, TypeDef[] templates) + private static void EmitTypeDefs(SourceProductionContext spc, TypeDef[] typeDefs) { - foreach (var tmp in templates) + foreach (var typeDef in typeDefs) { - if (tmp.Kind == TypeDefKind.Resource) + if (typeDef.Kind == TypeDefKind.Resource) { - var source = TypeDefGenerator.GenerateClass(tmp, templates, false); - spc.AddSource(tmp.Name + ".g.cs", source); + var source = TypeDefGenerator.GenerateClass(typeDef, typeDefs, false); + spc.AddSource(typeDef.Name + ".g.cs", source); } - else if (tmp.Kind == TypeDefKind.Record) + else if (typeDef.Kind == TypeDefKind.Record) { - var source = TypeDefGenerator.GenerateRecord(tmp, templates); - spc.AddSource(tmp.Name + ".g.cs", source); + var source = TypeDefGenerator.GenerateRecord(typeDef, typeDefs); + spc.AddSource(typeDef.Name + ".g.cs", source); } } var typesFile = "using System; \r\n namespace Esiur { public static class Generated { public static Type[] Resources {get;} = new Type[] { " + - string.Join(",", templates.Where(x => x.Kind == TypeDefKind.Resource).Select(x => $"typeof({x.Name})")) + string.Join(",", typeDefs.Where(x => x.Kind == TypeDefKind.Resource).Select(x => $"typeof({x.Name})")) + " }; \r\n public static Type[] Records { get; } = new Type[] { " + - string.Join(",", templates.Where(x => x.Kind == TypeDefKind.Record).Select(x => $"typeof({x.Name})")) + string.Join(",", typeDefs.Where(x => x.Kind == TypeDefKind.Record).Select(x => $"typeof({x.Name})")) + " }; " + "\r\n } \r\n}"; diff --git a/Esiur/Proxy/TypeDefGenerator.cs b/Esiur/Proxy/TypeDefGenerator.cs index 94acc2e..9cf83a6 100644 --- a/Esiur/Proxy/TypeDefGenerator.cs +++ b/Esiur/Proxy/TypeDefGenerator.cs @@ -61,7 +61,7 @@ public static class TypeDefGenerator } - internal static string GenerateRecord(TypeDef typeDef, TypeDef[] templates) + internal static string GenerateRecord(TypeDef typeDef, TypeDef[] typeDefs) { var cls = typeDef.Name.Split('.'); @@ -82,13 +82,13 @@ public static class TypeDefGenerator } } - rt.AppendLine($"[ClassId(\"{typeDef.Id.Data.ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[TypeId(\"{typeDef.Id.Data.ToHex(0, 16, null)}\")]"); rt.AppendLine($"[Export] public class {className} : IRecord {{"); foreach (var p in typeDef.Properties) { - var ptTypeName = GetTypeName(p.ValueType, templates); + var pdTypeName = GetTypeName(p.ValueType, typeDefs); if (p.Annotations != null) @@ -100,7 +100,7 @@ public static class TypeDefGenerator } - rt.AppendLine($"public {ptTypeName} {p.Name} {{ get; set; }}"); + rt.AppendLine($"public {pdTypeName} {p.Name} {{ get; set; }}"); rt.AppendLine(); } @@ -109,9 +109,9 @@ public static class TypeDefGenerator return rt.ToString(); } - internal static string GenerateEnum(TypeDef template, TypeDef[] templates) + internal static string GenerateEnum(TypeDef typeDef, TypeDef[] typeDefs) { - var cls = template.Name.Split('.'); + var cls = typeDef.Name.Split('.'); var nameSpace = string.Join(".", cls.Take(cls.Length - 1)); var className = cls.Last(); @@ -121,18 +121,18 @@ public static class TypeDefGenerator rt.AppendLine("using System;\r\nusing Esiur.Resource;\r\nusing Esiur.Core;\r\nusing Esiur.Data;\r\nusing Esiur.Protocol;"); rt.AppendLine($"namespace {nameSpace} {{"); - if (template.Annotations != null) + if (typeDef.Annotations != null) { - foreach (var ann in template.Annotations) + foreach (var ann in typeDef.Annotations) { rt.AppendLine($"[Annotation({ToLiteral(ann.Key)}, {ToLiteral(ann.Value)})]"); } } - rt.AppendLine($"[ClassId(\"{template.Id.Data.ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[TypeId(\"{typeDef.Id.Data.ToHex(0, 16, null)}\")]"); rt.AppendLine($"[Export] public enum {className} {{"); - rt.AppendLine(String.Join(",\r\n", template.Constants.Select(x => $"{x.Name}={x.Value}"))); + rt.AppendLine(String.Join(",\r\n", typeDef.Constants.Select(x => $"{x.Name}={x.Value}"))); rt.AppendLine("\r\n}\r\n}"); @@ -140,34 +140,34 @@ public static class TypeDefGenerator } - static string GetTypeName(TRU representationType, TypeDef[] templates) + static string GetTypeName(TRU tru, TypeDef[] typeDefs) { string name; - if (representationType.Identifier == TRUIdentifier.TypedResource)// == DataType.Resource) - name = templates.First(x => x.Id == representationType.UUID && (x.Kind == TypeDefKind.Resource)).Name; - else if (representationType.Identifier == TRUIdentifier.TypedRecord) - name = templates.First(x => x.Id == representationType.UUID && x.Kind == TypeDefKind.Record).Name; - else if (representationType.Identifier == TRUIdentifier.Enum) - name = templates.First(x => x.Id == representationType.UUID && x.Kind == TypeDefKind.Enum).Name; - else if (representationType.Identifier == TRUIdentifier.TypedList) - name = GetTypeName(representationType.SubTypes[0], templates) + "[]"; - else if (representationType.Identifier == TRUIdentifier.TypedMap) - name = "Map<" + GetTypeName(representationType.SubTypes[0], templates) - + "," + GetTypeName(representationType.SubTypes[1], templates) + if (tru.Identifier == TRUIdentifier.TypedResource)// == DataType.Resource) + name = typeDefs.First(x => x.Id == tru.UUID && (x.Kind == TypeDefKind.Resource)).Name; + else if (tru.Identifier == TRUIdentifier.TypedRecord) + name = typeDefs.First(x => x.Id == tru.UUID && x.Kind == TypeDefKind.Record).Name; + else if (tru.Identifier == TRUIdentifier.Enum) + name = typeDefs.First(x => x.Id == tru.UUID && x.Kind == TypeDefKind.Enum).Name; + else if (tru.Identifier == TRUIdentifier.TypedList) + name = GetTypeName(tru.SubTypes[0], typeDefs) + "[]"; + else if (tru.Identifier == TRUIdentifier.TypedMap) + name = "Map<" + GetTypeName(tru.SubTypes[0], typeDefs) + + "," + GetTypeName(tru.SubTypes[1], typeDefs) + ">"; - else if (representationType.Identifier == TRUIdentifier.Tuple2 || - representationType.Identifier == TRUIdentifier.Tuple3 || - representationType.Identifier == TRUIdentifier.Tuple4 || - representationType.Identifier == TRUIdentifier.Tuple5 || - representationType.Identifier == TRUIdentifier.Tuple6 || - representationType.Identifier == TRUIdentifier.Tuple7) - name = "(" + String.Join(",", representationType.SubTypes.Select(x => GetTypeName(x, templates))) + else if (tru.Identifier == TRUIdentifier.Tuple2 || + tru.Identifier == TRUIdentifier.Tuple3 || + tru.Identifier == TRUIdentifier.Tuple4 || + tru.Identifier == TRUIdentifier.Tuple5 || + tru.Identifier == TRUIdentifier.Tuple6 || + tru.Identifier == TRUIdentifier.Tuple7) + name = "(" + String.Join(",", tru.SubTypes.Select(x => GetTypeName(x, typeDefs))) + ")"; else { - name = representationType.Identifier switch + name = tru.Identifier switch { TRUIdentifier.Dynamic => "object", TRUIdentifier.Bool => "bool", @@ -193,10 +193,10 @@ public static class TypeDefGenerator }; } - return (representationType.Nullable) ? name + "?" : name; + return (tru.Nullable) ? name + "?" : name; } - public static string GetTemplate(string url, string dir = null, bool tempDir = false, string username = null, string password = null, bool asyncSetters = false) + public static string GetTypes(string url, string dir = null, bool tempDir = false, string username = null, string password = null, bool asyncSetters = false) { try { @@ -215,7 +215,7 @@ public static class TypeDefGenerator if (string.IsNullOrEmpty(dir)) dir = path[2].Replace(":", "_"); - var templates = con.GetLinkDefinitions(path[3]).Wait(60000); + var typeDefs = con.GetLinkDefinitions(path[3]).Wait(60000); // no longer needed Warehouse.Default.Remove(con); @@ -231,22 +231,22 @@ public static class TypeDefGenerator } // make sources - foreach (var tmp in templates) + foreach (var td in typeDefs) { - if (tmp.Kind == TypeDefKind.Resource) + if (td.Kind == TypeDefKind.Resource) { - var source = GenerateClass(tmp, templates, asyncSetters); - File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + tmp.Name + ".g.cs", source); + var source = GenerateClass(td, typeDefs, asyncSetters); + File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + td.Name + ".g.cs", source); } - else if (tmp.Kind == TypeDefKind.Record) + else if (td.Kind == TypeDefKind.Record) { - var source = GenerateRecord(tmp, templates); - File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + tmp.Name + ".g.cs", source); + var source = GenerateRecord(td, typeDefs); + File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + td.Name + ".g.cs", source); } - else if (tmp.Kind == TypeDefKind.Enum) + else if (td.Kind == TypeDefKind.Enum) { - var source = GenerateEnum(tmp, templates); - File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + tmp.Name + ".g.cs", source); + var source = GenerateEnum(td, typeDefs); + File.WriteAllText(dstDir.FullName + Path.DirectorySeparatorChar + td.Name + ".g.cs", source); } } @@ -256,13 +256,13 @@ public static class TypeDefGenerator namespace Esiur { public static class Generated { public static Type[] Resources {get;} = new Type[] { " + - string.Join(",", templates.Where(x => x.Kind == TypeDefKind.Resource).Select(x => $"typeof({x.Name})")) + string.Join(",", typeDefs.Where(x => x.Kind == TypeDefKind.Resource).Select(x => $"typeof({x.Name})")) + @" }; public static Type[] Records { get; } = new Type[] { " + - string.Join(",", templates.Where(x => x.Kind == TypeDefKind.Record).Select(x => $"typeof({x.Name})")) + string.Join(",", typeDefs.Where(x => x.Kind == TypeDefKind.Record).Select(x => $"typeof({x.Name})")) + @" }; public static Type[] Enums { get; } = new Type[] { " + - string.Join(",", templates.Where(x => x.Kind == TypeDefKind.Enum).Select(x => $"typeof({x.Name})")) + string.Join(",", typeDefs.Where(x => x.Kind == TypeDefKind.Enum).Select(x => $"typeof({x.Name})")) + @" };" + "\r\n } \r\n}"; @@ -279,9 +279,9 @@ public static class TypeDefGenerator } } - internal static string GenerateClass(TypeDef template, TypeDef[] templates, bool asyncSetters) + internal static string GenerateClass(TypeDef typeDef, TypeDef[] typeDefs, bool asyncSetters) { - var cls = template.Name.Split('.'); + var cls = typeDef.Name.Split('.'); var nameSpace = string.Join(".", cls.Take(cls.Length - 1)); var className = cls.Last(); @@ -293,33 +293,33 @@ public static class TypeDefGenerator rt.AppendLine($"namespace {nameSpace} {{"); - if (template.Annotations != null) + if (typeDef.Annotations != null) { - foreach (var ann in template.Annotations) + foreach (var ann in typeDef.Annotations) { rt.AppendLine($"[Annotation({ToLiteral(ann.Key)}, {ToLiteral(ann.Value)})]"); } } - rt.AppendLine($"[ClassId(\"{template.Id.Data.ToHex(0, 16, null)}\")]"); + rt.AppendLine($"[TypeId(\"{typeDef.Id.Data.ToHex(0, 16, null)}\")]"); // extends - if (template.ParentId == null) + if (typeDef.ParentId == null) rt.AppendLine($"public class {className} : EpResource {{"); else - rt.AppendLine($"public class {className} : {templates.First(x => x.Id == template.ParentId && x.Kind == TypeDefKind.Resource).Name} {{"); + rt.AppendLine($"public class {className} : {typeDefs.First(x => x.Id == typeDef.ParentId && x.Kind == TypeDefKind.Resource).Name} {{"); rt.AppendLine($"public {className}(EpConnection connection, uint instanceId, ulong age, string link) : base(connection, instanceId, age, link) {{}}"); rt.AppendLine($"public {className}() {{}}"); - foreach (var f in template.Functions) + foreach (var f in typeDef.Functions) { if (f.Inherited) continue; - var rtTypeName = GetTypeName(f.ReturnType, templates); + var rtTypeName = GetTypeName(f.ReturnType, typeDefs); var positionalArgs = f.Arguments.Where((x) => !x.Optional).ToArray(); var optionalArgs = f.Arguments.Where((x) => x.Optional).ToArray(); @@ -339,11 +339,11 @@ public static class TypeDefGenerator if (positionalArgs.Length > 0) rt.Append(", " + - String.Join(", ", positionalArgs.Select((a) => GetTypeName(a.Type, templates) + " " + a.Name))); + String.Join(", ", positionalArgs.Select((a) => GetTypeName(a.Type, typeDefs) + " " + a.Name))); if (optionalArgs.Length > 0) rt.Append(", " + - String.Join(", ", optionalArgs.Select((a) => GetTypeName(a.Type.ToNullable(), templates) + " " + a.Name + " = null"))); + String.Join(", ", optionalArgs.Select((a) => GetTypeName(a.Type.ToNullable(), typeDefs) + " " + a.Name + " = null"))); } else @@ -352,14 +352,14 @@ public static class TypeDefGenerator if (positionalArgs.Length > 0) rt.Append( - String.Join(", ", positionalArgs.Select((a) => GetTypeName(a.Type, templates) + " " + a.Name))); + String.Join(", ", positionalArgs.Select((a) => GetTypeName(a.Type, typeDefs) + " " + a.Name))); if (optionalArgs.Length > 0) { if (positionalArgs.Length > 0) rt.Append(","); rt.Append( - String.Join(", ", optionalArgs.Select((a) => GetTypeName(a.Type.ToNullable(), templates) + " " + a.Name + " = null"))); + String.Join(", ", optionalArgs.Select((a) => GetTypeName(a.Type.ToNullable(), typeDefs) + " " + a.Name + " = null"))); } } @@ -378,7 +378,7 @@ public static class TypeDefGenerator rt.AppendLine($"var rt = new AsyncReply<{rtTypeName}>();"); if (f.IsStatic) - rt.AppendLine($"connection.StaticCall(Guid.Parse(\"{template.Id.ToString()}\"), {f.Index}, args)"); + rt.AppendLine($"connection.StaticCall(Guid.Parse(\"{typeDef.Id.ToString()}\"), {f.Index}, args)"); else rt.AppendLine($"_Invoke({f.Index}, args)"); @@ -389,7 +389,7 @@ public static class TypeDefGenerator } - foreach (var p in template.Properties) + foreach (var p in typeDef.Properties) { if (p.Inherited) continue; @@ -402,7 +402,7 @@ public static class TypeDefGenerator } } - var ptTypeName = GetTypeName(p.ValueType, templates); + var ptTypeName = GetTypeName(p.ValueType, typeDefs); rt.AppendLine($"[Export] public {ptTypeName} {p.Name} {{"); rt.AppendLine($"get => ({ptTypeName})properties[{p.Index}];"); if (asyncSetters) @@ -412,7 +412,7 @@ public static class TypeDefGenerator rt.AppendLine("}"); } - foreach (var c in template.Constants) + foreach (var c in typeDef.Constants) { if (c.Inherited) continue; @@ -423,12 +423,12 @@ public static class TypeDefGenerator rt.AppendLine($"[Annotation({ToLiteral(ann.Key)}, {ToLiteral(ann.Value)})]"); } - var ctTypeName = GetTypeName(c.ValueType, templates); + var ctTypeName = GetTypeName(c.ValueType, typeDefs); rt.AppendLine($"[Export] public const {ctTypeName} {c.Name} = {c.Value};"); } - if (template.Events.Length > 0) + if (typeDef.Events.Length > 0) { rt.AppendLine("protected override void _EmitEventByIndex(byte index, object args) {"); @@ -436,9 +436,9 @@ public static class TypeDefGenerator var eventsList = new StringBuilder(); - foreach (var e in template.Events) + foreach (var e in typeDef.Events) { - var etTypeName = GetTypeName(e.ArgumentType, templates); + var etTypeName = GetTypeName(e.ArgumentType, typeDefs); rt.AppendLine($"case {e.Index}: {e.Name}?.Invoke(({etTypeName})args); break;"); diff --git a/Esiur/README.md b/Esiur/README.md index b89cb30..67d58ae 100644 --- a/Esiur/README.md +++ b/Esiur/README.md @@ -143,10 +143,10 @@ In the above client example, we relied on Esiur support for dynamic objects, but Esiur has a self describing feature which comes with every language it supports, allowing the developer to fetch and generate classes that match the ones on the other side (i.e. server). -After installing the Esiur nuget package a new command is added to Visual Studio Package Console Manager that is called ***Get-Template***, which generates client side classes for robust static typing. +After installing the Esiur nuget package a new command is added to Visual Studio Package Console Manager that is called ***Get-Types***, which generates client side classes for robust static typing. ```ps -Get-Template EP://localhost/sys/hello +Get-Types ep://localhost/sys/hello ``` This will generate and add wrappers for all types needed by our resource. diff --git a/Esiur/Resource/ClassIdAttribute.cs b/Esiur/Resource/ClassIdAttribute.cs deleted file mode 100644 index 86779e8..0000000 --- a/Esiur/Resource/ClassIdAttribute.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Esiur.Data; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Esiur.Resource -{ - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum)] - public class ClassIdAttribute : Attribute - { - public UUID ClassId { get; private set; } - - public ClassIdAttribute(string classId) - { - var data = DC.FromHex(classId, null); - ClassId = new UUID(data); - } - } -} diff --git a/Esiur/Resource/CustomEventOccurredInfo.cs b/Esiur/Resource/CustomEventOccurredInfo.cs index a72b796..8390254 100644 --- a/Esiur/Resource/CustomEventOccurredInfo.cs +++ b/Esiur/Resource/CustomEventOccurredInfo.cs @@ -8,18 +8,18 @@ namespace Esiur.Resource; public class CustomEventOccurredInfo { - public readonly EventDef EventTemplate; + public readonly EventDef EventDef; public readonly IResource Resource; public readonly object Value; public readonly object Issuer; public readonly Func Receivers; - public string Name => EventTemplate.Name; + public string Name => EventDef.Name; - public CustomEventOccurredInfo(IResource resource, EventDef eventTemplate, Func receivers, object issuer, object value) + public CustomEventOccurredInfo(IResource resource, EventDef eventDef, Func receivers, object issuer, object value) { Resource = resource; - EventTemplate = eventTemplate; + EventDef = eventDef; Receivers = receivers; Issuer = issuer; Value = value; diff --git a/Esiur/Resource/IStore.cs b/Esiur/Resource/IStore.cs index 9e03c04..298017c 100644 --- a/Esiur/Resource/IStore.cs +++ b/Esiur/Resource/IStore.cs @@ -70,8 +70,8 @@ public interface IStore : IResource //AsyncReply GetPropertyRecord(IResource resource, string propertyName, ulong fromAge, ulong toAge); //AsyncReply GetPropertyRecordByDate(IResource resource, string propertyName, DateTime fromDate, DateTime toDate); - //AsyncReply> GetRecord(IResource resource, ulong fromAge, ulong toAge); - // AsyncReply> GetRecordByDate(IResource resource, DateTime fromDate, DateTime toDate); + //AsyncReply> GetRecord(IResource resource, ulong fromAge, ulong toAge); + // AsyncReply> GetRecordByDate(IResource resource, DateTime fromDate, DateTime toDate); //AsyncReply> GetRecord(IResource resource, DateTime fromDate, DateTime toDate); } diff --git a/Esiur/Resource/PropertyModificationInfo.cs b/Esiur/Resource/PropertyModificationInfo.cs index 3be52ac..bb28714 100644 --- a/Esiur/Resource/PropertyModificationInfo.cs +++ b/Esiur/Resource/PropertyModificationInfo.cs @@ -9,15 +9,15 @@ namespace Esiur.Resource; public struct PropertyModificationInfo { public readonly IResource Resource; - public readonly PropertyDef PropertyTemplate; - public string Name => PropertyTemplate.Name; + public readonly PropertyDef PropertyDef; + public string Name => PropertyDef.Name; public readonly ulong Age; public object Value; - public PropertyModificationInfo(IResource resource, PropertyDef propertyTemplate, object value, ulong age) + public PropertyModificationInfo(IResource resource, PropertyDef propertyDef, object value, ulong age) { Resource = resource; - PropertyTemplate = propertyTemplate; + PropertyDef = propertyDef; Age = age; Value = value; } diff --git a/Esiur/Resource/TypeIdAttribute.cs b/Esiur/Resource/TypeIdAttribute.cs new file mode 100644 index 0000000..dcc52c8 --- /dev/null +++ b/Esiur/Resource/TypeIdAttribute.cs @@ -0,0 +1,19 @@ +using Esiur.Data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Resource +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum)] + public class TypeIdAttribute : Attribute + { + public UUID Id { get; private set; } + + public TypeIdAttribute(string id) + { + var data = DC.FromHex(id, null); + Id = new UUID(data); + } + } +} diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index ec7be16..55d4178 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -515,12 +515,12 @@ public class Warehouse } - /// - /// Get a TypeDef by type from the typeDefs warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. - /// - /// .Net type. - /// Resource template. - public TypeDef GetTypeDefByType(Type type) + /// + /// Get a TypeDef by type from the warehouse. If not in the warehouse, a new TypeDef is created and added to the warehouse. + /// + /// .Net type. + /// Resource TypeDef. + public TypeDef GetTypeDefByType(Type type) { if (!(type.IsClass || type.IsEnum)) return null; @@ -531,61 +531,61 @@ public class Warehouse || baseType == typeof(IRecord)) return null; - TypeDefKind schemaKind; + TypeDefKind typeDefKind; if (Codec.ImplementsInterface(type, typeof(IResource))) - schemaKind = TypeDefKind.Resource; + typeDefKind = TypeDefKind.Resource; else if (Codec.ImplementsInterface(type, typeof(IRecord))) - schemaKind = TypeDefKind.Record; + typeDefKind = TypeDefKind.Record; else if (type.IsEnum) - schemaKind = TypeDefKind.Enum; + typeDefKind = TypeDefKind.Enum; else return null; - var schema = typeDefs[schemaKind].Values.FirstOrDefault(x => x.DefinedType == baseType); - if (schema != null) - return schema; + var typeDef = typeDefs[typeDefKind].Values.FirstOrDefault(x => x.DefinedType == baseType); + if (typeDef != null) + return typeDef; - // create new template for type - schema = new TypeDef(baseType, this); - TypeDef.GetDependencies(schema, this); + // create new TypeDef for type + typeDef = new TypeDef(baseType, this); + TypeDef.GetDependencies(typeDef, this); - return schema; + return typeDef; } /// - /// Get a schema by class Id from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. + /// Get a TypeDef by TypeId from the warehouse. If not in the warehouse, a new TypeDef is created and added to the warehouse. /// - /// Class Id. - /// Resource template. - public TypeDef GetTypeDefById(UUID typeId, TypeDefKind? templateType = null) + /// typeId. + /// TypeDef. + public TypeDef GetTypeDefById(UUID typeId, TypeDefKind? typeDefKind = null) { - if (templateType == null) + if (typeDefKind == null) { // look into resources - var template = typeDefs[TypeDefKind.Resource][typeId]; - if (template != null) - return template; + var typeDef = typeDefs[TypeDefKind.Resource][typeId]; + if (typeDef != null) + return typeDef; // look into records - template = typeDefs[TypeDefKind.Record][typeId]; - if (template != null) - return template; + typeDef = typeDefs[TypeDefKind.Record][typeId]; + if (typeDef != null) + return typeDef; // look into enums - template = typeDefs[TypeDefKind.Enum][typeId]; - return template; + typeDef = typeDefs[TypeDefKind.Enum][typeId]; + return typeDef; } else - return typeDefs[templateType.Value][typeId]; + return typeDefs[typeDefKind.Value][typeId]; } /// - /// Get a template by class name from the templates warehouse. If not in the warehouse, a new ResourceTemplate is created and added to the warehouse. + /// Get a TypeDef by type name . If not in the warehouse, a new TypeDef is created and added to the warehouse. /// - /// Class name. - /// Resource template. + /// Class full name. + /// TypeDef. public TypeDef GetTypeDefByName(string typeName, TypeDefKind? typeDefKind = null) { if (typeDefKind == null) diff --git a/Esiur/Security/Permissions/ActionType.cs b/Esiur/Security/Permissions/ActionType.cs index 617e2a1..1253aeb 100644 --- a/Esiur/Security/Permissions/ActionType.cs +++ b/Esiur/Security/Permissions/ActionType.cs @@ -46,5 +46,5 @@ public enum ActionType RemoveChild, Rename, ReceiveEvent, - ViewTemplate + ViewTypeDef } diff --git a/Esiur/Tools/Esiur.psd1 b/Esiur/Tools/Esiur.psd1 index 3d425cd..1f4a265 100644 Binary files a/Esiur/Tools/Esiur.psd1 and b/Esiur/Tools/Esiur.psd1 differ diff --git a/Esiur/Tools/Esiur.psm1 b/Esiur/Tools/Esiur.psm1 index 31c71be..d8e8ed2 100644 --- a/Esiur/Tools/Esiur.psm1 +++ b/Esiur/Tools/Esiur.psm1 @@ -1,11 +1,11 @@ -function Get-Template($url, $dir, $username, $password, $asyncSetters) +function Get-Types($url, $dir, $username, $password, $asyncSetters) { $lib = Resolve-Path -Path "$($PSScriptRoot)\..\lib\netstandard2.0\Esiur.dll" #write-host "Lib is at $($lib)" $assembly = [Reflection.Assembly]::LoadFile($lib) - $tempPath = [Esiur.Proxy.TemplateGenerator]::GetTemplate($url, $dir, $true, $username,$password, $asyncSetters); + $tempPath = [Esiur.Proxy.TypeDefGenerator]::GetTypes($url, $dir, $true, $username,$password, $asyncSetters); $startupProject = GetStartupProject