2019-06-24 11:58:36 +02:00
|
|
|
using System.Linq.Expressions;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
2021-08-16 10:31:11 +02:00
|
|
|
using Umbraco.Extensions;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Infrastructure.ModelsBuilder;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// This is called from within the generated model classes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// DO NOT REMOVE - although there are not code references this is used directly by the generated models.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static class PublishedModelUtility
|
|
|
|
|
{
|
|
|
|
|
// looks safer but probably useless... ppl should not call these methods directly
|
|
|
|
|
// and if they do... they have to take care about not doing stupid things
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// public static PublishedPropertyType GetModelPropertyType2<T>(Expression<Func<T, object>> selector)
|
|
|
|
|
// where T : PublishedContentModel
|
|
|
|
|
// {
|
|
|
|
|
// var type = typeof (T);
|
|
|
|
|
// var s1 = type.GetField("ModelTypeAlias", BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
// var alias = (s1.IsLiteral && s1.IsInitOnly && s1.FieldType == typeof(string)) ? (string)s1.GetValue(null) : null;
|
|
|
|
|
// var s2 = type.GetField("ModelItemType", BindingFlags.Public | BindingFlags.Static);
|
|
|
|
|
// var itemType = (s2.IsLiteral && s2.IsInitOnly && s2.FieldType == typeof(PublishedItemType)) ? (PublishedItemType)s2.GetValue(null) : 0;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// var contentType = PublishedContentType.Get(itemType, alias);
|
|
|
|
|
// // etc...
|
|
|
|
|
// }
|
|
|
|
|
public static IPublishedContentType? GetModelContentType(
|
2024-10-01 15:03:02 +02:00
|
|
|
IPublishedContentTypeCache contentTypeCache,
|
2022-06-02 08:18:31 +02:00
|
|
|
PublishedItemType itemType,
|
|
|
|
|
string alias)
|
|
|
|
|
{
|
|
|
|
|
switch (itemType)
|
2019-06-24 11:58:36 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
case PublishedItemType.Content:
|
2024-10-01 15:03:02 +02:00
|
|
|
return contentTypeCache.Get(PublishedItemType.Content, alias);
|
2023-09-07 17:18:57 +02:00
|
|
|
case PublishedItemType.Element:
|
2024-10-01 15:03:02 +02:00
|
|
|
return contentTypeCache.Get(PublishedItemType.Element, alias);
|
2022-06-02 08:18:31 +02:00
|
|
|
case PublishedItemType.Media:
|
2024-10-01 15:03:02 +02:00
|
|
|
return contentTypeCache.Get(PublishedItemType.Media, alias);
|
2022-06-02 08:18:31 +02:00
|
|
|
case PublishedItemType.Member:
|
2024-10-01 15:03:02 +02:00
|
|
|
return contentTypeCache.Get(PublishedItemType.Member, alias);
|
2022-06-02 08:18:31 +02:00
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(itemType));
|
2019-06-24 11:58:36 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public static IPublishedPropertyType? GetModelPropertyType<TModel, TValue>(
|
|
|
|
|
IPublishedContentType contentType,
|
|
|
|
|
Expression<Func<TModel, TValue>> selector)
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2023-01-18 12:28:54 +01:00
|
|
|
// where TModel : PublishedContentModel // TODO: PublishedContentModel _or_ PublishedElementModel
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
2023-01-18 12:28:54 +01:00
|
|
|
// TODO therefore, missing a check on TModel here
|
2022-06-02 08:18:31 +02:00
|
|
|
if (selector.Body is not MemberExpression expr)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Not a property expression.", nameof(selector));
|
|
|
|
|
}
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// there _is_ a risk that contentType and T do not match
|
|
|
|
|
// see note above : accepted risk...
|
|
|
|
|
ImplementPropertyTypeAttribute? attr = expr.Member
|
|
|
|
|
.GetCustomAttributes(typeof(ImplementPropertyTypeAttribute), false)
|
|
|
|
|
.OfType<ImplementPropertyTypeAttribute>()
|
|
|
|
|
.SingleOrDefault();
|
2019-06-24 11:58:36 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(attr?.Alias))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"Could not figure out property alias for property \"{expr.Member.Name}\".");
|
2019-06-24 11:58:36 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
|
|
|
|
|
return contentType.GetPropertyType(attr.Alias);
|
2019-06-24 11:58:36 +02:00
|
|
|
}
|
|
|
|
|
}
|