From 400b452554915d3f63d70a3da93e203c1915e6ea Mon Sep 17 00:00:00 2001 From: Mpdreamz Date: Fri, 8 Dec 2017 17:17:05 +0100 Subject: [PATCH 1/2] can now map clr types dynamically on connection settings including an overload that takes a list of mappings to ease more dynamic configuration of types at run time --- .../ConnectionSettings/ClrTypeMapping.cs | 155 -------------- .../ConnectionSettingsBase.cs | 111 +++++++--- .../ConnectionSettings/PocoMapping.cs | 194 ++++++++++++++++++ src/Nest/Mapping/PropertyMapping.cs | 2 +- 4 files changed, 273 insertions(+), 189 deletions(-) delete mode 100644 src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs create mode 100644 src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs deleted file mode 100644 index 537d77fc502..00000000000 --- a/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using Elasticsearch.Net; - -namespace Nest -{ - public interface IClrTypeMapping where T : class - { - Type Type { get; } - - /// - /// When specified dictates the default Elasticsearch index name for - /// - string IndexName { get; set; } - - /// - /// When specified dictates the default Elasticsearch type name for - /// - string TypeName { get; set; } - - /// - /// When specified dictates the relation name for to resolve to. - /// - string RelationName { get; set; } - - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - Expression> IdProperty { get; set; } - - /// - /// When specified allows you to ignore or rename certain properties of clr type - /// - IList> Properties { get; set; } - } - - public class ClrTypeMapping : IClrTypeMapping where T : class - { - public Type Type { get; } = typeof (T); - - /// - /// When specified dictates the default Elasticsearch index name for - /// - public string IndexName { get; set; } - - /// - /// When specified dictates the default Elasticsearch type name for - /// - public string TypeName { get; set; } - - /// - /// When specified dictates the relation name for to resolve to. - /// - public string RelationName { get; set; } - - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - public Expression> IdProperty { get; set; } - - public IList> Properties { get; set; } - } - - public class ClrTypeMappingDescriptor : DescriptorBase,IClrTypeMapping> , IClrTypeMapping - where T : class - { - Type IClrTypeMapping.Type { get; } = typeof (T); - string IClrTypeMapping.IndexName { get; set; } - string IClrTypeMapping.TypeName { get; set; } - string IClrTypeMapping.RelationName { get; set; } - Expression> IClrTypeMapping.IdProperty { get; set; } - IList> IClrTypeMapping.Properties { get; set; } = new List>(); - - /// - /// When specified dictates the default Elasticsearch index name for - /// - public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); - - /// - /// When specified dictates the default Elasticsearch type name for - /// - public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); - - /// - /// When specified dictates the relation name for to resolve to. - /// - public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); - - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - public ClrTypeMappingDescriptor IdProperty(Expression> property) => Assign(a => a.IdProperty = property); - - /// - /// When specified allows you to ignore on clr type - /// - public ClrTypeMappingDescriptor Ignore(Expression> property) => - Assign(a => a.Properties.Add(new IgnorePropertyMapping(property))); - - /// - /// When specified allows you to rename on clr type - /// - public ClrTypeMappingDescriptor Rename(Expression> property, string newName) => - Assign(a => a.Properties.Add(new RenamePropertyMapping(property, newName))); - - } - - public interface IClrTypePropertyMapping where T : class - { - Expression> Property { get; set; } - bool Ignore { get; set; } - string NewName { get; set; } - IPropertyMapping ToPropertyMapping(); - } - - public abstract class ClrPropertyMappingBase : IClrTypePropertyMapping - where T : class - { - protected IClrTypePropertyMapping Self => this; - - Expression> IClrTypePropertyMapping.Property { get; set; } - - bool IClrTypePropertyMapping.Ignore { get; set; } - - string IClrTypePropertyMapping.NewName { get; set; } - - protected ClrPropertyMappingBase(Expression> property) - { - Self.Property = property; - } - - IPropertyMapping IClrTypePropertyMapping.ToPropertyMapping() => Self.Ignore ? PropertyMapping.Ignored : new PropertyMapping {Name = Self.NewName}; - } - - public class IgnorePropertyMapping : ClrPropertyMappingBase where T : class - { - public IgnorePropertyMapping(Expression> property) : base(property) - { - Self.Ignore = true; - } - } - - public class RenamePropertyMapping : ClrPropertyMappingBase where T : class - { - public RenamePropertyMapping(Expression> property, string newName) : base(property) - { - newName.ThrowIfNull(nameof(newName)); - Self.NewName = newName; - } - } - - - -} diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs index 3fb5d92d13b..be29a47f732 100644 --- a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs @@ -20,10 +20,12 @@ public ConnectionSettings(Uri uri = null) : this(new SingleNodeConnectionPool(uri ?? new Uri("http://localhost:9200"))) { } public ConnectionSettings(IConnectionPool connectionPool) : this(connectionPool, null, null) { } + public ConnectionSettings(IConnectionPool connectionPool, SourceSerializerFactory sourceSerializer) : this(connectionPool, null, sourceSerializer) { } public ConnectionSettings(IConnectionPool connectionPool, IConnection connection) : this(connectionPool, connection, null) { } + public ConnectionSettings(IConnectionPool connectionPool, IConnection connection, SourceSerializerFactory sourceSerializer) : this(connectionPool, connection, sourceSerializer, null) { } @@ -33,7 +35,6 @@ public ConnectionSettings( SourceSerializerFactory sourceSerializer, IPropertyMappingProvider propertyMappingProvider) : base(connectionPool, connection, sourceSerializer, propertyMappingProvider) { } - } /// @@ -85,7 +86,7 @@ protected ConnectionSettingsBase( IConnection connection, ConnectionSettings.SourceSerializerFactory sourceSerializerFactory, IPropertyMappingProvider propertyMappingProvider - ) + ) : base(connectionPool, connection, null) { var defaultSerializer = new JsonNetSerializer(this); @@ -113,7 +114,7 @@ IPropertyMappingProvider propertyMappingProvider public TConnectionSettings PluralizeTypeNames() { this._defaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer; - return (TConnectionSettings)this; + return (TConnectionSettings) this; } /// @@ -125,7 +126,7 @@ public TConnectionSettings PluralizeTypeNames() public TConnectionSettings DefaultIndex(string defaultIndex) { this._defaultIndex = defaultIndex; - return (TConnectionSettings)this; + return (TConnectionSettings) this; } private string LowerCaseAndPluralizeTypeNameInferrer(Type type) @@ -143,7 +144,7 @@ private string LowerCaseAndPluralizeTypeNameInferrer(Type type) public TConnectionSettings DefaultFieldNameInferrer(Func fieldNameInferrer) { this._defaultFieldNameInferrer = fieldNameInferrer; - return (TConnectionSettings)this; + return (TConnectionSettings) this; } /// @@ -155,17 +156,17 @@ public TConnectionSettings DefaultTypeNameInferrer(Func typeNameIn { typeNameInferrer.ThrowIfNull(nameof(typeNameInferrer)); this._defaultTypeNameInferrer = typeNameInferrer; - return (TConnectionSettings)this; + return (TConnectionSettings) this; } - /// - /// Specify which property on a given POCO should be used to infer the id of the document when - /// indexed in Elasticsearch. - /// - /// The type of the document. - /// The object path. - /// - private TConnectionSettings MapIdPropertyFor(Expression> objectPath) + /// + /// Specify which property on a given POCO should be used to infer the id of the document when + /// indexed in Elasticsearch. + /// + /// The type of the document. + /// The object path. + /// + private TConnectionSettings MapIdPropertyFor(Expression> objectPath) { objectPath.ThrowIfNull(nameof(objectPath)); @@ -175,17 +176,18 @@ private TConnectionSettings MapIdPropertyFor(Expression(IList> mappings) + private void ApplyPropertyMappings(IList> mappings) where TDocument : class { foreach (var mapping in mappings) @@ -207,34 +209,35 @@ private void ApplyPropertyMappings(IList - /// Specify how the mapping is inferred for a given POCO type. - /// Can be used to infer the index, type, id property and properties for the POCO. - /// - /// The type of the document. - /// The selector. - /// - public TConnectionSettings InferMappingFor(Func, IClrTypeMapping> selector) + /// + /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type and relation names. + /// The generic version also allows you to set a default id property and control serialization behavior for properties for the POCO. + /// + /// The type of the document. + /// The selector. + public TConnectionSettings InferMappingFor(Func, IPocoMapping> selector) where TDocument : class { - var inferMapping = selector(new ClrTypeMappingDescriptor()); + var inferMapping = selector(new PocoMappingDescriptor()); if (!inferMapping.IndexName.IsNullOrEmpty()) - this._defaultIndices.Add(inferMapping.Type, inferMapping.IndexName); + this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName); if (!inferMapping.TypeName.IsNullOrEmpty()) - this._defaultTypeNames.Add(inferMapping.Type, inferMapping.TypeName); + this._defaultTypeNames.Add(inferMapping.ClrType, inferMapping.TypeName); if (!inferMapping.RelationName.IsNullOrEmpty()) - this._defaultRelationNames.Add(inferMapping.Type, inferMapping.RelationName); + this._defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName); if (inferMapping.IdProperty != null) this.MapIdPropertyFor(inferMapping.IdProperty); @@ -242,8 +245,50 @@ public TConnectionSettings InferMappingFor(Func(inferMapping.Properties); - return (TConnectionSettings)this; + return (TConnectionSettings) this; } + /// + /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type, and relation names. + /// + /// The type of the POCO you wish to configure + /// describe the POCO configuration + public TConnectionSettings InferMappingFor(Type documentType, Func selector) + { + var inferMapping = selector(new PocoMappingDescriptor(documentType)); + if (!inferMapping.IndexName.IsNullOrEmpty()) + this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName); + + if (!inferMapping.TypeName.IsNullOrEmpty()) + this._defaultTypeNames.Add(inferMapping.ClrType, inferMapping.TypeName); + + if (!inferMapping.RelationName.IsNullOrEmpty()) + this._defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName); + + return (TConnectionSettings) this; + } + + /// + /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type, and relation names. + /// + /// The type of the POCO you wish to configure + /// describe the POCO configuration + public TConnectionSettings InferMappings(IEnumerable typeMappings) + { + if (typeMappings == null) return (TConnectionSettings) this; + foreach (var inferMapping in typeMappings) + { + if (!inferMapping.IndexName.IsNullOrEmpty()) + this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName); + + if (!inferMapping.TypeName.IsNullOrEmpty()) + this._defaultTypeNames.Add(inferMapping.ClrType, inferMapping.TypeName); + + if (!inferMapping.RelationName.IsNullOrEmpty()) + this._defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName); + } + + return (TConnectionSettings) this; + } } } diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs b/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs new file mode 100644 index 00000000000..a442901f34f --- /dev/null +++ b/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; + +namespace Nest +{ + public interface IPocoMapping + { + Type ClrType { get; } + + /// + /// When specified dictates the default Elasticsearch index name for + /// + string IndexName { get; set; } + + /// + /// When specified dictates the default Elasticsearch type name for + /// + string TypeName { get; set; } + + /// + /// When specified dictates the relation name for to resolve to. + /// + string RelationName { get; set; } + + } + + public interface IPocoMapping : IPocoMapping where T : class + { + /// + /// Allows you to set a default Id property on that NEST will evaluate + /// + Expression> IdProperty { get; set; } + + /// + /// When specified allows you to ignore or rename certain properties of clr type + /// + IList> Properties { get; set; } + } + + public class PocoMapping : IPocoMapping + { + + public Type ClrType { get; } + public PocoMapping(Type type) => ClrType = type; + + /// + /// When specified dictates the default Elasticsearch index name for + /// + public string IndexName { get; set; } + + /// + /// When specified dictates the default Elasticsearch type name for + /// + public string TypeName { get; set; } + + /// + /// When specified dictates the relation name for to resolve to. + /// + public string RelationName { get; set; } + + } + public class PocoMapping : PocoMapping, IPocoMapping where T : class + { + public PocoMapping() : base(typeof(T)) { } + + /// + /// Allows you to set a default Id property on that NEST will evaluate + /// + public Expression> IdProperty { get; set; } + + /// + /// When specified allows you to ignore or rename certain properties of clr type + /// + public IList> Properties { get; set; } + + } + + public class PocoMappingDescriptor : DescriptorBase,IPocoMapping> , IPocoMapping + where T : class + { + Type IPocoMapping.ClrType { get; } = typeof (T); + string IPocoMapping.IndexName { get; set; } + string IPocoMapping.TypeName { get; set; } + string IPocoMapping.RelationName { get; set; } + Expression> IPocoMapping.IdProperty { get; set; } + IList> IPocoMapping.Properties { get; set; } = new List>(); + + /// + /// When specified dictates the default Elasticsearch index name for + /// + public PocoMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); + + /// + /// When specified dictates the default Elasticsearch type name for + /// + public PocoMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); + + /// + /// When specified dictates the relation name for to resolve to. + /// + public PocoMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); + + /// + /// Allows you to set a default Id property on that NEST will evaluate + /// + public PocoMappingDescriptor IdProperty(Expression> property) => Assign(a => a.IdProperty = property); + + /// + /// When specified allows you to ignore on clr type + /// + public PocoMappingDescriptor Ignore(Expression> property) => + Assign(a => a.Properties.Add(new IgnorePropertyMapping(property))); + + /// + /// When specified allows you to rename on clr type + /// + public PocoMappingDescriptor Rename(Expression> property, string newName) => + Assign(a => a.Properties.Add(new RenamePropertyMapping(property, newName))); + + } + + public class PocoMappingDescriptor : DescriptorBase , IPocoMapping + { + private readonly Type _type; + Type IPocoMapping.ClrType => _type; + string IPocoMapping.IndexName { get; set; } + string IPocoMapping.TypeName { get; set; } + string IPocoMapping.RelationName { get; set; } + public PocoMappingDescriptor(Type type) => _type = type; + + /// + /// When specified dictates the default Elasticsearch index name for + /// + public PocoMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); + + /// + /// When specified dictates the default Elasticsearch type name for + /// + public PocoMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); + + /// + /// When specified dictates the relation name for to resolve to. + /// + public PocoMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); + } + + public interface IPocoPropertyMapping where T : class + { + Expression> Property { get; set; } + bool Ignore { get; set; } + string NewName { get; set; } + IPropertyMapping ToPropertyMapping(); + } + + public abstract class PocoPropertyMappingBase : IPocoPropertyMapping + where T : class + { + protected IPocoPropertyMapping Self => this; + + Expression> IPocoPropertyMapping.Property { get; set; } + + bool IPocoPropertyMapping.Ignore { get; set; } + + string IPocoPropertyMapping.NewName { get; set; } + + protected PocoPropertyMappingBase(Expression> property) + { + Self.Property = property; + } + + IPropertyMapping IPocoPropertyMapping.ToPropertyMapping() => Self.Ignore ? PropertyMapping.Ignored : new PropertyMapping {Name = Self.NewName}; + } + + public class IgnorePropertyMapping : PocoPropertyMappingBase where T : class + { + public IgnorePropertyMapping(Expression> property) : base(property) + { + Self.Ignore = true; + } + } + + public class RenamePropertyMapping : PocoPropertyMappingBase where T : class + { + public RenamePropertyMapping(Expression> property, string newName) : base(property) + { + newName.ThrowIfNull(nameof(newName)); + Self.NewName = newName; + } + } + + + +} diff --git a/src/Nest/Mapping/PropertyMapping.cs b/src/Nest/Mapping/PropertyMapping.cs index eda010ddd6a..0c72bfa6bf4 100644 --- a/src/Nest/Mapping/PropertyMapping.cs +++ b/src/Nest/Mapping/PropertyMapping.cs @@ -8,7 +8,7 @@ namespace Nest public class PropertyMappingDescriptor : DescriptorBase, IDescriptor> where TDocument : class { - internal IList> Mappings { get; } = new List>(); + internal IList> Mappings { get; } = new List>(); public PropertyMappingDescriptor Rename(Expression> property, string field) { From ce1127142ff42c201664c12095dfa34b77d24e93 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Thu, 14 Dec 2017 11:02:43 +1100 Subject: [PATCH 2/2] Rename Poco* types back to Clr* --- .../ConnectionSettings/ClrPropertyMapping.cs | 43 ++++ .../ConnectionSettings/ClrTypeMapping.cs | 149 ++++++++++++++ .../ConnectionSettingsBase.cs | 12 +- .../ConnectionSettings/PocoMapping.cs | 194 ------------------ .../SerializationBehavior/PropertyMapping.cs | 18 +- src/Nest/Mapping/PropertyMapping.cs | 6 +- 6 files changed, 214 insertions(+), 208 deletions(-) create mode 100644 src/Nest/CommonAbstractions/ConnectionSettings/ClrPropertyMapping.cs create mode 100644 src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs delete mode 100644 src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ClrPropertyMapping.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ClrPropertyMapping.cs new file mode 100644 index 00000000000..4db5de2a669 --- /dev/null +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ClrPropertyMapping.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq.Expressions; + +namespace Nest +{ + public abstract class ClrPropertyMappingBase : IClrPropertyMapping + where TDocument : class + { + Expression> IClrPropertyMapping.Property { get; set; } + bool IClrPropertyMapping.Ignore { get; set; } + string IClrPropertyMapping.NewName { get; set; } + + protected IClrPropertyMapping Self => this; + + protected ClrPropertyMappingBase(Expression> property) => Self.Property = property; + + IPropertyMapping IClrPropertyMapping.ToPropertyMapping() => Self.Ignore + ? PropertyMapping.Ignored + : new PropertyMapping {Name = Self.NewName}; + } + + public interface IClrPropertyMapping where TDocument : class + { + Expression> Property { get; set; } + bool Ignore { get; set; } + string NewName { get; set; } + IPropertyMapping ToPropertyMapping(); + } + + public class IgnoreClrPropertyMapping : ClrPropertyMappingBase where TDocument : class + { + public IgnoreClrPropertyMapping(Expression> property) : base(property) => Self.Ignore = true; + } + + public class RenameClrPropertyMapping : ClrPropertyMappingBase where TDocument : class + { + public RenameClrPropertyMapping(Expression> property, string newName) : base(property) + { + newName.ThrowIfNull(nameof(newName)); + Self.NewName = newName; + } + } +} diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs new file mode 100644 index 00000000000..f11ec67604d --- /dev/null +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ClrTypeMapping.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; + +namespace Nest +{ + public interface IClrTypeMapping + { + /// + /// The CLR type the mapping relates to + /// + Type ClrType { get; } + + /// + /// The default Elasticsearch index name for + /// + string IndexName { get; set; } + + /// + /// The default Elasticsearch type name for + /// + string TypeName { get; set; } + + /// + /// The relation name for to resolve to. + /// + string RelationName { get; set; } + } + + public interface IClrTypeMapping : IClrTypeMapping where TDocument : class + { + /// + /// Set a default Id property on CLR type that NEST will evaluate + /// + Expression> IdProperty { get; set; } + + /// + /// Ignore or rename certain properties of CLR type + /// + IList> Properties { get; set; } + } + + public class ClrTypeMapping : IClrTypeMapping + { + /// + /// Initializes a new instance of + /// + public ClrTypeMapping(Type type) => ClrType = type; + + /// + public Type ClrType { get; } + + /// + public string IndexName { get; set; } + + /// + public string TypeName { get; set; } + + /// + public string RelationName { get; set; } + + } + public class ClrTypeMapping : ClrTypeMapping, IClrTypeMapping where TDocument : class + { + public ClrTypeMapping() : base(typeof(TDocument)) { } + + /// + public Expression> IdProperty { get; set; } + + /// + public IList> Properties { get; set; } + } + + public class ClrTypeMappingDescriptor : DescriptorBase , IClrTypeMapping + { + private readonly Type _type; + + /// + /// Instantiates a new instance of + /// + /// The CLR type to map + public ClrTypeMappingDescriptor(Type type) => _type = type; + + Type IClrTypeMapping.ClrType => _type; + string IClrTypeMapping.IndexName { get; set; } + string IClrTypeMapping.TypeName { get; set; } + string IClrTypeMapping.RelationName { get; set; } + + /// + /// The default Elasticsearch index name for the CLR type + /// + public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); + + /// + /// The default Elasticsearch type name for the CLR type + /// + public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); + + /// + /// The relation name for the CLR type to resolve to. + /// + public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); + } + + public class ClrTypeMappingDescriptor + : DescriptorBase,IClrTypeMapping>, IClrTypeMapping + where TDocument : class + { + Type IClrTypeMapping.ClrType { get; } = typeof (TDocument); + string IClrTypeMapping.IndexName { get; set; } + string IClrTypeMapping.TypeName { get; set; } + string IClrTypeMapping.RelationName { get; set; } + Expression> IClrTypeMapping.IdProperty { get; set; } + IList> IClrTypeMapping.Properties { get; set; } = new List>(); + + /// + /// The default Elasticsearch index name for + /// + public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); + + /// + /// The default Elasticsearch type name for + /// + public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); + + /// + /// The relation name for to resolve to. + /// + public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); + + /// + /// Set a default Id property on CLR type that NEST will evaluate + /// + public ClrTypeMappingDescriptor IdProperty(Expression> property) => Assign(a => a.IdProperty = property); + + /// + /// Ignore on CLR type + /// + public ClrTypeMappingDescriptor Ignore(Expression> property) => + Assign(a => a.Properties.Add(new IgnoreClrPropertyMapping(property))); + + /// + /// Rename on CLR type + /// + public ClrTypeMappingDescriptor Rename(Expression> property, string newName) => + Assign(a => a.Properties.Add(new RenameClrPropertyMapping(property, newName))); + + } +} diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs index be29a47f732..edc225eb158 100644 --- a/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs +++ b/src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs @@ -187,7 +187,7 @@ private TConnectionSettings MapIdPropertyFor(Expression(IList> mappings) + private void ApplyPropertyMappings(IList> mappings) where TDocument : class { foreach (var mapping in mappings) @@ -226,10 +226,10 @@ private void ApplyPropertyMappings(IList /// The type of the document. /// The selector. - public TConnectionSettings InferMappingFor(Func, IPocoMapping> selector) + public TConnectionSettings InferMappingFor(Func, IClrTypeMapping> selector) where TDocument : class { - var inferMapping = selector(new PocoMappingDescriptor()); + var inferMapping = selector(new ClrTypeMappingDescriptor()); if (!inferMapping.IndexName.IsNullOrEmpty()) this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName); @@ -253,9 +253,9 @@ public TConnectionSettings InferMappingFor(Func /// The type of the POCO you wish to configure /// describe the POCO configuration - public TConnectionSettings InferMappingFor(Type documentType, Func selector) + public TConnectionSettings InferMappingFor(Type documentType, Func selector) { - var inferMapping = selector(new PocoMappingDescriptor(documentType)); + var inferMapping = selector(new ClrTypeMappingDescriptor(documentType)); if (!inferMapping.IndexName.IsNullOrEmpty()) this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName); @@ -273,7 +273,7 @@ public TConnectionSettings InferMappingFor(Type documentType, Func /// The type of the POCO you wish to configure /// describe the POCO configuration - public TConnectionSettings InferMappings(IEnumerable typeMappings) + public TConnectionSettings InferMappings(IEnumerable typeMappings) { if (typeMappings == null) return (TConnectionSettings) this; foreach (var inferMapping in typeMappings) diff --git a/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs b/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs deleted file mode 100644 index a442901f34f..00000000000 --- a/src/Nest/CommonAbstractions/ConnectionSettings/PocoMapping.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; - -namespace Nest -{ - public interface IPocoMapping - { - Type ClrType { get; } - - /// - /// When specified dictates the default Elasticsearch index name for - /// - string IndexName { get; set; } - - /// - /// When specified dictates the default Elasticsearch type name for - /// - string TypeName { get; set; } - - /// - /// When specified dictates the relation name for to resolve to. - /// - string RelationName { get; set; } - - } - - public interface IPocoMapping : IPocoMapping where T : class - { - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - Expression> IdProperty { get; set; } - - /// - /// When specified allows you to ignore or rename certain properties of clr type - /// - IList> Properties { get; set; } - } - - public class PocoMapping : IPocoMapping - { - - public Type ClrType { get; } - public PocoMapping(Type type) => ClrType = type; - - /// - /// When specified dictates the default Elasticsearch index name for - /// - public string IndexName { get; set; } - - /// - /// When specified dictates the default Elasticsearch type name for - /// - public string TypeName { get; set; } - - /// - /// When specified dictates the relation name for to resolve to. - /// - public string RelationName { get; set; } - - } - public class PocoMapping : PocoMapping, IPocoMapping where T : class - { - public PocoMapping() : base(typeof(T)) { } - - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - public Expression> IdProperty { get; set; } - - /// - /// When specified allows you to ignore or rename certain properties of clr type - /// - public IList> Properties { get; set; } - - } - - public class PocoMappingDescriptor : DescriptorBase,IPocoMapping> , IPocoMapping - where T : class - { - Type IPocoMapping.ClrType { get; } = typeof (T); - string IPocoMapping.IndexName { get; set; } - string IPocoMapping.TypeName { get; set; } - string IPocoMapping.RelationName { get; set; } - Expression> IPocoMapping.IdProperty { get; set; } - IList> IPocoMapping.Properties { get; set; } = new List>(); - - /// - /// When specified dictates the default Elasticsearch index name for - /// - public PocoMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); - - /// - /// When specified dictates the default Elasticsearch type name for - /// - public PocoMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); - - /// - /// When specified dictates the relation name for to resolve to. - /// - public PocoMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); - - /// - /// Allows you to set a default Id property on that NEST will evaluate - /// - public PocoMappingDescriptor IdProperty(Expression> property) => Assign(a => a.IdProperty = property); - - /// - /// When specified allows you to ignore on clr type - /// - public PocoMappingDescriptor Ignore(Expression> property) => - Assign(a => a.Properties.Add(new IgnorePropertyMapping(property))); - - /// - /// When specified allows you to rename on clr type - /// - public PocoMappingDescriptor Rename(Expression> property, string newName) => - Assign(a => a.Properties.Add(new RenamePropertyMapping(property, newName))); - - } - - public class PocoMappingDescriptor : DescriptorBase , IPocoMapping - { - private readonly Type _type; - Type IPocoMapping.ClrType => _type; - string IPocoMapping.IndexName { get; set; } - string IPocoMapping.TypeName { get; set; } - string IPocoMapping.RelationName { get; set; } - public PocoMappingDescriptor(Type type) => _type = type; - - /// - /// When specified dictates the default Elasticsearch index name for - /// - public PocoMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); - - /// - /// When specified dictates the default Elasticsearch type name for - /// - public PocoMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); - - /// - /// When specified dictates the relation name for to resolve to. - /// - public PocoMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); - } - - public interface IPocoPropertyMapping where T : class - { - Expression> Property { get; set; } - bool Ignore { get; set; } - string NewName { get; set; } - IPropertyMapping ToPropertyMapping(); - } - - public abstract class PocoPropertyMappingBase : IPocoPropertyMapping - where T : class - { - protected IPocoPropertyMapping Self => this; - - Expression> IPocoPropertyMapping.Property { get; set; } - - bool IPocoPropertyMapping.Ignore { get; set; } - - string IPocoPropertyMapping.NewName { get; set; } - - protected PocoPropertyMappingBase(Expression> property) - { - Self.Property = property; - } - - IPropertyMapping IPocoPropertyMapping.ToPropertyMapping() => Self.Ignore ? PropertyMapping.Ignored : new PropertyMapping {Name = Self.NewName}; - } - - public class IgnorePropertyMapping : PocoPropertyMappingBase where T : class - { - public IgnorePropertyMapping(Expression> property) : base(property) - { - Self.Ignore = true; - } - } - - public class RenamePropertyMapping : PocoPropertyMappingBase where T : class - { - public RenamePropertyMapping(Expression> property, string newName) : base(property) - { - newName.ThrowIfNull(nameof(newName)); - Self.NewName = newName; - } - } - - - -} diff --git a/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs b/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs index a2b2b772283..a6bbc5a01af 100644 --- a/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs +++ b/src/Nest/CommonAbstractions/SerializationBehavior/PropertyMapping.cs @@ -4,20 +4,20 @@ namespace Nest { - /// This class allows a serializer to report back on a properties behavior + /// Determines how a POCO property maps to the property on a JSON object when serialized public interface IPropertyMapping { - /// Override the json property name of a type + /// Override the property name serialized to JSON for this property string Name { get; set; } /// /// Ignore this property completely - ///
- When mapping automatically using AutoMap()
- ///
- When Indexing this type do not serialize whatever this value hold
+ /// - When mapping automatically using + /// - When Indexing this type do not serialize this property and its value ///
bool Ignore { get; set; } } - /// This class allows a serializer to report back on a properties behavior + /// public class PropertyMapping : IPropertyMapping { public static PropertyMapping Ignored = new PropertyMapping { Ignore = true }; @@ -29,15 +29,23 @@ public class PropertyMapping : IPropertyMapping public bool Ignore { get; set; } } + /// + /// Provides mappings for POCO properties + /// public interface IPropertyMappingProvider { + /// + /// Creates an for a + /// IPropertyMapping CreatePropertyMapping(MemberInfo memberInfo); } + /// public class PropertyMappingProvider : IPropertyMappingProvider { protected readonly ConcurrentDictionary Properties = new ConcurrentDictionary(); + /// public virtual IPropertyMapping CreatePropertyMapping(MemberInfo memberInfo) { var memberInfoString = $"{memberInfo.DeclaringType?.FullName}.{memberInfo.Name}"; diff --git a/src/Nest/Mapping/PropertyMapping.cs b/src/Nest/Mapping/PropertyMapping.cs index 0c72bfa6bf4..c3d81e05d75 100644 --- a/src/Nest/Mapping/PropertyMapping.cs +++ b/src/Nest/Mapping/PropertyMapping.cs @@ -8,20 +8,20 @@ namespace Nest public class PropertyMappingDescriptor : DescriptorBase, IDescriptor> where TDocument : class { - internal IList> Mappings { get; } = new List>(); + internal IList> Mappings { get; } = new List>(); public PropertyMappingDescriptor Rename(Expression> property, string field) { property.ThrowIfNull(nameof(property)); field.ThrowIfNullOrEmpty(nameof(field)); - this.Mappings.Add(new RenamePropertyMapping(property, field)); + this.Mappings.Add(new RenameClrPropertyMapping(property, field)); return this; } public PropertyMappingDescriptor Ignore(Expression> property) { property.ThrowIfNull(nameof(property)); - this.Mappings.Add(new IgnorePropertyMapping(property)); + this.Mappings.Add(new IgnoreClrPropertyMapping(property)); return this; } }