2013-05-01 08:43:14 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using NzbDrone.Common.Reflection;
|
2013-05-03 07:24:52 +02:00
|
|
|
|
using NzbDrone.Core.Annotations;
|
2013-05-01 08:43:14 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.ClientSchema
|
|
|
|
|
{
|
|
|
|
|
public static class SchemaBuilder
|
|
|
|
|
{
|
|
|
|
|
public static List<Field> GenerateSchema(object model)
|
|
|
|
|
{
|
|
|
|
|
var properties = model.GetType().GetSimpleProperties();
|
|
|
|
|
|
|
|
|
|
var result = new List<Field>(properties.Count);
|
|
|
|
|
|
|
|
|
|
foreach (var propertyInfo in properties)
|
|
|
|
|
{
|
2013-05-03 07:24:52 +02:00
|
|
|
|
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
|
2013-05-01 08:43:14 +02:00
|
|
|
|
|
2013-05-03 07:24:52 +02:00
|
|
|
|
if (fieldAttribute != null)
|
|
|
|
|
{
|
2013-05-01 08:43:14 +02:00
|
|
|
|
|
2013-05-03 07:24:52 +02:00
|
|
|
|
var field = new Field()
|
|
|
|
|
{
|
|
|
|
|
Name = propertyInfo.Name,
|
|
|
|
|
Label = fieldAttribute.Label,
|
|
|
|
|
HelpText = fieldAttribute.HelpText,
|
|
|
|
|
Order = fieldAttribute.Order,
|
2013-05-20 06:19:54 +02:00
|
|
|
|
Type = fieldAttribute.Type.ToString().ToLowerInvariant()
|
2013-05-03 07:24:52 +02:00
|
|
|
|
};
|
2013-05-01 08:43:14 +02:00
|
|
|
|
|
2013-05-03 07:24:52 +02:00
|
|
|
|
var value = propertyInfo.GetValue(model, null);
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
field.Value = value.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Add(field);
|
|
|
|
|
}
|
2013-05-01 08:43:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|