From 0d07dcee8024a982c1fd17c5073407c5d0191e8e Mon Sep 17 00:00:00 2001 From: ejsmith Date: Sat, 15 Oct 2016 14:44:17 -0500 Subject: [PATCH 1/3] Adding support for local metadata on properties. --- .../ElasticsearchPropertyAttributeBase.cs | 3 + src/Nest/Mapping/Types/PropertyBase.cs | 19 ++++-- .../Mapping/Types/PropertyDescriptorBase.cs | 3 + src/Nest/Mapping/Visitor/IMappingVisitor.cs | 68 +++++++++---------- src/Nest/Mapping/Visitor/MappingWalker.cs | 34 +++++----- .../GetMapping/GetMappingApiTest.cs | 34 +++++----- .../Mapping/Metafields/LocalMetadataTests.cs | 64 +++++++++++++++++ src/Tests/Tests.csproj | 1 + 8 files changed, 152 insertions(+), 74 deletions(-) create mode 100644 src/Tests/Mapping/Metafields/LocalMetadataTests.cs diff --git a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs index 79402c62ff1..21f153a9839 100644 --- a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs +++ b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Elasticsearch.Net; @@ -32,5 +33,7 @@ public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo) { return memberInfo.GetCustomAttribute(true); } + + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } } } diff --git a/src/Nest/Mapping/Types/PropertyBase.cs b/src/Nest/Mapping/Types/PropertyBase.cs index eafda63980a..c4ffdb4cc3b 100644 --- a/src/Nest/Mapping/Types/PropertyBase.cs +++ b/src/Nest/Mapping/Types/PropertyBase.cs @@ -1,24 +1,30 @@ -using System.Reflection; +using System.Collections.Generic; +using System.Reflection; using Newtonsoft.Json; namespace Nest { [JsonObject(MemberSerialization.OptIn)] [ContractJsonConverter(typeof(PropertyJsonConverter))] - public interface IProperty : IFieldMapping + public interface IProperty : IFieldMapping, IPropertyWithLocalMetadata { PropertyName Name { get; set; } [JsonProperty("type")] TypeName Type { get; set; } - } + } - public interface IPropertyWithClrOrigin + public interface IPropertyWithClrOrigin { PropertyInfo ClrOrigin { get; set; } } - public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin + public interface IPropertyWithLocalMetadata { + [JsonIgnore] + IDictionary LocalMetadata { get; set; } + } + + public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin { protected PropertyBase(TypeName typeName) { @@ -28,5 +34,6 @@ protected PropertyBase(TypeName typeName) public PropertyName Name { get; set; } public virtual TypeName Type { get; set; } PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; } - } + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + } } diff --git a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs index b719abd569e..96c729df6a7 100644 --- a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; namespace Nest @@ -17,5 +18,7 @@ public abstract class PropertyDescriptorBase public TDescriptor Name(PropertyName name) => Assign(a => a.Name = name); public TDescriptor Name(Expression> objectPath) => Assign(a => a.Name = objectPath); + + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } } } diff --git a/src/Nest/Mapping/Visitor/IMappingVisitor.cs b/src/Nest/Mapping/Visitor/IMappingVisitor.cs index 52bc434c32c..37ace6c8782 100644 --- a/src/Nest/Mapping/Visitor/IMappingVisitor.cs +++ b/src/Nest/Mapping/Visitor/IMappingVisitor.cs @@ -5,64 +5,64 @@ namespace Nest public interface IMappingVisitor { int Depth { get; set; } - void Visit(TypeMapping mapping); + void Visit(ITypeMapping mapping); #pragma warning disable 618 - void Visit(StringProperty property); + void Visit(IStringProperty property); #pragma warning restore 618 - void Visit(TextProperty property); - void Visit(KeywordProperty property); - void Visit(DateProperty property); - void Visit(BooleanProperty property); - void Visit(BinaryProperty property); - void Visit(ObjectProperty property); - void Visit(NestedProperty property); - void Visit(IpProperty property); - void Visit(GeoPointProperty property); - void Visit(GeoShapeProperty property); - void Visit(AttachmentProperty property); - void Visit(NumberProperty property); - void Visit(CompletionProperty property); - void Visit(Murmur3HashProperty property); - void Visit(TokenCountProperty property); + void Visit(ITextProperty property); + void Visit(IKeywordProperty property); + void Visit(IDateProperty property); + void Visit(IBooleanProperty property); + void Visit(IBinaryProperty property); + void Visit(IObjectProperty property); + void Visit(INestedProperty property); + void Visit(IIpProperty property); + void Visit(IGeoPointProperty property); + void Visit(IGeoShapeProperty property); + void Visit(IAttachmentProperty property); + void Visit(INumberProperty property); + void Visit(ICompletionProperty property); + void Visit(IMurmur3HashProperty property); + void Visit(ITokenCountProperty property); } public class NoopMappingVisitor : IMappingVisitor { public virtual int Depth { get; set; } - public virtual void Visit(TypeMapping mapping) { } + public virtual void Visit(ITypeMapping mapping) { } #pragma warning disable 618 - public virtual void Visit(StringProperty property ) { } + public virtual void Visit(IStringProperty property ) { } #pragma warning restore 618 - public virtual void Visit(TextProperty property) { } + public virtual void Visit(ITextProperty property) { } - public virtual void Visit(KeywordProperty property) { } + public virtual void Visit(IKeywordProperty property) { } - public virtual void Visit(DateProperty property) { } + public virtual void Visit(IDateProperty property) { } - public virtual void Visit(BooleanProperty property) { } + public virtual void Visit(IBooleanProperty property) { } - public virtual void Visit(BinaryProperty property) { } + public virtual void Visit(IBinaryProperty property) { } - public virtual void Visit(NumberProperty property) { } + public virtual void Visit(INumberProperty property) { } - public virtual void Visit(ObjectProperty property) { } + public virtual void Visit(IObjectProperty property) { } - public virtual void Visit(NestedProperty property) { } + public virtual void Visit(INestedProperty property) { } - public virtual void Visit(IpProperty property) { } + public virtual void Visit(IIpProperty property) { } - public virtual void Visit(GeoPointProperty property) { } + public virtual void Visit(IGeoPointProperty property) { } - public virtual void Visit(GeoShapeProperty property) { } + public virtual void Visit(IGeoShapeProperty property) { } - public virtual void Visit(AttachmentProperty property) { } + public virtual void Visit(IAttachmentProperty property) { } - public virtual void Visit(CompletionProperty property) { } + public virtual void Visit(ICompletionProperty property) { } - public virtual void Visit(Murmur3HashProperty property) { } + public virtual void Visit(IMurmur3HashProperty property) { } - public virtual void Visit(TokenCountProperty property) { } + public virtual void Visit(ITokenCountProperty property) { } } } diff --git a/src/Nest/Mapping/Visitor/MappingWalker.cs b/src/Nest/Mapping/Visitor/MappingWalker.cs index 02a17dce799..e4b3ff04071 100644 --- a/src/Nest/Mapping/Visitor/MappingWalker.cs +++ b/src/Nest/Mapping/Visitor/MappingWalker.cs @@ -23,7 +23,7 @@ public void Accept(IGetMappingResponse response) } } - public void Accept(TypeMapping mapping) + public void Accept(ITypeMapping mapping) { if (mapping == null) return; this._visitor.Visit(mapping); @@ -41,20 +41,20 @@ public void Accept(IProperties properties) switch (type.Name) { case "text": - var t = field as TextProperty; + var t = field as ITextProperty; if (t == null) continue; this._visitor.Visit(t); this.Accept(t.Fields); break; case "keyword": - var k = field as KeywordProperty; + var k = field as IKeywordProperty; if (k == null) continue; this._visitor.Visit(k); this.Accept(k.Fields); break; case "string": #pragma warning disable 618 - var s = field as StringProperty; + var s = field as IStringProperty; #pragma warning restore 618 if (s == null) continue; this._visitor.Visit(s); @@ -66,31 +66,31 @@ public void Accept(IProperties properties) case "short": case "integer": case "long": - var nu = field as NumberProperty; + var nu = field as INumberProperty; if (nu == null) continue; this._visitor.Visit(nu); this.Accept(nu.Fields); break; case "date": - var d = field as DateProperty; + var d = field as IDateProperty; if (d == null) continue; this._visitor.Visit(d); this.Accept(d.Fields); break; case "boolean": - var bo = field as BooleanProperty; + var bo = field as IBooleanProperty; if (bo == null) continue; this._visitor.Visit(bo); this.Accept(bo.Fields); break; case "binary": - var bi = field as BinaryProperty; + var bi = field as IBinaryProperty; if (bi == null) continue; this._visitor.Visit(bi); this.Accept(bi.Fields); break; case "object": - var o = field as ObjectProperty; + var o = field as IObjectProperty; if (o == null) continue; this._visitor.Visit(o); this._visitor.Depth += 1; @@ -98,7 +98,7 @@ public void Accept(IProperties properties) this._visitor.Depth -= 1; break; case "nested": - var n = field as NestedProperty; + var n = field as INestedProperty; if (n == null) continue; this._visitor.Visit(n); this._visitor.Depth += 1; @@ -106,43 +106,43 @@ public void Accept(IProperties properties) this._visitor.Depth -= 1; break; case "ip": - var i = field as IpProperty; + var i = field as IIpProperty; if (i == null) continue; this._visitor.Visit(i); this.Accept(i.Fields); break; case "geo_point": - var gp = field as GeoPointProperty; + var gp = field as IGeoPointProperty; if (gp == null) continue; this._visitor.Visit(gp); this.Accept(gp.Fields); break; case "geo_shape": - var gs = field as GeoShapeProperty; + var gs = field as IGeoShapeProperty; if (gs == null) continue; this._visitor.Visit(gs); this.Accept(gs.Fields); break; case "attachment": - var a = field as AttachmentProperty; + var a = field as IAttachmentProperty; if (a == null) continue; this._visitor.Visit(a); this.Accept(a.Fields); break; case "completion": - var c = field as CompletionProperty; + var c = field as ICompletionProperty; if (c == null) continue; this._visitor.Visit(c); this.Accept(c.Fields); break; case "murmur3": - var mm = field as Murmur3HashProperty; + var mm = field as IMurmur3HashProperty; if (mm == null) continue; this._visitor.Visit(mm); this.Accept(mm.Fields); break; case "token_count": - var tc = field as TokenCountProperty; + var tc = field as ITokenCountProperty; if (tc == null) continue; this._visitor.Visit(tc); this.Accept(tc.Fields); diff --git a/src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs b/src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs index 5a13b69be04..9e0449b9b4b 100644 --- a/src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs +++ b/src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs @@ -117,87 +117,87 @@ public void CountsShouldContainKeyAndCountBe(string key, int count) } #pragma warning disable 618 - public void Visit(StringProperty mapping) + public void Visit(IStringProperty mapping) { Increment("string"); } #pragma warning restore 618 - public void Visit(DateProperty mapping) + public void Visit(IDateProperty mapping) { Increment("date"); } - public void Visit(BinaryProperty mapping) + public void Visit(IBinaryProperty mapping) { Increment("binary"); } - public void Visit(NestedProperty mapping) + public void Visit(INestedProperty mapping) { Increment("nested"); } - public void Visit(GeoPointProperty mapping) + public void Visit(IGeoPointProperty mapping) { Increment("geo_point"); } - public void Visit(AttachmentProperty mapping) + public void Visit(IAttachmentProperty mapping) { Increment("attachment"); } - public void Visit(CompletionProperty mapping) + public void Visit(ICompletionProperty mapping) { Increment("completion"); } - public void Visit(TokenCountProperty mapping) + public void Visit(ITokenCountProperty mapping) { Increment("token_count"); } - public void Visit(Murmur3HashProperty mapping) + public void Visit(IMurmur3HashProperty mapping) { Increment("murmur3"); } - public void Visit(NumberProperty mapping) + public void Visit(INumberProperty mapping) { Increment("number"); } - public void Visit(GeoShapeProperty mapping) + public void Visit(IGeoShapeProperty mapping) { Increment("geo_shape"); } - public void Visit(IpProperty mapping) + public void Visit(IIpProperty mapping) { Increment("ip"); } - public void Visit(ObjectProperty mapping) + public void Visit(IObjectProperty mapping) { Increment("object"); } - public void Visit(BooleanProperty mapping) + public void Visit(IBooleanProperty mapping) { Increment("boolean"); } - public void Visit(TextProperty mapping) + public void Visit(ITextProperty mapping) { Increment("text"); } - public void Visit(KeywordProperty mapping) + public void Visit(IKeywordProperty mapping) { Increment("keyword"); } - public void Visit(TypeMapping mapping) + public void Visit(ITypeMapping mapping) { Increment("type"); } diff --git a/src/Tests/Mapping/Metafields/LocalMetadataTests.cs b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs new file mode 100644 index 00000000000..5deaba710c3 --- /dev/null +++ b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using Nest; +using Tests.Mapping.Types.Core.Text; +using Xunit; + +namespace Tests.Mapping.Metafields +{ + public class LocalMetadataTests + { + [Fact] + public void CanAssignAndAccessLocalMetadata() + { + var descriptor = new TypeMappingDescriptor().Properties(p => p + .Text(t => t + .Name(o => o.Full) + .Norms() + .AddTestLocalMetadata() + )) as ITypeMapping; + + var visitor = new LocalMatadataVisitor(); + var walker = new MappingWalker(visitor); + walker.Accept(descriptor.Properties); + + visitor.MetadataCount.Should().Be(1); + } + } + + public static class TestLocalMetadataMappingExtensions + { + public static TDescriptor AddTestLocalMetadata(this TDescriptor descriptor) + where TDescriptor : IDescriptor + { + var descriptorWithLocalMetadata = descriptor as IPropertyWithLocalMetadata; + + if (descriptorWithLocalMetadata == null) + return descriptor; + + if (descriptorWithLocalMetadata.LocalMetadata == null) + descriptorWithLocalMetadata.LocalMetadata = new Dictionary(); + + descriptorWithLocalMetadata.LocalMetadata.Add("Test", "TestValue"); + + return descriptor; + } + } + + public class LocalMatadataVisitor : NoopMappingVisitor + { + public int MetadataCount { get; set; } + + public override void Visit(ITextProperty property) + { + var propertyWithLocalMetadata = property as IPropertyWithLocalMetadata; + propertyWithLocalMetadata.Should().NotBeNull(); + propertyWithLocalMetadata.LocalMetadata.Should().NotBeNull(); + + MetadataCount += propertyWithLocalMetadata.LocalMetadata.Count; + + propertyWithLocalMetadata.LocalMetadata.Should().Contain("Test", "TestValue"); + } + } +} diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index ac9f1e1b228..3db754083ef 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -391,6 +391,7 @@ + From 30bb290a1edf6661d90a02b498dd5fce88b40b0a Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Mon, 17 Oct 2016 13:17:06 -0500 Subject: [PATCH 2/3] Fix whitespace --- .../ElasticsearchPropertyAttributeBase.cs | 4 +- src/Nest/Mapping/TypeMapping.cs | 8 +- src/Nest/Mapping/Types/PropertyBase.cs | 18 ++-- .../Mapping/Types/PropertyDescriptorBase.cs | 2 +- .../Mapping/Metafields/LocalMetadataTests.cs | 86 +++++++++---------- 5 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs index 21f153a9839..0ced784fcf6 100644 --- a/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs +++ b/src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs @@ -32,8 +32,8 @@ protected ElasticsearchPropertyAttributeBase(Type type) public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo) { return memberInfo.GetCustomAttribute(true); - } + } - IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } } } diff --git a/src/Nest/Mapping/TypeMapping.cs b/src/Nest/Mapping/TypeMapping.cs index 07bedbb2c32..540590c3b1a 100644 --- a/src/Nest/Mapping/TypeMapping.cs +++ b/src/Nest/Mapping/TypeMapping.cs @@ -157,11 +157,11 @@ public TypeMappingDescriptor AutoMap(IPropertyVisitor visitor = null, int max /// public TypeMappingDescriptor SizeField(Func sizeFieldSelector) => Assign(a => a.SizeField = sizeFieldSelector?.Invoke(new SizeFieldDescriptor())); - /// - public TypeMappingDescriptor SourceField(Func sourceFieldSelector) => Assign(a => a.SourceField = sourceFieldSelector?.Invoke(new SourceFieldDescriptor())); + /// + public TypeMappingDescriptor SourceField(Func sourceFieldSelector) => Assign(a => a.SourceField = sourceFieldSelector?.Invoke(new SourceFieldDescriptor())); - /// - public TypeMappingDescriptor DisableSizeField(bool disabled = true) => Assign(a => a.SizeField = new SizeField { Enabled = !disabled }); + /// + public TypeMappingDescriptor DisableSizeField(bool disabled = true) => Assign(a => a.SizeField = new SizeField { Enabled = !disabled }); /// public TypeMappingDescriptor DisableIndexField(bool disabled = true) => Assign(a => a.IndexField = new IndexField { Enabled = !disabled }); diff --git a/src/Nest/Mapping/Types/PropertyBase.cs b/src/Nest/Mapping/Types/PropertyBase.cs index c4ffdb4cc3b..a32e2052f64 100644 --- a/src/Nest/Mapping/Types/PropertyBase.cs +++ b/src/Nest/Mapping/Types/PropertyBase.cs @@ -12,19 +12,19 @@ public interface IProperty : IFieldMapping, IPropertyWithLocalMetadata [JsonProperty("type")] TypeName Type { get; set; } - } + } - public interface IPropertyWithClrOrigin + public interface IPropertyWithClrOrigin { PropertyInfo ClrOrigin { get; set; } } - public interface IPropertyWithLocalMetadata { - [JsonIgnore] - IDictionary LocalMetadata { get; set; } - } + public interface IPropertyWithLocalMetadata { + [JsonIgnore] + IDictionary LocalMetadata { get; set; } + } - public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin + public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin { protected PropertyBase(TypeName typeName) { @@ -34,6 +34,6 @@ protected PropertyBase(TypeName typeName) public PropertyName Name { get; set; } public virtual TypeName Type { get; set; } PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; } - IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } - } + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + } } diff --git a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs index 96c729df6a7..8f1cb36403f 100644 --- a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs @@ -19,6 +19,6 @@ public abstract class PropertyDescriptorBase public TDescriptor Name(Expression> objectPath) => Assign(a => a.Name = objectPath); - IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } } } diff --git a/src/Tests/Mapping/Metafields/LocalMetadataTests.cs b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs index 5deaba710c3..c9c19129d10 100644 --- a/src/Tests/Mapping/Metafields/LocalMetadataTests.cs +++ b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs @@ -7,58 +7,58 @@ namespace Tests.Mapping.Metafields { - public class LocalMetadataTests - { - [Fact] - public void CanAssignAndAccessLocalMetadata() - { - var descriptor = new TypeMappingDescriptor().Properties(p => p - .Text(t => t - .Name(o => o.Full) - .Norms() - .AddTestLocalMetadata() - )) as ITypeMapping; + public class LocalMetadataTests + { + [Fact] + public void CanAssignAndAccessLocalMetadata() + { + var descriptor = new TypeMappingDescriptor().Properties(p => p + .Text(t => t + .Name(o => o.Full) + .Norms() + .AddTestLocalMetadata() + )) as ITypeMapping; - var visitor = new LocalMatadataVisitor(); - var walker = new MappingWalker(visitor); - walker.Accept(descriptor.Properties); + var visitor = new LocalMatadataVisitor(); + var walker = new MappingWalker(visitor); + walker.Accept(descriptor.Properties); - visitor.MetadataCount.Should().Be(1); - } - } + visitor.MetadataCount.Should().Be(1); + } + } - public static class TestLocalMetadataMappingExtensions - { - public static TDescriptor AddTestLocalMetadata(this TDescriptor descriptor) - where TDescriptor : IDescriptor - { - var descriptorWithLocalMetadata = descriptor as IPropertyWithLocalMetadata; + public static class TestLocalMetadataMappingExtensions + { + public static TDescriptor AddTestLocalMetadata(this TDescriptor descriptor) + where TDescriptor : IDescriptor + { + var descriptorWithLocalMetadata = descriptor as IPropertyWithLocalMetadata; - if (descriptorWithLocalMetadata == null) - return descriptor; + if (descriptorWithLocalMetadata == null) + return descriptor; - if (descriptorWithLocalMetadata.LocalMetadata == null) - descriptorWithLocalMetadata.LocalMetadata = new Dictionary(); + if (descriptorWithLocalMetadata.LocalMetadata == null) + descriptorWithLocalMetadata.LocalMetadata = new Dictionary(); - descriptorWithLocalMetadata.LocalMetadata.Add("Test", "TestValue"); + descriptorWithLocalMetadata.LocalMetadata.Add("Test", "TestValue"); - return descriptor; - } - } + return descriptor; + } + } - public class LocalMatadataVisitor : NoopMappingVisitor - { - public int MetadataCount { get; set; } + public class LocalMatadataVisitor : NoopMappingVisitor + { + public int MetadataCount { get; set; } - public override void Visit(ITextProperty property) - { - var propertyWithLocalMetadata = property as IPropertyWithLocalMetadata; - propertyWithLocalMetadata.Should().NotBeNull(); - propertyWithLocalMetadata.LocalMetadata.Should().NotBeNull(); + public override void Visit(ITextProperty property) + { + var propertyWithLocalMetadata = property as IPropertyWithLocalMetadata; + propertyWithLocalMetadata.Should().NotBeNull(); + propertyWithLocalMetadata.LocalMetadata.Should().NotBeNull(); - MetadataCount += propertyWithLocalMetadata.LocalMetadata.Count; + MetadataCount += propertyWithLocalMetadata.LocalMetadata.Count; - propertyWithLocalMetadata.LocalMetadata.Should().Contain("Test", "TestValue"); - } - } + propertyWithLocalMetadata.LocalMetadata.Should().Contain("Test", "TestValue"); + } + } } From 59dd1caab2bf60aee14845567b5b41bc5bbb2ad2 Mon Sep 17 00:00:00 2001 From: ejsmith Date: Thu, 20 Oct 2016 16:16:51 -0500 Subject: [PATCH 3/3] Move LocalMetadata into the IProperty interface --- src/Nest/Mapping/Types/PropertyBase.cs | 19 ++++++++++++------- .../Mapping/Types/PropertyDescriptorBase.cs | 5 ++++- .../Mapping/Metafields/LocalMetadataTests.cs | 19 +++++++++---------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Nest/Mapping/Types/PropertyBase.cs b/src/Nest/Mapping/Types/PropertyBase.cs index c4ffdb4cc3b..732efe3acdf 100644 --- a/src/Nest/Mapping/Types/PropertyBase.cs +++ b/src/Nest/Mapping/Types/PropertyBase.cs @@ -6,12 +6,18 @@ namespace Nest { [JsonObject(MemberSerialization.OptIn)] [ContractJsonConverter(typeof(PropertyJsonConverter))] - public interface IProperty : IFieldMapping, IPropertyWithLocalMetadata + public interface IProperty : IFieldMapping { PropertyName Name { get; set; } [JsonProperty("type")] TypeName Type { get; set; } + + /// + /// Local property metadata that will NOT be stored in Elasticsearch with the mappings + /// + [JsonIgnore] + IDictionary LocalMetadata { get; set; } } public interface IPropertyWithClrOrigin @@ -19,11 +25,6 @@ public interface IPropertyWithClrOrigin PropertyInfo ClrOrigin { get; set; } } - public interface IPropertyWithLocalMetadata { - [JsonIgnore] - IDictionary LocalMetadata { get; set; } - } - public abstract class PropertyBase : IProperty, IPropertyWithClrOrigin { protected PropertyBase(TypeName typeName) @@ -34,6 +35,10 @@ protected PropertyBase(TypeName typeName) public PropertyName Name { get; set; } public virtual TypeName Type { get; set; } PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; } - IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + + /// + /// Local property metadata that will NOT be stored in Elasticsearch with the mappings + /// + public IDictionary LocalMetadata { get; set; } } } diff --git a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs index 96c729df6a7..776e5231d74 100644 --- a/src/Nest/Mapping/Types/PropertyDescriptorBase.cs +++ b/src/Nest/Mapping/Types/PropertyDescriptorBase.cs @@ -19,6 +19,9 @@ public abstract class PropertyDescriptorBase public TDescriptor Name(Expression> objectPath) => Assign(a => a.Name = objectPath); - IDictionary IPropertyWithLocalMetadata.LocalMetadata { get; set; } + /// + /// Local property metadata that will NOT be stored in Elasticsearch with the mappings + /// + public IDictionary LocalMetadata { get; set; } } } diff --git a/src/Tests/Mapping/Metafields/LocalMetadataTests.cs b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs index 5deaba710c3..f4ce457c9e3 100644 --- a/src/Tests/Mapping/Metafields/LocalMetadataTests.cs +++ b/src/Tests/Mapping/Metafields/LocalMetadataTests.cs @@ -32,15 +32,15 @@ public static class TestLocalMetadataMappingExtensions public static TDescriptor AddTestLocalMetadata(this TDescriptor descriptor) where TDescriptor : IDescriptor { - var descriptorWithLocalMetadata = descriptor as IPropertyWithLocalMetadata; + var propertyDescriptor = descriptor as IProperty; - if (descriptorWithLocalMetadata == null) + if (propertyDescriptor == null) return descriptor; - if (descriptorWithLocalMetadata.LocalMetadata == null) - descriptorWithLocalMetadata.LocalMetadata = new Dictionary(); + if (propertyDescriptor.LocalMetadata == null) + propertyDescriptor.LocalMetadata = new Dictionary(); - descriptorWithLocalMetadata.LocalMetadata.Add("Test", "TestValue"); + propertyDescriptor.LocalMetadata.Add("Test", "TestValue"); return descriptor; } @@ -52,13 +52,12 @@ public class LocalMatadataVisitor : NoopMappingVisitor public override void Visit(ITextProperty property) { - var propertyWithLocalMetadata = property as IPropertyWithLocalMetadata; - propertyWithLocalMetadata.Should().NotBeNull(); - propertyWithLocalMetadata.LocalMetadata.Should().NotBeNull(); + property.Should().NotBeNull(); + property.LocalMetadata.Should().NotBeNull(); - MetadataCount += propertyWithLocalMetadata.LocalMetadata.Count; + MetadataCount += property.LocalMetadata.Count; - propertyWithLocalMetadata.LocalMetadata.Should().Contain("Test", "TestValue"); + property.LocalMetadata.Should().Contain("Test", "TestValue"); } } }