added schema generation

pull/2/head
kay.one 12 years ago committed by Keivan Beigi
parent e8c832ac18
commit a66d43b806

@ -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; }
}
}

@ -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" />

@ -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; }
}
}

@ -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; }
}
}

@ -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;
}
}
}

@ -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 @@ namespace NzbDrone.Api.Indexers
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;
}
}
}

@ -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 @@ namespace NzbDrone.Api.Indexers
{
public Boolean Enable { get; set; }
public String Name { get; set; }
public String Settings { get; set; }
public List<Field> Fields { get; set; }
}
}

@ -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" />

@ -45,5 +45,16 @@ namespace NzbDrone.Common.Reflection
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;
}
}
}

@ -1,10 +1,8 @@
'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',
template : 'Settings/Indexers/CollectionTemplate'
});
});
});

@ -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 }
];
}
}
});
});

Loading…
Cancel
Save