Skip to content

Commit

Permalink
Implemented MultivaluedMappedToCollectionRule.
Browse files Browse the repository at this point in the history
  • Loading branch information
endforward committed Mar 12, 2010
1 parent cc64e27 commit 1267222
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions SolrNet.Tests/Resources/solrSchemaBasic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="name" type="string" indexed="true" stored="false" required="true" />
<field name="nameSort" type="string" indexed="true" stored="false"/>
<field name="features" type="string" indexed="true" stored="false" multiValued="true"/>
<dynamicField name="*_s" type="string" indexed="true" stored="true"/>
</fields>
<uniqueKey>id</uniqueKey>
Expand Down
1 change: 1 addition & 0 deletions SolrNet.Tests/Resources/solrSchemaMultiValuedName.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
</types>
<fields>
<field name="name" type="string" indexed="true" stored="false" required="true" multiValued="true" />
<field name="author" type="string" indexed="true" stored="false" required="true" multiValued="false" />
</fields>
</schema>
4 changes: 3 additions & 1 deletion SolrNet.Tests/SolrSchemaParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public void SolrFieldParsing()
XmlDocument xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaBasic.xml");
SolrSchema schemaDoc = schemaParser.Parse(xml);

Assert.AreEqual(3, schemaDoc.SolrFields.Count);
Assert.AreEqual(4, schemaDoc.SolrFields.Count);
Assert.AreEqual("id", schemaDoc.SolrFields[0].Name);
Assert.IsTrue(schemaDoc.SolrFields[0].IsRequired);
Assert.IsFalse(schemaDoc.SolrFields[2].IsRequired);
Assert.IsTrue(schemaDoc.SolrFields[3].IsMultiValued);
Assert.IsFalse(schemaDoc.SolrFields[0].IsMultiValued);
Assert.AreEqual("string", schemaDoc.SolrFields[0].Type.Name);
}

Expand Down
18 changes: 17 additions & 1 deletion SolrNet.Tests/ValidationRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void MutivaluedSolrFieldNotMappedToCollectionShouldReturnError() {
[Test]
public void MultivaluedSolrFieldMappedToCollectionShouldNotReturnError() {
var mgr = new MappingManager();
mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("NameCollection"), "name");
mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("NameCollection"), "name");

var schemaManager = new MappingValidationManager(mgr, new[] { new MultivaluedMappedToCollectionRule() });

Expand All @@ -197,5 +197,21 @@ public void MultivaluedSolrFieldMappedToCollectionShouldNotReturnError() {
var validationResults = schemaManager.Validate<SchemaMappingTestDocument>(schema).ToList();
Assert.AreEqual(0, validationResults.Count);
}

[Test]
public void CollectionMappedToNonMultiValuedFolrFieldShouldReturnError()
{
var mgr = new MappingManager();
mgr.Add(typeof(SchemaMappingTestDocument).GetProperty("NameCollection"), "author");

var schemaManager = new MappingValidationManager(mgr, new[] { new MultivaluedMappedToCollectionRule() });

var schemaXmlDocument = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.solrSchemaMultiValuedName.xml");
var solrSchemaParser = new SolrSchemaParser();
var schema = solrSchemaParser.Parse(schemaXmlDocument);

var validationResults = schemaManager.Validate<SchemaMappingTestDocument>(schema).ToList();
Assert.AreEqual(1, validationResults.Count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,46 @@

using System;
using System.Collections.Generic;
using SolrNet.Impl.FieldParsers;
using SolrNet.Schema;

namespace SolrNet.Mapping.Validation.Rules {
/// <summary>
/// Represents a rule that validates that fields mapped to a solr field with mutilvalued set to true
/// are of a type that implements <see cref="IEnumerable{T}" />.
/// </summary>
public class MultivaluedMappedToCollectionRule : IValidationRule {
/// <summary>
/// Validates the specified the mapped document against the solr schema.
/// </summary>
/// <typeparam name="T">The type which mappings will be validated.</typeparam>
/// <param name="solrSchema">The solr schema.</param>
/// <param name="mappingManager">The mapping manager.</param>
/// <returns>
/// A collection of <see cref="MappingValidationItem"/> objects with any issues found during validation.
/// </returns>
public IEnumerable<MappingValidationItem> Validate<T>(SolrSchema solrSchema, IReadOnlyMappingManager mappingManager) {
throw new NotImplementedException();
var collectionFieldParser = new CollectionFieldParser(null); // Used to check if the type is a collection type.

foreach(var prop in mappingManager.GetFields(typeof(T))) {
if (collectionFieldParser.CanHandleType(prop.Key.PropertyType)) { // Field is a collection so solr field should be too.
var solrField = solrSchema.FindSolrFieldByName(prop.Value);
if (solrField != null) {
if(!solrField.IsMultiValued) {
yield return new MappingValidationError(String.Format("SolrField '{0}' is not multivalued while mapped type '{1}' implements IEnumberable<T>.", solrField.Name, prop.Key.Name));
}
}
} else { //Mapped type is not a collection so solr field shouldn't be either.
var solrField = solrSchema.FindSolrFieldByName(prop.Value);
if (solrField != null)
{
if (solrField.IsMultiValued)
{
yield return new MappingValidationError(String.Format("SolrField '{0}' is multivalued while mapped type '{1}' does not implement IEnumberable<T>.", solrField.Name, prop.Key.Name));
}
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions SolrNet/Schema/SolrField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class SolrField
/// </value>
public bool IsRequired { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this instance is multi valued.
/// </summary>
/// <value>
/// <c>true</c> if this instance is multi valued; otherwise, <c>false</c>.
/// </value>
public bool IsMultiValued { get; set; }

/// <summary>
/// Gets or sets the type.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions SolrNet/Schema/SolrSchemaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public SolrSchema Parse(XmlDocument solrSchemaXml) {
var field = new SolrField();
field.Name = fieldNode.Attributes["name"].Value;
field.IsRequired = fieldNode.Attributes["required"] != null ? fieldNode.Attributes["required"].Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
field.IsMultiValued = fieldNode.Attributes["multiValued"] != null ? fieldNode.Attributes["multiValued"].Value.ToLower().Equals(Boolean.TrueString.ToLower()) : false;
foreach(var type in result.SolrFieldTypes) {
if (type.Name.Equals(fieldNode.Attributes["type"].Value)) {
field.Type = type;
Expand Down

0 comments on commit 1267222

Please sign in to comment.