From 0e865406ebe8a2bef2601660e72024ded6b61210 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Tue, 7 Nov 2023 16:31:23 +0300 Subject: [PATCH] events --- Esiur/Net/IIP/DistributedConnection.cs | 4 ++-- Esiur/Proxy/ResourceGenerator.cs | 23 ++++++++++++------- Esiur/Proxy/ResourceGeneratorReceiver.cs | 2 +- Esiur/Proxy/ResourceProxy.cs | 2 +- ...{PublicAttribute.cs => ExportAttribute.cs} | 4 ++-- ...PrivateAttribute.cs => IgnoreAttribute.cs} | 2 +- Esiur/Resource/Instance.cs | 2 ++ Esiur/Resource/Template/TypeTemplate.cs | 14 +++++------ Esiur/Resource/Warehouse.cs | 4 +--- 9 files changed, 32 insertions(+), 25 deletions(-) rename Esiur/Resource/{PublicAttribute.cs => ExportAttribute.cs} (79%) rename Esiur/Resource/{PrivateAttribute.cs => IgnoreAttribute.cs} (70%) diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 5cbfaff..b128540 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -120,7 +120,7 @@ public partial class DistributedConnection : NetworkConnection, IStore public DistributedServer Server { get; internal set; } - [Public] public virtual ConnectionStatus Status { get; set; } + [Export] public virtual ConnectionStatus Status { get; set; } public bool Remove(IResource resource) { @@ -356,7 +356,7 @@ public partial class DistributedConnection : NetworkConnection, IStore keepAliveTimer.Elapsed += KeepAliveTimer_Elapsed; } - [Public] public virtual uint Jitter { get; set; } + [Export] public virtual uint Jitter { get; set; } public uint KeepAliveTime { get; set; } = 10; diff --git a/Esiur/Proxy/ResourceGenerator.cs b/Esiur/Proxy/ResourceGenerator.cs index d6bc033..1fa9c7e 100644 --- a/Esiur/Proxy/ResourceGenerator.cs +++ b/Esiur/Proxy/ResourceGenerator.cs @@ -74,13 +74,12 @@ public class ResourceGenerator : ISourceGenerator - static string SuggestPropertyName(string propertyName) + public static string SuggestExportName(string fieldName) { - if (Char.IsUpper(propertyName[0])) - return propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1); + if (Char.IsUpper(fieldName[0])) + return fieldName.Substring(0, 1).ToLower() + fieldName.Substring(1); else - return propertyName.Substring(0, 1).ToUpper() + propertyName.Substring(1); - + return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1); } public void Execute(GeneratorExecutionContext context) @@ -173,16 +172,24 @@ namespace { ci.ClassSymbol.ContainingNamespace.ToDisplayString() } {{ foreach (var f in ci.Fields) { - var givenName = f.GetAttributes().Where(x => x.AttributeClass.Name == "PublicAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value as string; + var givenName = f.GetAttributes().Where(x => x.AttributeClass.Name == "ExportAttribute").FirstOrDefault()?.ConstructorArguments.FirstOrDefault().Value as string; var fn = f.Name; - var pn =string.IsNullOrEmpty(givenName) ? SuggestPropertyName(fn): givenName; + var pn = string.IsNullOrEmpty(givenName) ? SuggestExportName(fn) : givenName; //System.IO.File.AppendAllText("c:\\gen\\fields.txt", fn + " -> " + pn + "\r\n"); // copy attributes var attrs = string.Join("\r\n\t", f.GetAttributes().Select(x => $"[{x.ToString()}]")); - code += $"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n"; + //Debugger.Launch(); + if (f.Type.Name.StartsWith("ResourceEventHandler") || f.Type.Name.StartsWith("CustomResourceEventHandler")) + { + code += $"\t{attrs}\r\n\t public event {f.Type} {pn};\r\n"; + } + else + { + code += $"\t{attrs}\r\n\t public {f.Type} {pn} {{ \r\n\t\t get => {fn}; \r\n\t\t set {{ \r\n\t\t this.{fn} = value; \r\n\t\t Instance?.Modified(); \r\n\t\t}}\r\n\t}}\r\n"; + } } code += "}}\r\n"; diff --git a/Esiur/Proxy/ResourceGeneratorReceiver.cs b/Esiur/Proxy/ResourceGeneratorReceiver.cs index 7c9d6c5..dfee955 100644 --- a/Esiur/Proxy/ResourceGeneratorReceiver.cs +++ b/Esiur/Proxy/ResourceGeneratorReceiver.cs @@ -50,7 +50,7 @@ public class ResourceGeneratorReceiver : ISyntaxContextReceiver var fields = cds.Members.Where(x => x is FieldDeclarationSyntax) .Select(x => context.SemanticModel.GetDeclaredSymbol((x as FieldDeclarationSyntax).Declaration.Variables.First()) as IFieldSymbol) .Where(x => !x.IsConst) - .Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.PublicAttribute")) + .Where(x => x.GetAttributes().Any(a => a.AttributeClass.ToDisplayString() == "Esiur.Resource.ExportAttribute")) .ToArray(); //if (!Debugger.IsAttached) diff --git a/Esiur/Proxy/ResourceProxy.cs b/Esiur/Proxy/ResourceProxy.cs index 35e4e74..3fce124 100644 --- a/Esiur/Proxy/ResourceProxy.cs +++ b/Esiur/Proxy/ResourceProxy.cs @@ -73,7 +73,7 @@ public static class ResourceProxy var props = from p in typeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public) where p.CanWrite && p.SetMethod.IsVirtual && !p.SetMethod.IsFinal && - p.GetCustomAttribute(false) != null + p.GetCustomAttribute(false) != null select p; #else diff --git a/Esiur/Resource/PublicAttribute.cs b/Esiur/Resource/ExportAttribute.cs similarity index 79% rename from Esiur/Resource/PublicAttribute.cs rename to Esiur/Resource/ExportAttribute.cs index fe6ead6..1e15899 100644 --- a/Esiur/Resource/PublicAttribute.cs +++ b/Esiur/Resource/ExportAttribute.cs @@ -6,11 +6,11 @@ namespace Esiur.Resource; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Event | AttributeTargets.Class | AttributeTargets.Enum)] -public class PublicAttribute : Attribute +public class ExportAttribute : Attribute { public string Name { get; set; } - public PublicAttribute(string name = null) + public ExportAttribute(string name = null) { Name = name; } diff --git a/Esiur/Resource/PrivateAttribute.cs b/Esiur/Resource/IgnoreAttribute.cs similarity index 70% rename from Esiur/Resource/PrivateAttribute.cs rename to Esiur/Resource/IgnoreAttribute.cs index be4c869..406134b 100644 --- a/Esiur/Resource/PrivateAttribute.cs +++ b/Esiur/Resource/IgnoreAttribute.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Text; namespace Esiur.Resource; -public class PrivateAttribute : Attribute +public class IgnoreAttribute : Attribute { } diff --git a/Esiur/Resource/Instance.cs b/Esiur/Resource/Instance.cs index 702e96d..b4d2e6a 100644 --- a/Esiur/Resource/Instance.cs +++ b/Esiur/Resource/Instance.cs @@ -588,6 +588,8 @@ public class Instance } } + + // internal void EmitResourceEvent(string name, string[] users, DistributedConnection[] connections, object[] args) internal void EmitCustomResourceEvent(object issuer, Func receivers, EventTemplate eventTemplate, object value) diff --git a/Esiur/Resource/Template/TypeTemplate.cs b/Esiur/Resource/Template/TypeTemplate.cs index 9298929..2a1c66a 100644 --- a/Esiur/Resource/Template/TypeTemplate.cs +++ b/Esiur/Resource/Template/TypeTemplate.cs @@ -373,7 +373,7 @@ public class TypeTemplate - public static ConstantTemplate MakeConstantTemplate(Type type, FieldInfo ci, PublicAttribute publicAttr, byte index = 0, TypeTemplate typeTemplate = null) + public static ConstantTemplate MakeConstantTemplate(Type type, FieldInfo ci, ExportAttribute exportAttr, byte index = 0, TypeTemplate typeTemplate = null) { var annotationAttr = ci.GetCustomAttribute(true); var nullableAttr = ci.GetCustomAttribute(true); @@ -388,7 +388,7 @@ public class TypeTemplate if (typeTemplate.Type == TemplateType.Enum) value = Convert.ChangeType(value, ci.FieldType.GetEnumUnderlyingType()); - var ct = new ConstantTemplate(typeTemplate, index, publicAttr?.Name ?? ci.Name, ci.DeclaringType != type, valueType, value, annotationAttr?.Annotation); + var ct = new ConstantTemplate(typeTemplate, index, exportAttr?.Name ?? ci.Name, ci.DeclaringType != type, valueType, value, annotationAttr?.Annotation); return ct; @@ -589,7 +589,7 @@ public class TypeTemplate while (type != null) { - var classIsPublic = type.IsEnum || (type.GetCustomAttribute() != null); + var classIsPublic = type.IsEnum || (type.GetCustomAttribute() != null); if (classIsPublic) { @@ -598,11 +598,11 @@ public class TypeTemplate .Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method) .Where(x => !(x is FieldInfo c && !c.IsStatic)) - .Where(x => x.GetCustomAttribute() == null) + .Where(x => x.GetCustomAttribute() == null) .Where(x => !(x is MethodInfo m && m.IsSpecialName)) .Select(x => new MemberData() { - Name = x.GetCustomAttribute()?.Name ?? x.Name, + Name = x.GetCustomAttribute()?.Name ?? x.Name, Info = x, Order = order }) @@ -618,11 +618,11 @@ public class TypeTemplate .Where(x => x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Event || x.MemberType == MemberTypes.Method) .Where(x => !(x is FieldInfo c && !c.IsStatic)) - .Where(x => x.GetCustomAttribute() != null) + .Where(x => x.GetCustomAttribute() != null) .Where(x => !(x is MethodInfo m && m.IsSpecialName)) .Select(x => new MemberData { - Name = x.GetCustomAttribute()?.Name ?? x.Name, + Name = x.GetCustomAttribute()?.Name ?? x.Name, Info = x, Order = order }) diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index 0a108ae..4656536 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -375,8 +375,6 @@ public static class Warehouse public static async AsyncReply Query(string path) { - var rt = new AsyncReply(); - var p = path.Trim().Split('/'); IResource resource; @@ -512,7 +510,7 @@ public static class Warehouse var res = await Query(path); - if (res.Length == 0) + if (res == null || res.Length == 0) return default(T); else return (T)res.First();