mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Dynamic data models - Rewrote the visualization to use leverage new core APIs
This commit is contained in:
1 parent
26db794467
commit
8f552d0f71
22 files changed
+463
-374
No files matched your search
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
|
||||
namespace Artemis.Core
|
||||
@@ -11,22 +13,29 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public class DataModelPath
|
||||
{
|
||||
private readonly LinkedList<DataModelPathPart> _parts;
|
||||
private readonly LinkedList<DataModelPathSegment> _segments;
|
||||
|
||||
internal DataModelPath(DataModel dataModel, string path)
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="DataModelPath" /> class
|
||||
/// </summary>
|
||||
/// <param name="dataModel">The data model at which this path starts</param>
|
||||
/// <param name="path">A string representation of the <see cref="DataModelPath" /></param>
|
||||
public DataModelPath(object dataModel, string path)
|
||||
{
|
||||
DataModel = dataModel ?? throw new ArgumentNullException(nameof(dataModel));
|
||||
Target = dataModel ?? throw new ArgumentNullException(nameof(dataModel));
|
||||
Path = path ?? throw new ArgumentNullException(nameof(path));
|
||||
DataModelGuid = dataModel.PluginInfo.Guid;
|
||||
|
||||
_parts = new LinkedList<DataModelPathPart>();
|
||||
if (string.IsNullOrWhiteSpace(Path))
|
||||
throw new ArgumentException("Path cannot be empty");
|
||||
|
||||
_segments = new LinkedList<DataModelPathSegment>();
|
||||
Initialize(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data model at which this path starts
|
||||
/// </summary>
|
||||
public DataModel DataModel { get; }
|
||||
public object Target { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a string representation of the <see cref="DataModelPath" />
|
||||
@@ -34,49 +43,111 @@ namespace Artemis.Core
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether all <see cref="Parts" /> are valid
|
||||
/// Gets a boolean indicating whether all <see cref="Segments" /> are valid
|
||||
/// </summary>
|
||||
public bool IsValid => Parts.All(p => p.Type != DataModelPathPartType.Invalid);
|
||||
public bool IsValid => Segments.All(p => p.Type != DataModelPathSegmentType.Invalid);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only list of all parts of this path
|
||||
/// Gets a read-only list of all segments of this path
|
||||
/// </summary>
|
||||
public IReadOnlyCollection<DataModelPathPart> Parts => _parts.ToList().AsReadOnly();
|
||||
public IReadOnlyCollection<DataModelPathSegment> Segments => _segments.ToList().AsReadOnly();
|
||||
|
||||
internal Func<DataModel, object> Accessor { get; private set; }
|
||||
/// <summary>
|
||||
/// Gets a boolean indicating whether this data model path can have an inner path because it points to a list
|
||||
/// </summary>
|
||||
public bool CanHaveInnerPath => Segments.LastOrDefault()?.GetPropertyType()?.IsAssignableFrom(typeof(IList)) ?? false;
|
||||
|
||||
internal Guid DataModelGuid { get; set; }
|
||||
/// <summary>
|
||||
/// Gets the inner path of this path, only available if this path points to a list
|
||||
/// </summary>
|
||||
public DataModelPath InnerPath { get; internal set; }
|
||||
|
||||
internal Func<object, object> Accessor { get; private set; }
|
||||
|
||||
public void SetInnerPath(string path)
|
||||
{
|
||||
if (!CanHaveInnerPath)
|
||||
{
|
||||
var type = Segments.LastOrDefault()?.GetPropertyType();
|
||||
throw new ArtemisCoreException($"Cannot set an inner path on a data model path if it does not point to a list (value is of type: {type?.Name})");
|
||||
}
|
||||
|
||||
InnerPath = new DataModelPath(GetValue(), path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current value of the path
|
||||
/// </summary>
|
||||
public object GetValue()
|
||||
{
|
||||
return Accessor?.Invoke(Target);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property info of the property this path points to
|
||||
/// </summary>
|
||||
/// <returns>If static, the property info. If dynamic, <c>null</c></returns>
|
||||
public PropertyInfo GetPropertyInfo()
|
||||
{
|
||||
if (InnerPath != null)
|
||||
return InnerPath.GetPropertyInfo();
|
||||
return Segments.LastOrDefault()?.GetPropertyInfo();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the property this path points to
|
||||
/// </summary>
|
||||
/// <returns>If possible, the property type</returns>
|
||||
public Type GetPropertyType()
|
||||
{
|
||||
if (InnerPath != null)
|
||||
return InnerPath.GetPropertyType();
|
||||
return Segments.LastOrDefault()?.GetPropertyType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property description of the property this path points to
|
||||
/// </summary>
|
||||
/// <returns>If found, the data model property description</returns>
|
||||
public DataModelPropertyAttribute GetPropertyDescription()
|
||||
{
|
||||
if (InnerPath != null)
|
||||
return InnerPath.GetPropertyDescription();
|
||||
return Segments.LastOrDefault()?.GetPropertyDescription();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
if (InnerPath != null)
|
||||
return $"{Path} > {InnerPath}";
|
||||
return Path;
|
||||
}
|
||||
|
||||
private void Initialize(string path)
|
||||
{
|
||||
var parts = path.Split(".");
|
||||
for (var index = 0; index < parts.Length; index++)
|
||||
var segments = path.Split(".");
|
||||
for (var index = 0; index < segments.Length; index++)
|
||||
{
|
||||
var identifier = parts[index];
|
||||
var node = _parts.AddLast(new DataModelPathPart(this, identifier, string.Join('.', parts.Take(index + 1))));
|
||||
var identifier = segments[index];
|
||||
var node = _segments.AddLast(new DataModelPathSegment(this, identifier, string.Join('.', segments.Take(index + 1))));
|
||||
node.Value.Node = node;
|
||||
}
|
||||
|
||||
var parameter = Expression.Parameter(typeof(DataModel), "dm");
|
||||
Expression expression = Expression.Convert(parameter, DataModel.GetType());
|
||||
var parameter = Expression.Parameter(typeof(object), "t");
|
||||
Expression expression = Expression.Convert(parameter, Target.GetType());
|
||||
Expression nullCondition = null;
|
||||
|
||||
foreach (var part in _parts)
|
||||
foreach (var segment in _segments)
|
||||
{
|
||||
var notNull = Expression.NotEqual(expression, Expression.Constant(null));
|
||||
var notNull = Expression.NotEqual(expression, Expression.Default(expression.Type));
|
||||
nullCondition = nullCondition != null ? Expression.AndAlso(nullCondition, notNull) : notNull;
|
||||
expression = part.Initialize(parameter, expression, nullCondition);
|
||||
expression = segment.Initialize(parameter, expression, nullCondition);
|
||||
if (expression == null)
|
||||
return;
|
||||
}
|
||||
|
||||
Accessor = Expression.Lambda<Func<DataModel, object>>(
|
||||
Accessor = Expression.Lambda<Func<object, object>>(
|
||||
// Wrap with a null check
|
||||
Expression.Condition(
|
||||
nullCondition,
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a part of a data model path
|
||||
/// </summary>
|
||||
public class DataModelPathPart
|
||||
{
|
||||
internal DataModelPathPart(DataModelPath dataModelPath, string identifier, string path)
|
||||
{
|
||||
DataModelPath = dataModelPath;
|
||||
Identifier = identifier;
|
||||
Path = path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data model path this is a part of
|
||||
/// </summary>
|
||||
public DataModelPath DataModelPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the identifier that is associated with this part
|
||||
/// </summary>
|
||||
public string Identifier { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path that leads to this part
|
||||
/// </summary>
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of data model this part of the path points to
|
||||
/// </summary>
|
||||
public DataModelPathPartType Type { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of dynamic data model this path points to
|
||||
/// <para>Not used if the <see cref="Type" /> is <see cref="DataModelPathPartType.Static" /></para>
|
||||
/// </summary>
|
||||
public Type DynamicDataModelType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the previous part in the path
|
||||
/// </summary>
|
||||
public DataModelPathPart Previous => Node.Previous?.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the next part in the path
|
||||
/// </summary>
|
||||
public DataModelPathPart Next => Node.Next?.Value;
|
||||
|
||||
internal Func<DataModel, object> Accessor { get; set; }
|
||||
internal LinkedListNode<DataModelPathPart> Node { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current value of the path up to this part
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object GetValue()
|
||||
{
|
||||
return Type == DataModelPathPartType.Invalid ? null : Accessor(DataModelPath.DataModel);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Type}] {Path}";
|
||||
}
|
||||
|
||||
internal Expression Initialize(ParameterExpression parameter, Expression expression, Expression nullCondition)
|
||||
{
|
||||
var previousValue = Previous != null ? Previous.GetValue() : DataModelPath.DataModel;
|
||||
if (previousValue == null)
|
||||
{
|
||||
Type = DataModelPathPartType.Invalid;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Determine this part's type by looking for a dynamic data model with the identifier
|
||||
if (previousValue is DataModel dataModel)
|
||||
{
|
||||
var hasDynamicDataModel = dataModel.DynamicDataModels.TryGetValue(Identifier, out var dynamicDataModel);
|
||||
// If a dynamic data model is found the use that
|
||||
if (hasDynamicDataModel)
|
||||
DetermineDynamicType(dynamicDataModel);
|
||||
// Otherwise look for a static type
|
||||
else
|
||||
DetermineStaticType(previousValue);
|
||||
}
|
||||
// Only data models can have dynamic types so if it is something else, its always going to be static
|
||||
else
|
||||
DetermineStaticType(previousValue);
|
||||
|
||||
return CreateExpression(parameter, expression, nullCondition);
|
||||
}
|
||||
|
||||
private Expression CreateExpression(ParameterExpression parameter, Expression expression, Expression nullCondition)
|
||||
{
|
||||
if (Type == DataModelPathPartType.Invalid)
|
||||
{
|
||||
Accessor = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
Expression accessorExpression;
|
||||
// A static part just needs to access the property or filed
|
||||
if (Type == DataModelPathPartType.Static)
|
||||
accessorExpression = Expression.PropertyOrField(expression, Identifier);
|
||||
// A dynamic part calls the generic method DataModel.DynamicChild<T> and provides the identifier as an argument
|
||||
else
|
||||
{
|
||||
accessorExpression = Expression.Call(
|
||||
expression,
|
||||
nameof(DataModel.DynamicChild),
|
||||
new[] {DynamicDataModelType},
|
||||
Expression.Constant(Identifier)
|
||||
);
|
||||
}
|
||||
|
||||
Accessor = Expression.Lambda<Func<DataModel, object>>(
|
||||
// Wrap with a null check
|
||||
Expression.Condition(
|
||||
nullCondition,
|
||||
Expression.Convert(accessorExpression, typeof(object)),
|
||||
Expression.Convert(Expression.Default(accessorExpression.Type), typeof(object))
|
||||
),
|
||||
parameter
|
||||
).Compile();
|
||||
|
||||
return accessorExpression;
|
||||
}
|
||||
|
||||
private void DetermineDynamicType(DataModel dynamicDataModel)
|
||||
{
|
||||
Type = DataModelPathPartType.Dynamic;
|
||||
DynamicDataModelType = dynamicDataModel.GetType();
|
||||
}
|
||||
|
||||
private void DetermineStaticType(object previous)
|
||||
{
|
||||
var previousType = previous.GetType();
|
||||
var property = previousType.GetProperty(Identifier, BindingFlags.Public | BindingFlags.Instance);
|
||||
Type = property == null ? DataModelPathPartType.Invalid : DataModelPathPartType.Static;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.DataModelExpansions;
|
||||
using Humanizer;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a segment of a data model path
|
||||
/// </summary>
|
||||
public class DataModelPathSegment
|
||||
{
|
||||
internal DataModelPathSegment(DataModelPath dataModelPath, string identifier, string path)
|
||||
{
|
||||
DataModelPath = dataModelPath;
|
||||
Identifier = identifier;
|
||||
Path = path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data model path this is a segment of
|
||||
/// </summary>
|
||||
public DataModelPath DataModelPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the identifier that is associated with this segment
|
||||
/// </summary>
|
||||
public string Identifier { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path that leads to this segment
|
||||
/// </summary>
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of data model this segment of the path points to
|
||||
/// </summary>
|
||||
public DataModelPathSegmentType Type { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of dynamic data model this path points to
|
||||
/// <para>Not used if the <see cref="Type" /> is <see cref="DataModelPathSegmentType.Static" /></para>
|
||||
/// </summary>
|
||||
public Type DynamicDataModelType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the previous segment in the path
|
||||
/// </summary>
|
||||
public DataModelPathSegment Previous => Node.Previous?.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the next segment in the path
|
||||
/// </summary>
|
||||
public DataModelPathSegment Next => Node.Next?.Value;
|
||||
|
||||
internal Func<object, object> Accessor { get; set; }
|
||||
internal LinkedListNode<DataModelPathSegment> Node { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current value of the path up to this segment
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object GetValue()
|
||||
{
|
||||
return Type == DataModelPathSegmentType.Invalid ? null : Accessor(DataModelPath.Target);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Type}] {Path}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property info of the property this segment points to
|
||||
/// </summary>
|
||||
/// <returns>If static, the property info. If dynamic, <c>null</c></returns>
|
||||
public PropertyInfo GetPropertyInfo()
|
||||
{
|
||||
// Dynamic types have no property and therefore no property info
|
||||
if (Type == DataModelPathSegmentType.Dynamic)
|
||||
return null;
|
||||
|
||||
// If this is the first segment in a path, the property is located on the data model
|
||||
if (Previous == null)
|
||||
return DataModelPath.Target.GetType().GetProperty(Identifier);
|
||||
// If this is not the first segment in a path, the property is located on the previous segment
|
||||
return Previous.GetValue()?.GetType().GetProperty(Identifier);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property description of the property this segment points to
|
||||
/// </summary>
|
||||
/// <returns>If found, the data model property description</returns>
|
||||
public DataModelPropertyAttribute GetPropertyDescription()
|
||||
{
|
||||
// Dynamic types have a data model description
|
||||
if (Type == DataModelPathSegmentType.Dynamic)
|
||||
return ((DataModel) GetValue())?.DataModelDescription;
|
||||
|
||||
var propertyInfo = GetPropertyInfo();
|
||||
if (propertyInfo == null)
|
||||
return null;
|
||||
|
||||
// Static types may have one as an attribute
|
||||
return (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute)) ??
|
||||
new DataModelPropertyAttribute {Name = propertyInfo.Name.Humanize(), ResetsDepth = false};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the property this path points to
|
||||
/// </summary>
|
||||
/// <returns>If possible, the property type</returns>
|
||||
public Type GetPropertyType()
|
||||
{
|
||||
// Prefer basing the type on the property info
|
||||
var propertyInfo = GetPropertyInfo();
|
||||
var type = propertyInfo?.PropertyType;
|
||||
// Property info is not available on dynamic paths though, so fall back on the current value
|
||||
if (propertyInfo == null)
|
||||
{
|
||||
var currentValue = GetValue();
|
||||
if (currentValue != null)
|
||||
type = currentValue.GetType();
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
internal Expression Initialize(ParameterExpression parameter, Expression expression, Expression nullCondition)
|
||||
{
|
||||
var previousValue = Previous != null ? Previous.GetValue() : DataModelPath.Target;
|
||||
if (previousValue == null)
|
||||
{
|
||||
Type = DataModelPathSegmentType.Invalid;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Determine this segment's type by looking for a dynamic data model with the identifier
|
||||
if (previousValue is DataModel dataModel)
|
||||
{
|
||||
var hasDynamicDataModel = dataModel.DynamicDataModels.TryGetValue(Identifier, out var dynamicDataModel);
|
||||
// If a dynamic data model is found the use that
|
||||
if (hasDynamicDataModel)
|
||||
DetermineDynamicType(dynamicDataModel);
|
||||
// Otherwise look for a static type
|
||||
else
|
||||
DetermineStaticType(previousValue);
|
||||
}
|
||||
// Only data models can have dynamic types so if it is something else, its always going to be static
|
||||
else
|
||||
DetermineStaticType(previousValue);
|
||||
|
||||
return CreateExpression(parameter, expression, nullCondition);
|
||||
}
|
||||
|
||||
private Expression CreateExpression(ParameterExpression parameter, Expression expression, Expression nullCondition)
|
||||
{
|
||||
if (Type == DataModelPathSegmentType.Invalid)
|
||||
{
|
||||
Accessor = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
Expression accessorExpression;
|
||||
// A static segment just needs to access the property or filed
|
||||
if (Type == DataModelPathSegmentType.Static)
|
||||
accessorExpression = Expression.PropertyOrField(expression, Identifier);
|
||||
// A dynamic segment calls the generic method DataModel.DynamicChild<T> and provides the identifier as an argument
|
||||
else
|
||||
{
|
||||
accessorExpression = Expression.Call(
|
||||
expression,
|
||||
nameof(DataModel.DynamicChild),
|
||||
new[] {DynamicDataModelType},
|
||||
Expression.Constant(Identifier)
|
||||
);
|
||||
}
|
||||
|
||||
Accessor = Expression.Lambda<Func<object, object>>(
|
||||
// Wrap with a null check
|
||||
Expression.Condition(
|
||||
nullCondition,
|
||||
Expression.Convert(accessorExpression, typeof(object)),
|
||||
Expression.Convert(Expression.Default(accessorExpression.Type), typeof(object))
|
||||
),
|
||||
parameter
|
||||
).Compile();
|
||||
|
||||
return accessorExpression;
|
||||
}
|
||||
|
||||
private void DetermineDynamicType(DataModel dynamicDataModel)
|
||||
{
|
||||
Type = DataModelPathSegmentType.Dynamic;
|
||||
DynamicDataModelType = dynamicDataModel.GetType();
|
||||
}
|
||||
|
||||
private void DetermineStaticType(object previous)
|
||||
{
|
||||
var previousType = previous.GetType();
|
||||
var property = previousType.GetProperty(Identifier, BindingFlags.Public | BindingFlags.Instance);
|
||||
Type = property == null ? DataModelPathSegmentType.Invalid : DataModelPathSegmentType.Static;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Represents a type of data model path
|
||||
/// </summary>
|
||||
public enum DataModelPathPartType
|
||||
public enum DataModelPathSegmentType
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an invalid data model type that points to a missing data model
|
||||
@@ -68,7 +68,7 @@ namespace Artemis.Core.DataModelExpansions
|
||||
if (_dynamicDataModels.ContainsKey(key))
|
||||
{
|
||||
throw new ArtemisCoreException($"Cannot add a dynamic data model with key '{key}' " +
|
||||
"because the key is already in use on this data model.");
|
||||
"because the key is already in use on by another dynamic property this data model.");
|
||||
}
|
||||
|
||||
if (_dynamicDataModels.ContainsValue(dynamicDataModel))
|
||||
@@ -78,6 +78,12 @@ namespace Artemis.Core.DataModelExpansions
|
||||
$"because the dynamic data model is already added with key '{existingKey}.");
|
||||
}
|
||||
|
||||
if (GetType().GetProperty(key) != null)
|
||||
{
|
||||
throw new ArtemisCoreException($"Cannot add a dynamic data model with key '{key}' " +
|
||||
"because the key is already in use by a static property on this data model.");
|
||||
}
|
||||
|
||||
dynamicDataModel.PluginInfo = PluginInfo;
|
||||
dynamicDataModel.DataModelDescription = new DataModelPropertyAttribute
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user