1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

added schema generation

This commit is contained in:
kay.one 2013-04-30 23:43:14 -07:00 committed by Keivan Beigi
parent e8c832ac18
commit a66d43b806
11 changed files with 150 additions and 15 deletions

View File

@ -0,0 +1,45 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Test.Common;
namespace NzbDrone.Api.Test.ClientSchemaTests
{
[TestFixture]
public class SchemaBuilderFixture : TestBase
{
[Test]
public void should_return_field_for_every_property()
{
var schema = SchemaBuilder.GenerateSchema(new TestModel());
schema.Should().HaveCount(2);
}
[Test]
public void schema_should_have_proper_fields()
{
var model = new TestModel
{
FirstName = "Bob",
LastName = "Poop"
};
var schema = SchemaBuilder.GenerateSchema(model);
schema.Should().Contain(c => c.Order == 1 && c.Name == "LastName" && c.Label == "Last Name" && c.HelpText == "Your Last Name" && c.Value == "Poop");
schema.Should().Contain(c => c.Order == 0 && c.Name == "FirstName" && c.Label == "First Name" && c.HelpText == "Your First Name" && c.Value == "Bob");
}
}
public class TestModel
{
[FieldDefinition(0, Label = "First Name", HelpText = "Your First Name")]
public string FirstName { get; set; }
[FieldDefinition(1, Label = "Last Name", HelpText = "Your Last Name")]
public string LastName { get; set; }
}
}

View File

@ -72,6 +72,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ClientSchemaTests\SchemaBuilderFixture.cs" />
<Compile Include="MappingTests\ReflectionExtensionFixture.cs" />
<Compile Include="MappingTests\ResourceMappingFixture.cs" />
<Compile Include="StaticResourceMapperFixture.cs" />

View File

@ -0,0 +1,11 @@
namespace NzbDrone.Api.ClientSchema
{
public class Field
{
public int Order { get; set; }
public string Name { get; set; }
public string Label { get; set; }
public string HelpText { get; set; }
public string Value { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
namespace NzbDrone.Api.ClientSchema
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class FieldDefinitionAttribute : Attribute
{
public FieldDefinitionAttribute(int order)
{
Order = order;
}
public int Order { get; private set; }
public string Label { get; set; }
public string HelpText { get; set; }
}
}

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using NzbDrone.Common.Reflection;
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)
{
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>();
var field = new Field()
{
Name = propertyInfo.Name,
Label = fieldAttribute.Label,
HelpText = fieldAttribute.HelpText,
Order = fieldAttribute.Order,
};
var value = propertyInfo.GetValue(model, null);
if (value != null)
{
field.Value = value.ToString();
}
result.Add(field);
}
return result;
}
}
}

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Core.Indexers;
using Omu.ValueInjecter;
namespace NzbDrone.Api.Indexers
{
@ -15,7 +17,18 @@ public IndexerModule(IIndexerService indexerService)
private List<IndexerResource> GetAll()
{
return ApplyToList(_indexerService.All);
var indexers = _indexerService.All();
var result = new List<IndexerResource>(indexers.Count);
foreach (var indexerDefinition in indexers)
{
var resource = new IndexerResource();
resource.InjectFrom(indexerDefinition);
resource.Fields = SchemaBuilder.GenerateSchema(indexerDefinition.Settings);
}
return result;
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.REST;
namespace NzbDrone.Api.Indexers
@ -7,6 +9,7 @@ public class IndexerResource : RestResource
{
public Boolean Enable { get; set; }
public String Name { get; set; }
public String Settings { get; set; }
public List<Field> Fields { get; set; }
}
}

View File

@ -84,6 +84,9 @@
<ItemGroup>
<Compile Include="AutomapperBootstraper.cs" />
<Compile Include="Calendar\CalendarModule.cs" />
<Compile Include="ClientSchema\FieldDefinitionAttribute.cs" />
<Compile Include="ClientSchema\Field.cs" />
<Compile Include="ClientSchema\SchemaBuilder.cs" />
<Compile Include="Commands\CommandModule.cs" />
<Compile Include="Commands\CommandResource.cs" />
<Compile Include="Directories\DirectoryModule.cs" />

View File

@ -45,5 +45,16 @@ public static bool IsWritable(this PropertyInfo propertyInfo)
return propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null;
}
public static T GetAttribute<T>(this MemberInfo member, bool isRequired = true) where T : Attribute
{
var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();
if (attribute == null && isRequired)
{
throw new ArgumentException(String.Format("The {0} attribute must be defined on member {1}", typeof(T).Name, member.Name));
}
return (T)attribute;
}
}
}

View File

@ -1,7 +1,5 @@
'use strict';
define(['app', 'Settings/Indexers/ItemView'], function (app) {
define(['app', 'Settings/Indexers/ItemView'], function () {
NzbDrone.Settings.Indexers.CollectionView = Backbone.Marionette.CompositeView.extend({
itemView : NzbDrone.Settings.Indexers.ItemView,
itemViewContainer : '#x-indexers',

View File

@ -1,13 +1,5 @@
"use strict";
define(['app'], function (app) {
define(['app'], function () {
NzbDrone.Settings.Indexers.Model = Backbone.DeepModel.extend({
mutators: {
fields: function () {
return [
{ key: 'username', title: 'Username', helpText: 'HALP!', value: 'mark', index: 0 },
{ key: 'apiKey', title: 'API Key', helpText: 'HALP!', value: '', index: 1 }
];
}
}
});
});