jellyfin/Emby.Server.Implementations/Services/StringMapTypeDeserializer.cs

117 lines
4.2 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2016-11-11 12:55:12 -07:00
using System;
using System.Collections.Generic;
using System.Reflection;
2020-04-19 06:18:28 -07:00
using MediaBrowser.Common.Extensions;
2016-11-11 12:55:12 -07:00
2017-02-13 13:54:28 -07:00
namespace Emby.Server.Implementations.Services
2016-11-11 12:55:12 -07:00
{
/// <summary>
/// Serializer cache of delegates required to create a type from a string map (e.g. for REST urls)
/// </summary>
public class StringMapTypeDeserializer
{
internal class PropertySerializerEntry
{
2019-02-20 08:49:03 -07:00
public PropertySerializerEntry(Action<object, object> propertySetFn, Func<string, object> propertyParseStringFn, Type propertyType)
2016-11-11 12:55:12 -07:00
{
PropertySetFn = propertySetFn;
PropertyParseStringFn = propertyParseStringFn;
2019-09-20 03:42:08 -07:00
PropertyType = propertyType;
2016-11-11 12:55:12 -07:00
}
2019-02-20 08:49:03 -07:00
public Action<object, object> PropertySetFn { get; private set; }
public Func<string, object> PropertyParseStringFn { get; private set; }
public Type PropertyType { get; private set; }
2016-11-11 12:55:12 -07:00
}
private readonly Type type;
private readonly Dictionary<string, PropertySerializerEntry> propertySetterMap
= new Dictionary<string, PropertySerializerEntry>(StringComparer.OrdinalIgnoreCase);
public Func<string, object> GetParseFn(Type propertyType)
{
if (propertyType == typeof(string))
2019-02-20 08:49:03 -07:00
{
2016-11-11 12:55:12 -07:00
return s => s;
2019-02-20 08:49:03 -07:00
}
2016-11-11 12:55:12 -07:00
2017-02-12 19:06:54 -07:00
return _GetParseFn(propertyType);
2016-11-11 12:55:12 -07:00
}
2017-02-12 19:06:54 -07:00
private readonly Func<Type, object> _CreateInstanceFn;
private readonly Func<Type, Func<string, object>> _GetParseFn;
public StringMapTypeDeserializer(Func<Type, object> createInstanceFn, Func<Type, Func<string, object>> getParseFn, Type type)
2016-11-11 12:55:12 -07:00
{
2017-02-12 19:06:54 -07:00
_CreateInstanceFn = createInstanceFn;
_GetParseFn = getParseFn;
2016-11-11 12:55:12 -07:00
this.type = type;
2017-02-12 19:06:54 -07:00
foreach (var propertyInfo in RestPath.GetSerializableProperties(type))
2016-11-11 12:55:12 -07:00
{
var propertySetFn = TypeAccessor.GetSetPropertyMethod(propertyInfo);
2016-11-11 12:55:12 -07:00
var propertyType = propertyInfo.PropertyType;
var propertyParseStringFn = GetParseFn(propertyType);
2019-02-20 08:49:03 -07:00
var propertySerializer = new PropertySerializerEntry(propertySetFn, propertyParseStringFn, propertyType);
2016-11-11 12:55:12 -07:00
propertySetterMap[propertyInfo.Name] = propertySerializer;
}
}
public object PopulateFromMap(object instance, IDictionary<string, string> keyValuePairs)
{
PropertySerializerEntry propertySerializerEntry = null;
if (instance == null)
2019-02-20 08:49:03 -07:00
{
2017-02-12 19:06:54 -07:00
instance = _CreateInstanceFn(type);
2019-02-20 08:49:03 -07:00
}
2016-11-11 12:55:12 -07:00
2017-08-30 11:52:29 -07:00
foreach (var pair in keyValuePairs)
2016-11-11 12:55:12 -07:00
{
2019-02-20 08:49:03 -07:00
string propertyName = pair.Key;
string propertyTextValue = pair.Value;
2017-08-30 11:52:29 -07:00
if (propertyTextValue == null
2019-02-20 08:49:03 -07:00
|| !propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry)
|| propertySerializerEntry.PropertySetFn == null)
2016-11-11 12:55:12 -07:00
{
continue;
}
if (propertySerializerEntry.PropertyType == typeof(bool))
{
2020-06-14 02:11:11 -07:00
// InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
2020-04-19 06:18:28 -07:00
propertyTextValue = StringExtensions.LeftPart(propertyTextValue, ',').ToString();
2016-11-11 12:55:12 -07:00
}
var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
if (value == null)
{
continue;
}
2019-02-20 08:49:03 -07:00
2016-11-11 12:55:12 -07:00
propertySerializerEntry.PropertySetFn(instance, value);
}
return instance;
}
}
internal static class TypeAccessor
2016-11-11 12:55:12 -07:00
{
public static Action<object, object> GetSetPropertyMethod(PropertyInfo propertyInfo)
2016-11-11 12:55:12 -07:00
{
2019-02-20 08:49:03 -07:00
if (!propertyInfo.CanWrite || propertyInfo.GetIndexParameters().Length > 0)
{
return null;
}
2016-11-11 12:55:12 -07:00
var setMethodInfo = propertyInfo.SetMethod;
return (instance, value) => setMethodInfo.Invoke(instance, new[] { value });
}
}
}