Обновления веб-отображения
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using Pf2e.Common;
|
||||
using Pf2e.Database.Model;
|
||||
using Pf2e.Database.Model.Base;
|
||||
using Pf2e.Database.Model.Common.I18n;
|
||||
using Pf2e.Database.Model.Enum;
|
||||
|
||||
namespace Pf2e.Web.Helpers;
|
||||
|
||||
@@ -42,6 +37,11 @@ public class EntitySerializer
|
||||
public bool IsImplemented { get; set; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Форматтер полей сущности.
|
||||
/// </summary>
|
||||
public EntityFieldFormatter FieldFormatter { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Сериализовать FullEntity.
|
||||
/// </summary>
|
||||
@@ -65,11 +65,6 @@ public class EntitySerializer
|
||||
result.Groups.Add(subEntityGroup);
|
||||
}
|
||||
|
||||
// Группа для ссылок
|
||||
FieldGroup referencesGroup = new() { Name = "Ссылки" };
|
||||
AddReferencesToGroup(referencesGroup, fullEntity);
|
||||
result.Groups.Add(referencesGroup);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -87,162 +82,29 @@ public class EntitySerializer
|
||||
if (property.Name == "Entity")
|
||||
continue;
|
||||
|
||||
FieldInfo fieldInfo = new()
|
||||
{
|
||||
Name = property.Name,
|
||||
Type = GetTypeDisplayName(property.PropertyType),
|
||||
Value = property.GetValue(obj),
|
||||
};
|
||||
|
||||
// Обработка значения для отображения
|
||||
var (displayValue, isImplemented) = FormatValue(fieldInfo.Value);
|
||||
fieldInfo.DisplayValue = displayValue;
|
||||
fieldInfo.IsImplemented = isImplemented;
|
||||
|
||||
object? value = property.GetValue(obj);
|
||||
FieldInfo fieldInfo = CreateFieldInfo(property, value);
|
||||
group.Fields.Add(fieldInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавить ссылки в группу.
|
||||
/// Создать FieldInfo для свойства.
|
||||
/// </summary>
|
||||
private void AddReferencesToGroup(FieldGroup group, FullEntity fullEntity)
|
||||
/// <param name="property">Поле класса.</param>
|
||||
/// <param name="value">Значение поля.</param>
|
||||
/// <returns>Информация о поле.</returns>
|
||||
private FieldInfo CreateFieldInfo(PropertyInfo property, object? value)
|
||||
{
|
||||
// Добавляем информацию о связанных сущностях
|
||||
Entity entity = fullEntity.Entity;
|
||||
string? displayValue = FieldFormatter.FormatValue(value);
|
||||
|
||||
// Traits
|
||||
if (entity.Traits?.Any() == true)
|
||||
return new()
|
||||
{
|
||||
var (displayValue, isImplemented) = FormatValue(entity.Traits);
|
||||
group.Fields.Add(
|
||||
new FieldInfo
|
||||
{
|
||||
Name = "Traits",
|
||||
Type = "ICollection<Trait>",
|
||||
Value = entity.Traits,
|
||||
DisplayValue = displayValue,
|
||||
IsImplemented = isImplemented,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Keywords
|
||||
if (entity.Keywords?.Any() == true)
|
||||
{
|
||||
var (displayValue, isImplemented) = FormatValue(entity.Keywords);
|
||||
group.Fields.Add(
|
||||
new FieldInfo
|
||||
{
|
||||
Name = "Keywords",
|
||||
Type = "ICollection<Keyword>",
|
||||
Value = entity.Keywords,
|
||||
DisplayValue = displayValue,
|
||||
IsImplemented = isImplemented,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Sources
|
||||
if (entity.Sources?.Any() == true)
|
||||
{
|
||||
var (displayValue, isImplemented) = FormatValue(entity.Sources);
|
||||
group.Fields.Add(
|
||||
new FieldInfo
|
||||
{
|
||||
Name = "Sources",
|
||||
Type = "ICollection<EntitySource>",
|
||||
Value = entity.Sources,
|
||||
DisplayValue = displayValue,
|
||||
IsImplemented = isImplemented,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Externals
|
||||
if (entity.Externals?.Any() == true)
|
||||
{
|
||||
var (displayValue, isImplemented) = FormatValue(entity.Externals);
|
||||
group.Fields.Add(
|
||||
new()
|
||||
{
|
||||
Name = "Externals",
|
||||
Type = "ICollection<EntityExternal>",
|
||||
Value = entity.Externals,
|
||||
DisplayValue = displayValue,
|
||||
IsImplemented = isImplemented,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить отображаемое имя типа.
|
||||
/// </summary>
|
||||
private string GetTypeDisplayName(Type type)
|
||||
{
|
||||
if (type.IsEnum)
|
||||
return "enum";
|
||||
|
||||
return type.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Форматировать значение для отображения.
|
||||
/// </summary>
|
||||
private (string displayValue, bool isImplemented) FormatValue(object? value)
|
||||
{
|
||||
if (value == null)
|
||||
return ("null", true);
|
||||
|
||||
Type type = value.GetType();
|
||||
|
||||
// Обработка перечислений
|
||||
if (type.IsEnum)
|
||||
{
|
||||
Enum enumValue = (Enum)value;
|
||||
return ($"{enumValue} ({(int)(object)enumValue})", true);
|
||||
}
|
||||
|
||||
// Обработка примитивных типов
|
||||
if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
|
||||
{
|
||||
return (value.ToString() ?? "null", true);
|
||||
}
|
||||
|
||||
// Обработка IEntity
|
||||
if (typeof(IEntity).IsAssignableFrom(type))
|
||||
{
|
||||
var entity = (IEntity)value;
|
||||
var entityType = entity.Entity.Type;
|
||||
var typeName = PgNameHelper.GetPgName(entityType);
|
||||
var slug = entity.Entity.Slug;
|
||||
var name = entity.Entity.Name[LocaleType.English];
|
||||
return ($"<a href=\"/{typeName}/{slug}\">{name}</a>", true);
|
||||
}
|
||||
|
||||
// Обработка коллекций
|
||||
if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
|
||||
{
|
||||
var collection = (IEnumerable)value;
|
||||
var items = new List<string>();
|
||||
bool allImplemented = true;
|
||||
|
||||
foreach (var item in collection)
|
||||
{
|
||||
var (itemDisplay, itemImplemented) = FormatValue(item);
|
||||
items.Add(itemDisplay);
|
||||
if (!itemImplemented)
|
||||
allImplemented = false;
|
||||
}
|
||||
|
||||
if (items.Count == 0)
|
||||
return ("(empty)", true);
|
||||
|
||||
return (string.Join(", ", items), allImplemented);
|
||||
}
|
||||
|
||||
// Для остальных сложных объектов
|
||||
return ($"[{type.Name}] - НЕ РЕАЛИЗОВАНО", false);
|
||||
Name = property.Name,
|
||||
Type = property.PropertyType.Name,
|
||||
Value = value,
|
||||
DisplayValue = displayValue ?? "null",
|
||||
IsImplemented = displayValue is not null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user