using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core
{
///
/// Represents a path that points to a property in data model
///
public class DataModelPath : IStorageModel, IDisposable
{
private readonly LinkedList _segments;
private Expression>? _accessorLambda;
///
/// Creates a new instance of the class pointing directly to the target
///
/// The target at which this path starts
public DataModelPath(object target)
{
Target = target ?? throw new ArgumentNullException(nameof(target));
Path = "";
Entity = new DataModelPathEntity();
if (Target is DataModel dataModel)
DataModelGuid = dataModel.PluginInfo.Guid;
_segments = new LinkedList();
Save();
Initialize();
SubscribeToDataModelStore();
}
///
/// Creates a new instance of the class pointing to the provided path
///
/// The target at which this path starts
/// A point-separated path
public DataModelPath(object target, string path)
{
Target = target ?? throw new ArgumentNullException(nameof(target));
Path = path ?? throw new ArgumentNullException(nameof(path));
Entity = new DataModelPathEntity();
if (Target is DataModel dataModel)
DataModelGuid = dataModel.PluginInfo.Guid;
_segments = new LinkedList();
Save();
Initialize();
SubscribeToDataModelStore();
}
internal DataModelPath(object? target, DataModelPathEntity entity)
{
Target = target!;
Path = entity.Path;
Entity = entity;
_segments = new LinkedList();
Load();
Initialize();
SubscribeToDataModelStore();
}
///
/// Gets the data model at which this path starts
///
public object? Target { get; private set; }
internal DataModelPathEntity Entity { get; }
///
/// Gets the data model GUID of the if it is a
///
public Guid? DataModelGuid { get; private set; }
///
/// Gets the point-separated path associated with this
///
public string Path { get; private set; }
///
/// Gets a boolean indicating whether all are valid
///
public bool IsValid => Segments.Any() && Segments.All(p => p.Type != DataModelPathSegmentType.Invalid);
///
/// Gets a read-only list of all segments of this path
///
public IReadOnlyCollection Segments => _segments.ToList().AsReadOnly();
///
/// Gets a boolean indicating whether this data model path points to a list
///
public bool PointsToList => Segments.LastOrDefault()?.GetPropertyType() != null && typeof(IList).IsAssignableFrom(Segments.LastOrDefault()?.GetPropertyType());
internal Func