mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-26 21:13:13 +00:00
Added JSON
This commit is contained in:
@ -29,11 +29,14 @@ using System.Text;
|
||||
using Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Esiur.Resource
|
||||
{
|
||||
public delegate bool QueryFilter<T>(T value);
|
||||
|
||||
//[JsonConverter(typeof(ResourceJsonConverter))]
|
||||
|
||||
public interface IResource : IDestructible///, INotifyPropertyChanged
|
||||
{
|
||||
|
||||
|
@ -13,6 +13,7 @@ using Esiur.Resource.Template;
|
||||
using Esiur.Security.Authority;
|
||||
using Esiur.Proxy;
|
||||
using Esiur.Core;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Esiur.Resource
|
||||
{
|
||||
@ -20,7 +21,8 @@ namespace Esiur.Resource
|
||||
{
|
||||
string name;
|
||||
|
||||
// public int IntVal { get; set; }
|
||||
// public int IntVal { get; set; }
|
||||
|
||||
|
||||
WeakReference<IResource> resource;
|
||||
IStore store;
|
||||
@ -115,7 +117,7 @@ namespace Esiur.Resource
|
||||
|
||||
return rt;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
var st = new Structure();
|
||||
@ -183,7 +185,7 @@ namespace Esiur.Resource
|
||||
if (at != null)
|
||||
if (at.Info.CanWrite)
|
||||
at.Info.SetValue(res, DC.CastConvert(kv.Value, at.Info.PropertyType));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -426,6 +428,15 @@ namespace Esiur.Resource
|
||||
return true;
|
||||
}
|
||||
|
||||
public string ToJson()
|
||||
{
|
||||
IResource res;
|
||||
if (resource.TryGetTarget(out res))
|
||||
return JsonSerializer.Serialize(res, Global.SerializeOptions);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Export all properties with ResourceProperty attributed as bytes array.
|
||||
/// </summary>
|
||||
@ -436,28 +447,18 @@ namespace Esiur.Resource
|
||||
|
||||
foreach (var pt in template.Properties)
|
||||
{
|
||||
/*
|
||||
#if NETSTANDARD
|
||||
var pi = resource.GetType().GetTypeInfo().GetProperty(pt.Name);
|
||||
#else
|
||||
var pi = resource.GetType().GetProperty(pt.Name);
|
||||
#endif
|
||||
*/
|
||||
IResource res;
|
||||
|
||||
//if (pt.Serilize)
|
||||
//{
|
||||
IResource res;
|
||||
if (resource.TryGetTarget(out res))
|
||||
{
|
||||
var rt = pt.Info.GetValue(res, null);// pt.Serilize ? pt.Info.GetValue(res, null) : null;
|
||||
|
||||
props.Add(new PropertyValue(rt, ages[pt.Index], modificationDates[pt.Index]));
|
||||
}
|
||||
//}
|
||||
if (resource.TryGetTarget(out res))
|
||||
{
|
||||
var rt = pt.Info.GetValue(res, null);
|
||||
props.Add(new PropertyValue(rt, ages[pt.Index], modificationDates[pt.Index]));
|
||||
}
|
||||
}
|
||||
|
||||
return props.ToArray();
|
||||
}
|
||||
|
||||
/*
|
||||
public bool Deserialize(byte[] data, uint offset, uint length)
|
||||
{
|
||||
@ -712,7 +713,7 @@ namespace Esiur.Resource
|
||||
if (this.resource.TryGetTarget(out res))
|
||||
{
|
||||
//if (!(store is null))
|
||||
return store.Children<T>(res, name);
|
||||
return store.Children<T>(res, name);
|
||||
//else
|
||||
// return (res as IStore).Children<T>(res, name);
|
||||
}
|
||||
@ -824,14 +825,14 @@ namespace Esiur.Resource
|
||||
if (this.resource.TryGetTarget(out res))
|
||||
{
|
||||
//return store.Applicable(res, session, action, member, inquirer);
|
||||
|
||||
|
||||
foreach (IPermissionsManager manager in managers)
|
||||
{
|
||||
var r = manager.Applicable(res, session, action, member, inquirer);
|
||||
if (r != Ruling.DontCare)
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return Ruling.DontCare;
|
||||
@ -873,14 +874,14 @@ namespace Esiur.Resource
|
||||
this.template = customTemplate;
|
||||
else
|
||||
this.template = Warehouse.GetTemplate(resource.GetType());
|
||||
|
||||
// set ages
|
||||
|
||||
// set ages
|
||||
for (byte i = 0; i < template.Properties.Length; i++)
|
||||
{
|
||||
ages.Add(0);
|
||||
modificationDates.Add(DateTime.MinValue);
|
||||
}
|
||||
|
||||
|
||||
// connect events
|
||||
Type t = ResourceProxy.GetBaseType(resource);
|
||||
|
||||
@ -903,9 +904,9 @@ namespace Esiur.Resource
|
||||
if (evt.Info.EventHandlerType == typeof(ResourceEventHanlder))
|
||||
{
|
||||
|
||||
// var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
|
||||
// if (ca.Length == 0)
|
||||
// continue;
|
||||
// var ca = (ResourceEvent[])evt.GetCustomAttributes(typeof(ResourceEvent), true);
|
||||
// if (ca.Length == 0)
|
||||
// continue;
|
||||
|
||||
ResourceEventHanlder proxyDelegate = (args) => EmitResourceEvent(evt.Name, args);
|
||||
evt.Info.AddEventHandler(resource, proxyDelegate);
|
||||
@ -920,7 +921,7 @@ namespace Esiur.Resource
|
||||
CustomResourceEventHanlder proxyDelegate = (issuer, receivers, args) => EmitCustomResourceEvent(issuer, receivers, evt.Name, args);
|
||||
evt.Info.AddEventHandler(resource, proxyDelegate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
else if (evt.EventHandlerType == typeof(CustomUsersEventHanlder))
|
||||
@ -959,7 +960,7 @@ namespace Esiur.Resource
|
||||
|
||||
//IQueryable<IResource> Children => store.GetChildren(this);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* private void Children_OnRemoved(Instance parent, IResource value)
|
||||
{
|
||||
|
@ -47,9 +47,9 @@ namespace Esiur.Resource
|
||||
|
||||
public abstract AsyncReply<bool> Trigger(ResourceTrigger trigger);
|
||||
|
||||
public T New(string name = null, object attributes = null, object properties = null)
|
||||
public async AsyncReply<T> New(string name = null, object attributes = null, object properties = null)
|
||||
{
|
||||
var resource = Warehouse.New<T>(name, this, null, null, attributes, properties);
|
||||
var resource = await Warehouse.New<T>(name, this, null, null, attributes, properties);
|
||||
resource.Instance.Managers.AddRange(this.Instance.Managers.ToArray());
|
||||
return resource;
|
||||
}
|
||||
|
@ -37,6 +37,7 @@ using System.Text.RegularExpressions;
|
||||
using Esiur.Misc;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
|
||||
namespace Esiur.Resource
|
||||
{
|
||||
@ -62,7 +63,7 @@ namespace Esiur.Resource
|
||||
public static event StoreConnectedEvent StoreConnected;
|
||||
public static event StoreDisconnectedEvent StoreDisconnected;
|
||||
|
||||
public delegate IStore ProtocolInstance(string name, object properties);
|
||||
public delegate AsyncReply<IStore> ProtocolInstance(string name, object properties);
|
||||
|
||||
public static KeyList<string, ProtocolInstance> Protocols { get; } = GetSupportedProtocols();
|
||||
|
||||
@ -73,7 +74,7 @@ namespace Esiur.Resource
|
||||
static KeyList<string, ProtocolInstance> GetSupportedProtocols()
|
||||
{
|
||||
var rt = new KeyList<string, ProtocolInstance>();
|
||||
rt.Add("iip", (name, props) => Warehouse.New<DistributedConnection>(name, null, null, null, props));
|
||||
rt.Add("iip", async (name, attributes) => await Warehouse.New<DistributedConnection>(name, null, null, null, attributes));
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -378,68 +379,100 @@ namespace Esiur.Resource
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns>Resource instance.</returns>
|
||||
public static AsyncReply<IResource> Get(string path, object attributes = null, IResource parent = null, IPermissionsManager manager = null)
|
||||
public static async AsyncReply<IResource> Get(string path, object attributes = null, IResource parent = null, IPermissionsManager manager = null)
|
||||
{
|
||||
var rt = new AsyncReply<IResource>();
|
||||
//var rt = new AsyncReply<IResource>();
|
||||
|
||||
// Should we create a new store ?
|
||||
|
||||
if (urlRegex.IsMatch(path))
|
||||
{
|
||||
|
||||
//if (path.Contains("://"))
|
||||
//{
|
||||
var url = urlRegex.Split(path);
|
||||
//var url = path.Split(new string[] { "://" }, 2, StringSplitOptions.None);
|
||||
//var hostname = url[1].Split(new char[] { '/' }, 2)[0];
|
||||
//var pathname = string.Join("/", url[1].Split(new char[] { '/' }).Skip(1));
|
||||
|
||||
|
||||
if (Protocols.ContainsKey(url[1]))
|
||||
{
|
||||
if (!warehouseIsOpen)
|
||||
await Open();
|
||||
|
||||
var handler = Protocols[url[1]];
|
||||
var store = await handler(url[2], attributes);
|
||||
|
||||
var store = handler(url[2], attributes);
|
||||
|
||||
store.Trigger(ResourceTrigger.Open).Then(x =>
|
||||
try
|
||||
{
|
||||
|
||||
warehouseIsOpen = true;
|
||||
Put(store, url[2], null, parent, null, 0, manager, attributes);
|
||||
//await Put(store, url[2], null, parent, null, 0, manager, attributes);
|
||||
|
||||
if (url[3].Length > 0 && url[3] != "")
|
||||
store.Get(url[3]).Then(r =>
|
||||
{
|
||||
rt.Trigger(r);
|
||||
}).Error(e =>
|
||||
{
|
||||
Warehouse.Remove(store);
|
||||
rt.TriggerError(e);
|
||||
});
|
||||
return await store.Get(url[3]);
|
||||
else
|
||||
rt.Trigger(store);
|
||||
}).Error(e =>
|
||||
{
|
||||
rt.TriggerError(e);
|
||||
//Warehouse.Remove(store);
|
||||
});
|
||||
return store;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Warehouse.Remove(store);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
|
||||
// store.Get(url[3]).Then(r =>
|
||||
// {
|
||||
// rt.Trigger(r);
|
||||
// }).Error(e =>
|
||||
// {
|
||||
// Warehouse.Remove(store);
|
||||
// rt.TriggerError(e);
|
||||
// });
|
||||
// else
|
||||
// rt.Trigger(store);
|
||||
|
||||
// store.Trigger(ResourceTrigger.Open).Then(x =>
|
||||
// {
|
||||
|
||||
// warehouseIsOpen = true;
|
||||
// await Put(store, url[2], null, parent, null, 0, manager, attributes);
|
||||
|
||||
// if (url[3].Length > 0 && url[3] != "")
|
||||
// store.Get(url[3]).Then(r =>
|
||||
// {
|
||||
// rt.Trigger(r);
|
||||
// }).Error(e =>
|
||||
// {
|
||||
// Warehouse.Remove(store);
|
||||
// rt.TriggerError(e);
|
||||
// });
|
||||
// else
|
||||
// rt.Trigger(store);
|
||||
// }).Error(e =>
|
||||
// {
|
||||
// rt.TriggerError(e);
|
||||
// //Warehouse.Remove(store);
|
||||
// });
|
||||
|
||||
// return rt;
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
Query(path).Then(rs =>
|
||||
{
|
||||
// rt.TriggerError(new Exception());
|
||||
if (rs != null && rs.Length > 0)
|
||||
rt.Trigger(rs.First());
|
||||
else
|
||||
rt.Trigger(null);
|
||||
});
|
||||
//await Query(path).Then(rs =>
|
||||
//{
|
||||
// // rt.TriggerError(new Exception());
|
||||
// if (rs != null && rs.Length > 0)
|
||||
// rt.Trigger(rs.First());
|
||||
// else
|
||||
// rt.Trigger(null);
|
||||
//});
|
||||
|
||||
return rt;
|
||||
//return rt;
|
||||
|
||||
var res = await Query(path);
|
||||
|
||||
if (res.Length == 0)
|
||||
return null;
|
||||
else
|
||||
return res.First();
|
||||
|
||||
}
|
||||
|
||||
@ -450,7 +483,7 @@ namespace Esiur.Resource
|
||||
/// <param name="name">Resource name.</param>
|
||||
/// <param name="store">IStore that manages the resource. Can be null if the resource is a store.</param>
|
||||
/// <param name="parent">Parent resource. if not presented the store becomes the parent for the resource.</param>
|
||||
public static void Put(IResource resource, string name, IStore store = null, IResource parent = null, ResourceTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null)
|
||||
public static async AsyncReply<bool> Put(IResource resource, string name, IStore store = null, IResource parent = null, ResourceTemplate customTemplate = null, ulong age = 0, IPermissionsManager manager = null, object attributes = null)
|
||||
{
|
||||
if (resource.Instance != null)
|
||||
throw new Exception("Resource has a store.");
|
||||
@ -522,13 +555,14 @@ namespace Esiur.Resource
|
||||
//else
|
||||
|
||||
|
||||
store.Put(resource);
|
||||
if (!await store.Put(resource))
|
||||
return false;
|
||||
|
||||
|
||||
if (parent != null)
|
||||
{
|
||||
parent.Instance.Store.AddChild(parent, resource);
|
||||
store.AddParent(resource, parent);
|
||||
await parent.Instance.Store.AddChild(parent, resource);
|
||||
await store.AddParent(resource, parent);
|
||||
//store.AddChild(parent, resource);
|
||||
|
||||
}
|
||||
@ -542,11 +576,19 @@ namespace Esiur.Resource
|
||||
resources.TryAdd(resource.Instance.Id, resourceReference);
|
||||
|
||||
if (warehouseIsOpen)
|
||||
resource.Trigger(ResourceTrigger.Initialize);
|
||||
{
|
||||
await resource.Trigger(ResourceTrigger.Initialize);
|
||||
if (resource is IStore)
|
||||
await resource.Trigger(ResourceTrigger.Open);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public static IResource New(Type type, string name = null, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
|
||||
public static async AsyncReply<IResource> New(Type type, string name = null, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
|
||||
{
|
||||
type = ResourceProxy.GetProxy(type);
|
||||
|
||||
@ -608,16 +650,19 @@ namespace Esiur.Resource
|
||||
}
|
||||
|
||||
if (store != null || parent != null || res is IStore)
|
||||
Put(res, name, store, parent, null, 0, manager, attributes);
|
||||
{
|
||||
if (!await Put(res, name, store, parent, null, 0, manager, attributes))
|
||||
return null;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public static T New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
|
||||
public static async AsyncReply<T> New<T>(string name, IStore store = null, IResource parent = null, IPermissionsManager manager = null, object attributes = null, object properties = null)
|
||||
where T : IResource
|
||||
{
|
||||
return (T)New(typeof(T), name, store, parent, manager, attributes, properties);
|
||||
return (T)(await New(typeof(T), name, store, parent, manager, attributes, properties));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user