Lidarr/src/NzbDrone.Api/ClientSchema/SchemaBuilder.cs

147 lines
4.9 KiB
C#
Raw Normal View History

2013-06-13 07:20:33 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
2013-09-22 23:40:36 +00:00
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Extensions;
2013-05-01 06:43:14 +00:00
using NzbDrone.Common.Reflection;
2013-05-03 05:24:52 +00:00
using NzbDrone.Core.Annotations;
using Omu.ValueInjecter;
2013-05-01 06:43:14 +00:00
namespace NzbDrone.Api.ClientSchema
{
public static class SchemaBuilder
{
2013-09-22 23:40:36 +00:00
public static List<Field> ToSchema(object model)
2013-05-01 06:43:14 +00:00
{
2013-11-30 23:53:07 +00:00
Ensure.That(model, () => model).IsNotNull();
2013-09-22 23:40:36 +00:00
2013-05-01 06:43:14 +00:00
var properties = model.GetType().GetSimpleProperties();
var result = new List<Field>(properties.Count);
foreach (var propertyInfo in properties)
{
2013-05-03 05:24:52 +00:00
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
2013-05-01 06:43:14 +00:00
2013-05-03 05:24:52 +00:00
if (fieldAttribute != null)
{
2013-05-01 06:43:14 +00:00
var field = new Field
2013-05-03 05:24:52 +00:00
{
Name = propertyInfo.Name,
Label = fieldAttribute.Label,
HelpText = fieldAttribute.HelpText,
HelpLink = fieldAttribute.HelpLink,
2013-05-03 05:24:52 +00:00
Order = fieldAttribute.Order,
Advanced = fieldAttribute.Advanced,
2013-05-20 04:19:54 +00:00
Type = fieldAttribute.Type.ToString().ToLowerInvariant()
2013-05-03 05:24:52 +00:00
};
2013-05-01 06:43:14 +00:00
2013-05-03 05:24:52 +00:00
var value = propertyInfo.GetValue(model, null);
if (value != null)
{
field.Value = value;
2013-05-03 05:24:52 +00:00
}
2013-06-13 07:20:33 +00:00
if (fieldAttribute.Type == FieldType.Select)
{
field.SelectOptions = GetSelectOptions(fieldAttribute.SelectOptions);
}
2013-05-03 05:24:52 +00:00
result.Add(field);
}
2013-05-01 06:43:14 +00:00
}
return result;
}
2013-06-13 07:20:33 +00:00
2013-09-22 23:40:36 +00:00
public static object ReadFormSchema(List<Field> fields, Type targetType, object defaults = null)
2013-09-22 23:40:36 +00:00
{
2013-11-30 23:53:07 +00:00
Ensure.That(targetType, () => targetType).IsNotNull();
2013-09-22 23:40:36 +00:00
var properties = targetType.GetSimpleProperties();
var target = Activator.CreateInstance(targetType);
if (defaults != null)
{
target.InjectFrom(defaults);
}
2013-09-22 23:40:36 +00:00
foreach (var propertyInfo in properties)
{
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
if (fieldAttribute != null)
{
var field = fields.Find(f => f.Name == propertyInfo.Name);
if (propertyInfo.PropertyType == typeof(Int32))
{
2013-12-12 00:41:58 +00:00
var value = Convert.ToInt32(field.Value);
propertyInfo.SetValue(target, value, null);
}
2013-12-19 08:38:28 +00:00
else if (propertyInfo.PropertyType == typeof(Int64))
2013-12-12 00:41:58 +00:00
{
var value = Convert.ToInt64(field.Value);
propertyInfo.SetValue(target, value, null);
2013-09-22 23:40:36 +00:00
}
else if (propertyInfo.PropertyType == typeof(Nullable<Int32>))
{
2013-12-12 00:41:58 +00:00
var value = field.Value.ToString().ParseInt32();
propertyInfo.SetValue(target, value, null);
}
else if (propertyInfo.PropertyType == typeof(Nullable<Int64>))
{
var value = field.Value.ToString().ParseInt64();
propertyInfo.SetValue(target, value, null);
2013-09-22 23:40:36 +00:00
}
else if (propertyInfo.PropertyType == typeof (IEnumerable<Int32>))
{
IEnumerable<Int32> value;
if (field.Value.GetType() == typeof (JArray))
{
value = ((JArray) field.Value).Select(s => s.Value<Int32>());
}
else
{
value = field.Value.ToString().Split(new []{','}, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s));
}
propertyInfo.SetValue(target, value, null);
}
2013-09-22 23:40:36 +00:00
else
{
propertyInfo.SetValue(target, field.Value, null);
}
}
}
return target;
}
public static T ReadFormSchema<T>(List<Field> fields)
{
2013-11-30 23:53:07 +00:00
return (T)ReadFormSchema(fields, typeof(T));
2013-09-22 23:40:36 +00:00
}
2013-06-13 07:20:33 +00:00
private static List<SelectOption> GetSelectOptions(Type selectOptions)
{
var options = from Enum e in Enum.GetValues(selectOptions)
2013-09-22 23:40:36 +00:00
select new SelectOption { Value = Convert.ToInt32(e), Name = e.ToString() };
2013-06-13 07:20:33 +00:00
return options.OrderBy(o => o.Value).ToList();
}
2013-05-01 06:43:14 +00:00
}
}