|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | + |
| 5 | +namespace Nest |
| 6 | +{ |
| 7 | + public interface IClrTypeMapping |
| 8 | + { |
| 9 | + /// <summary> |
| 10 | + /// The CLR type the mapping relates to |
| 11 | + /// </summary> |
| 12 | + Type ClrType { get; } |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// The default Elasticsearch index name for <see cref="ClrType"/> |
| 16 | + /// </summary> |
| 17 | + string IndexName { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// The default Elasticsearch type name for <see cref="ClrType"/> |
| 21 | + /// </summary> |
| 22 | + string TypeName { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The relation name for <see cref="ClrType"/> to resolve to. |
| 26 | + /// </summary> |
| 27 | + string RelationName { get; set; } |
| 28 | + } |
| 29 | + |
| 30 | + public interface IClrTypeMapping<TDocument> : IClrTypeMapping where TDocument : class |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate |
| 34 | + /// </summary> |
| 35 | + Expression<Func<TDocument, object>> IdProperty { get; set; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Ignore or rename certain properties of CLR type <typeparamref name="TDocument" /> |
| 39 | + /// </summary> |
| 40 | + IList<IClrPropertyMapping<TDocument>> Properties { get; set; } |
| 41 | + } |
| 42 | + |
| 43 | + public class ClrTypeMapping : IClrTypeMapping |
| 44 | + { |
| 45 | + /// <summary> |
| 46 | + /// Initializes a new instance of <see cref="ClrTypeMapping"/> |
| 47 | + /// </summary> |
| 48 | + public ClrTypeMapping(Type type) => ClrType = type; |
| 49 | + |
| 50 | + /// <inheritdoc /> |
| 51 | + public Type ClrType { get; } |
| 52 | + |
| 53 | + /// <inheritdoc /> |
| 54 | + public string IndexName { get; set; } |
| 55 | + |
| 56 | + /// <inheritdoc /> |
| 57 | + public string TypeName { get; set; } |
| 58 | + |
| 59 | + /// <inheritdoc /> |
| 60 | + public string RelationName { get; set; } |
| 61 | + |
| 62 | + } |
| 63 | + public class ClrTypeMapping<TDocument> : ClrTypeMapping, IClrTypeMapping<TDocument> where TDocument : class |
| 64 | + { |
| 65 | + public ClrTypeMapping() : base(typeof(TDocument)) { } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public Expression<Func<TDocument, object>> IdProperty { get; set; } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + public IList<IClrPropertyMapping<TDocument>> Properties { get; set; } |
| 72 | + } |
| 73 | + |
| 74 | + public class ClrTypeMappingDescriptor : DescriptorBase<ClrTypeMappingDescriptor,IClrTypeMapping> , IClrTypeMapping |
| 75 | + { |
| 76 | + private readonly Type _type; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Instantiates a new instance of <see cref="ClrTypeMappingDescriptor"/> |
| 80 | + /// </summary> |
| 81 | + /// <param name="type">The CLR type to map</param> |
| 82 | + public ClrTypeMappingDescriptor(Type type) => _type = type; |
| 83 | + |
| 84 | + Type IClrTypeMapping.ClrType => _type; |
| 85 | + string IClrTypeMapping.IndexName { get; set; } |
| 86 | + string IClrTypeMapping.TypeName { get; set; } |
| 87 | + string IClrTypeMapping.RelationName { get; set; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// The default Elasticsearch index name for the CLR type |
| 91 | + /// </summary> |
| 92 | + public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// The default Elasticsearch type name for the CLR type |
| 96 | + /// </summary> |
| 97 | + public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// The relation name for the CLR type to resolve to. |
| 101 | + /// </summary> |
| 102 | + public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); |
| 103 | + } |
| 104 | + |
| 105 | + public class ClrTypeMappingDescriptor<TDocument> |
| 106 | + : DescriptorBase<ClrTypeMappingDescriptor<TDocument>,IClrTypeMapping<TDocument>>, IClrTypeMapping<TDocument> |
| 107 | + where TDocument : class |
| 108 | + { |
| 109 | + Type IClrTypeMapping.ClrType { get; } = typeof (TDocument); |
| 110 | + string IClrTypeMapping.IndexName { get; set; } |
| 111 | + string IClrTypeMapping.TypeName { get; set; } |
| 112 | + string IClrTypeMapping.RelationName { get; set; } |
| 113 | + Expression<Func<TDocument, object>> IClrTypeMapping<TDocument>.IdProperty { get; set; } |
| 114 | + IList<IClrPropertyMapping<TDocument>> IClrTypeMapping<TDocument>.Properties { get; set; } = new List<IClrPropertyMapping<TDocument>>(); |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// The default Elasticsearch index name for <typeparamref name="TDocument"/> |
| 118 | + /// </summary> |
| 119 | + public ClrTypeMappingDescriptor<TDocument> IndexName(string indexName) => Assign(a => a.IndexName = indexName); |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// The default Elasticsearch type name for <typeparamref name="TDocument" /> |
| 123 | + /// </summary> |
| 124 | + public ClrTypeMappingDescriptor<TDocument> TypeName(string typeName) => Assign(a => a.TypeName = typeName); |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// The relation name for <typeparamref name="TDocument" /> to resolve to. |
| 128 | + /// </summary> |
| 129 | + public ClrTypeMappingDescriptor<TDocument> RelationName(string relationName) => Assign(a => a.RelationName = relationName); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate |
| 133 | + /// </summary> |
| 134 | + public ClrTypeMappingDescriptor<TDocument> IdProperty(Expression<Func<TDocument, object>> property) => Assign(a => a.IdProperty = property); |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Ignore <paramref name="property" /> on CLR type <typeparamref name="TDocument" /> |
| 138 | + /// </summary> |
| 139 | + public ClrTypeMappingDescriptor<TDocument> Ignore(Expression<Func<TDocument, object>> property) => |
| 140 | + Assign(a => a.Properties.Add(new IgnoreClrPropertyMapping<TDocument>(property))); |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Rename <paramref name="property" /> on CLR type <typeparamref name="TDocument" /> |
| 144 | + /// </summary> |
| 145 | + public ClrTypeMappingDescriptor<TDocument> Rename(Expression<Func<TDocument, object>> property, string newName) => |
| 146 | + Assign(a => a.Properties.Add(new RenameClrPropertyMapping<TDocument>(property, newName))); |
| 147 | + |
| 148 | + } |
| 149 | +} |
0 commit comments