Skip to content

Commit ee62df7

Browse files
committed
Change DynamicMapping to Union<bool, DynamicMapping>
1 parent 3f8b753 commit ee62df7

File tree

8 files changed

+18
-90
lines changed

8 files changed

+18
-90
lines changed

src/Nest/Indices/MappingManagement/PutMapping/PutMappingRequest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal PutMappingRequest() { }
2525
/// <inheritdoc/>
2626
public IDynamicTemplateContainer DynamicTemplates { get; set; }
2727
/// <inheritdoc/>
28-
public DynamicMapping? Dynamic { get; set; }
28+
public Union<bool, DynamicMapping> Dynamic { get; set; }
2929
/// <inheritdoc/>
3030
public string Analyzer { get; set; }
3131
/// <inheritdoc/>
@@ -63,7 +63,7 @@ public PutMappingRequest() : this(typeof(T), typeof(T)) { }
6363
/// <inheritdoc/>
6464
public IDynamicTemplateContainer DynamicTemplates { get; set; }
6565
/// <inheritdoc/>
66-
public DynamicMapping? Dynamic { get; set; }
66+
public Union<bool, DynamicMapping> Dynamic { get; set; }
6767
/// <inheritdoc/>
6868
public string Analyzer { get; set; }
6969
/// <inheritdoc/>
@@ -102,7 +102,7 @@ public PutMappingDescriptor(IndexName index, TypeName type) : base(r=>r.Required
102102
string ITypeMapping.Analyzer { get; set; }
103103
string ITypeMapping.SearchAnalyzer { get; set; }
104104
IDynamicTemplateContainer ITypeMapping.DynamicTemplates { get; set; }
105-
DynamicMapping? ITypeMapping.Dynamic { get; set; }
105+
Union<bool, DynamicMapping> ITypeMapping.Dynamic { get; set; }
106106
IFieldNamesField ITypeMapping.FieldNamesField { get; set; }
107107
IIndexField ITypeMapping.IndexField { get; set; }
108108
FluentDictionary<string, object> ITypeMapping.Meta { get; set; }
@@ -126,10 +126,7 @@ public PutMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int maxR
126126
public PutMappingDescriptor<T> AutoMap(int maxRecursion) => AutoMap(null, maxRecursion);
127127

128128
/// <inheritdoc/>
129-
public PutMappingDescriptor<T> Dynamic(DynamicMapping dynamic) => Assign(a => a.Dynamic = dynamic);
130-
131-
/// <inheritdoc/>
132-
public PutMappingDescriptor<T> Dynamic(bool dynamic = true) => this.Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
129+
public PutMappingDescriptor<T> Dynamic(Union<bool, DynamicMapping> dynamic) => Assign(a => a.Dynamic = dynamic);
133130

134131
/// <inheritdoc/>
135132
public PutMappingDescriptor<T> Parent(TypeName parentType) => Assign(a => a.ParentField = new ParentField { Type = parentType });

src/Nest/Mapping/DynamicMapping.cs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,17 @@
11
using System;
22
using System.Runtime.Serialization;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
45

56
namespace Nest
67
{
7-
// TODO 5.x Make this a Union type
8-
/// <summary>
9-
/// Controls how elasticsearch handles dynamic mapping changes when a new document present new fields
10-
/// </summary>
11-
[JsonConverter(typeof(DynamicMappingJsonConverter))]
8+
[JsonConverter(typeof(StringEnumConverter))]
129
public enum DynamicMapping
1310
{
14-
/// <summary>
15-
/// Default value, allows unmapped fields to be cause a mapping update
16-
/// </summary>
17-
[EnumMember(Value = "allow")]
18-
Allow,
19-
/// <summary>
20-
/// New unmapped fields will be silently ignored
21-
/// </summary>
22-
[EnumMember(Value = "ignore")]
23-
Ignore,
2411
/// <summary>
2512
/// If new unmapped fields are passed, the whole document WON'T be added/updated
2613
/// </summary>
2714
[EnumMember(Value = "strict")]
2815
Strict
2916
}
30-
31-
internal class DynamicMappingJsonConverter : JsonConverter
32-
{
33-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
34-
{
35-
var v = value as DynamicMapping?;
36-
if (!v.HasValue)
37-
{
38-
writer.WriteValue(true);
39-
return;
40-
}
41-
switch (v.Value)
42-
{
43-
case DynamicMapping.Strict:
44-
writer.WriteValue("strict");
45-
break;
46-
case DynamicMapping.Ignore:
47-
writer.WriteValue(false);
48-
break;
49-
default:
50-
writer.WriteValue(true);
51-
break;
52-
}
53-
}
54-
55-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
56-
{
57-
var v = reader.Value;
58-
if (v == null)
59-
return null;
60-
61-
var sv = v.ToString().ToLowerInvariant();
62-
switch (sv)
63-
{
64-
case "false":
65-
return DynamicMapping.Ignore;
66-
case "strict":
67-
return DynamicMapping.Strict;
68-
default:
69-
return DynamicMapping.Allow;
70-
}
71-
}
72-
73-
public override bool CanConvert(Type objectType) => objectType == typeof(DynamicMapping?);
74-
}
7517
}

src/Nest/Mapping/TypeMapping.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface ITypeMapping
5454
IDynamicTemplateContainer DynamicTemplates { get; set; }
5555

5656
[JsonProperty("dynamic")]
57-
DynamicMapping? Dynamic { get; set; }
57+
Union<bool, DynamicMapping> Dynamic { get; set; }
5858

5959
[JsonProperty("properties", TypeNameHandling = TypeNameHandling.None)]
6060
IProperties Properties { get; set; }
@@ -69,7 +69,7 @@ public class TypeMapping : ITypeMapping
6969
/// <inheritdoc/>
7070
public bool? DateDetection { get; set; }
7171
/// <inheritdoc/>
72-
public DynamicMapping? Dynamic { get; set; }
72+
public Union<bool, DynamicMapping> Dynamic { get; set; }
7373
/// <inheritdoc/>
7474
public IEnumerable<string> DynamicDateFormats { get; set; }
7575
/// <inheritdoc/>
@@ -104,7 +104,7 @@ public class TypeMappingDescriptor<T> : DescriptorBase<TypeMappingDescriptor<T>,
104104
IAllField ITypeMapping.AllField { get; set; }
105105
string ITypeMapping.Analyzer { get; set; }
106106
bool? ITypeMapping.DateDetection { get; set; }
107-
DynamicMapping? ITypeMapping.Dynamic { get; set; }
107+
Union<bool, DynamicMapping> ITypeMapping.Dynamic { get; set; }
108108
IEnumerable<string> ITypeMapping.DynamicDateFormats { get; set; }
109109
IDynamicTemplateContainer ITypeMapping.DynamicTemplates { get; set; }
110110
IFieldNamesField ITypeMapping.FieldNamesField { get; set; }
@@ -131,10 +131,7 @@ public TypeMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int max
131131
public TypeMappingDescriptor<T> AutoMap(int maxRecursion) => AutoMap(null, maxRecursion);
132132

133133
/// <inheritdoc/>
134-
public TypeMappingDescriptor<T> Dynamic(DynamicMapping dynamic) => Assign(a => a.Dynamic = dynamic);
135-
136-
/// <inheritdoc/>
137-
public TypeMappingDescriptor<T> Dynamic(bool dynamic = true) => this.Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
134+
public TypeMappingDescriptor<T> Dynamic(Union<bool, DynamicMapping> dynamic) => Assign(a => a.Dynamic = dynamic);
138135

139136
/// <inheritdoc/>
140137
public TypeMappingDescriptor<T> Parent(TypeName parentType) => Assign(a => a.ParentField = new ParentField { Type = parentType });

src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ public class ObjectAttribute : ElasticsearchCorePropertyAttributeBase, IObjectPr
66
{
77
IObjectProperty Self => this;
88

9-
DynamicMapping? IObjectProperty.Dynamic { get; set; }
9+
Union<bool, DynamicMapping> IObjectProperty.Dynamic { get; set; }
1010
bool? IObjectProperty.Enabled { get; set; }
1111
bool? IObjectProperty.IncludeInAll { get; set; }
1212
IProperties IObjectProperty.Properties { get; set; }
1313

14-
public DynamicMapping Dynamic { get { return Self.Dynamic.GetValueOrDefault(); } set { Self.Dynamic = value; } }
1514
public bool Enabled { get { return Self.Enabled.GetValueOrDefault(); } set { Self.Enabled = value; } }
1615
public bool IncludeInAll { get { return Self.IncludeInAll.GetValueOrDefault(); } set { Self.IncludeInAll = value; } }
1716

src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Nest
88
public interface IObjectProperty : ICoreProperty
99
{
1010
[JsonProperty("dynamic")]
11-
DynamicMapping? Dynamic { get; set; }
11+
Union<bool, DynamicMapping> Dynamic { get; set; }
1212

1313
[JsonProperty("enabled")]
1414
bool? Enabled { get; set; }
@@ -26,7 +26,7 @@ public ObjectProperty() : base("object") { }
2626

2727
protected ObjectProperty(string type) : base(type) { }
2828

29-
public DynamicMapping? Dynamic { get; set; }
29+
public Union<bool, DynamicMapping> Dynamic { get; set; }
3030
public bool? Enabled { get; set; }
3131
public bool? IncludeInAll { get; set; }
3232
public IProperties Properties { get; set; }
@@ -48,7 +48,7 @@ public abstract class ObjectPropertyDescriptorBase<TDescriptor, TInterface, TPar
4848
{
4949
internal TypeName _TypeName { get; set; }
5050

51-
DynamicMapping? IObjectProperty.Dynamic { get; set; }
51+
Union<bool, DynamicMapping> IObjectProperty.Dynamic { get; set; }
5252
bool? IObjectProperty.Enabled { get; set; }
5353
bool? IObjectProperty.IncludeInAll { get; set; }
5454
IProperties IObjectProperty.Properties { get; set; }
@@ -60,12 +60,9 @@ protected ObjectPropertyDescriptorBase(string type) : base(type)
6060
_TypeName = TypeName.Create<TChild>();
6161
}
6262

63-
public TDescriptor Dynamic(DynamicMapping dynamic) =>
63+
public TDescriptor Dynamic(Union<bool, DynamicMapping> dynamic) =>
6464
Assign(a => a.Dynamic = dynamic);
6565

66-
public TDescriptor Dynamic(bool dynamic = true) =>
67-
Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
68-
6966
public TDescriptor Enabled(bool enabled = true) =>
7067
Assign(a => a.Enabled = enabled);
7168

src/Tests/Mapping/Types/Complex/Nested/NestedAttributeTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class InnerObject
1313
[Nested(
1414
IncludeInParent = true,
1515
IncludeInRoot = false,
16-
Dynamic = DynamicMapping.Strict,
1716
Enabled = true,
1817
IncludeInAll = true)]
1918
public InnerObject Full { get; set; }
@@ -33,7 +32,6 @@ public class NestedAttributeTests : AttributeTestsBase<NestedTest>
3332
type = "nested",
3433
include_in_parent = true,
3534
include_in_root = false,
36-
dynamic = "strict",
3735
enabled = true,
3836
include_in_all = true,
3937
properties = new

src/Tests/Mapping/Types/Complex/Object/ObjectAttributeTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class InnerObject
1111
}
1212

1313
[Object(
14-
Dynamic = DynamicMapping.Strict,
1514
Enabled = true,
1615
IncludeInAll = true)]
1716
public InnerObject Full { get; set; }
@@ -29,7 +28,6 @@ public class ObjectAttributeTests : AttributeTestsBase<ObjectTest>
2928
full = new
3029
{
3130
type = "object",
32-
dynamic = "strict",
3331
enabled = true,
3432
include_in_all = true,
3533
properties = new

src/Tests/Mapping/Types/Complex/Object/ObjectPropertyTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ObjectPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(
1818
leadDeveloper = new
1919
{
2020
type = "object",
21-
dynamic = "strict",
21+
dynamic = true,
2222
enabled = true,
2323
include_in_all = true,
2424
properties = new
@@ -35,7 +35,7 @@ public ObjectPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(
3535
protected override Func<PropertiesDescriptor<Project>, IPromise<IProperties>> FluentProperties => f => f
3636
.Object<Developer>(n => n
3737
.Name(p => p.LeadDeveloper)
38-
.Dynamic(DynamicMapping.Strict)
38+
.Dynamic(true)
3939
.Enabled()
4040
.IncludeInAll()
4141
.Properties(pps => pps
@@ -49,7 +49,7 @@ public ObjectPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(
4949
{
5050
{ "leadDeveloper", new ObjectProperty
5151
{
52-
Dynamic = DynamicMapping.Strict,
52+
Dynamic = true,
5353
Enabled = true,
5454
IncludeInAll = true,
5555
Properties = new Properties

0 commit comments

Comments
 (0)