Skip to content

Commit c1e062b

Browse files
committed
Add Visit methods for other supported IProperty types
Add NoopMappingVisitor to make it easier for consumers to write their own IMappingVisitor implementation
1 parent 8ad3032 commit c1e062b

File tree

7 files changed

+230
-6
lines changed

7 files changed

+230
-6
lines changed

src/Benchmarking/project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7811,7 +7811,7 @@
78117811
"runtime.win7.System.Net.Requests/4.0.11-beta-23516": {
78127812
"type": "package",
78137813
"serviceable": true,
7814-
"sha512": "HI99nCEekL4SNvkLmpqkOE0PuEF5B6xyDcnJesdjo06BrGYH3QCvqJt2VmzBVe6hDSo6FnGOlhMvLdCUpDXiXA==",
7814+
"sha512": "mqWBQUhXhzkiwb+zVUuKg+wswJUsnQtZkFtz6eISw8vWNXA9i2jkzYjU3pjjIVmtdopnhle9YaS4a/w4OuWGLw==",
78157815
"files": [
78167816
"ref/dotnet/_._",
78177817
"runtime.win7.System.Net.Requests.4.0.11-beta-23516.nupkg",

src/Elasticsearch.Net/project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@
17391739
"runtime.win7.System.Net.Requests/4.0.11-beta-23516": {
17401740
"type": "package",
17411741
"serviceable": true,
1742-
"sha512": "HI99nCEekL4SNvkLmpqkOE0PuEF5B6xyDcnJesdjo06BrGYH3QCvqJt2VmzBVe6hDSo6FnGOlhMvLdCUpDXiXA==",
1742+
"sha512": "mqWBQUhXhzkiwb+zVUuKg+wswJUsnQtZkFtz6eISw8vWNXA9i2jkzYjU3pjjIVmtdopnhle9YaS4a/w4OuWGLw==",
17431743
"files": [
17441744
"ref/dotnet/_._",
17451745
"runtime.win7.System.Net.Requests.4.0.11-beta-23516.nupkg",

src/Nest/Mapping/Visitor/IMappingVisitor.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
namespace Nest
1+
using System;
2+
3+
namespace Nest
24
{
35
public interface IMappingVisitor
46
{
57
int Depth { get; set; }
68
void Visit(TypeMapping mapping);
79
void Visit(StringProperty mapping);
10+
[Obsolete("Use Visit(NumberProperty mapping) for number mappings")]
811
void Visit(NumberType mapping);
912
void Visit(DateProperty mapping);
1013
void Visit(BooleanProperty mapping);
@@ -16,5 +19,78 @@ public interface IMappingVisitor
1619
void Visit(GeoShapeProperty mapping);
1720
void Visit(AttachmentProperty mapping);
1821
void Visit(NumberProperty mapping);
22+
void Visit(CompletionProperty mapping);
23+
void Visit(Murmur3HashProperty mapping);
24+
void Visit(TokenCountProperty mapping);
25+
}
26+
27+
public class NoopMappingVisitor : IMappingVisitor
28+
{
29+
public virtual int Depth { get; set; }
30+
31+
public virtual void Visit(TypeMapping mapping)
32+
{
33+
}
34+
35+
public virtual void Visit(StringProperty mapping)
36+
{
37+
}
38+
39+
[Obsolete("Use Visit(NumberProperty mapping) for number mappings")]
40+
public virtual void Visit(NumberType mapping)
41+
{
42+
}
43+
44+
public virtual void Visit(DateProperty mapping)
45+
{
46+
}
47+
48+
public virtual void Visit(BooleanProperty mapping)
49+
{
50+
}
51+
52+
public virtual void Visit(BinaryProperty mapping)
53+
{
54+
}
55+
56+
public virtual void Visit(ObjectProperty mapping)
57+
{
58+
}
59+
60+
public virtual void Visit(NestedProperty mapping)
61+
{
62+
}
63+
64+
public virtual void Visit(IpProperty mapping)
65+
{
66+
}
67+
68+
public virtual void Visit(GeoPointProperty mapping)
69+
{
70+
}
71+
72+
public virtual void Visit(GeoShapeProperty mapping)
73+
{
74+
}
75+
76+
public virtual void Visit(AttachmentProperty mapping)
77+
{
78+
}
79+
80+
public virtual void Visit(NumberProperty mapping)
81+
{
82+
}
83+
84+
public virtual void Visit(CompletionProperty mapping)
85+
{
86+
}
87+
88+
public virtual void Visit(Murmur3HashProperty mapping)
89+
{
90+
}
91+
92+
public virtual void Visit(TokenCountProperty mapping)
93+
{
94+
}
1995
}
2096
}

src/Nest/Mapping/Visitor/MappingWalker.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,45 @@ public void Accept(IProperties properties)
9090
var i = field as IpProperty;
9191
if (i == null) continue;
9292
this._visitor.Visit(i);
93+
this.Accept(i.Fields);
9394
break;
9495
case "geo_point":
9596
var gp = field as GeoPointProperty;
9697
if (gp == null) continue;
9798
this._visitor.Visit(gp);
99+
this.Accept(gp.Fields);
98100
break;
99101
case "geo_shape":
100102
var gs = field as GeoShapeProperty;
101103
if (gs == null) continue;
102104
this._visitor.Visit(gs);
105+
this.Accept(gs.Fields);
103106
break;
104107
case "attachment":
105108
var a = field as AttachmentProperty;
106109
if (a == null) continue;
107110
this._visitor.Visit(a);
111+
this.Accept(a.Fields);
112+
break;
113+
case "completion":
114+
var c = field as CompletionProperty;
115+
if (c == null) continue;
116+
this._visitor.Visit(c);
117+
this.Accept(c.Fields);
118+
break;
119+
case "murmur3":
120+
var mm = field as Murmur3HashProperty;
121+
if (mm == null) continue;
122+
this._visitor.Visit(mm);
123+
this.Accept(mm.Fields);
124+
break;
125+
case "token_count":
126+
var tc = field as TokenCountProperty;
127+
if (tc == null) continue;
128+
this._visitor.Visit(tc);
129+
this.Accept(tc.Fields);
108130
break;
109131
}
110-
111132
}
112133
}
113134
}

src/Nest/project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@
20592059
"runtime.win7.System.Net.Requests/4.0.11-beta-23516": {
20602060
"type": "package",
20612061
"serviceable": true,
2062-
"sha512": "HI99nCEekL4SNvkLmpqkOE0PuEF5B6xyDcnJesdjo06BrGYH3QCvqJt2VmzBVe6hDSo6FnGOlhMvLdCUpDXiXA==",
2062+
"sha512": "mqWBQUhXhzkiwb+zVUuKg+wswJUsnQtZkFtz6eISw8vWNXA9i2jkzYjU3pjjIVmtdopnhle9YaS4a/w4OuWGLw==",
20632063
"files": [
20642064
"ref/dotnet/_._",
20652065
"runtime.win7.System.Net.Requests.4.0.11-beta-23516.nupkg",

src/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using Elasticsearch.Net;
4+
using FluentAssertions;
35
using Nest;
46
using Tests.Framework;
57
using Tests.Framework.Integration;
@@ -33,5 +35,130 @@ protected override LazyResponses ClientUsage() => Calls(
3335
{
3436
IgnoreUnavailable = true
3537
};
38+
39+
protected override void ExpectResponse(IGetMappingResponse response)
40+
{
41+
response.IsValid.Should().BeTrue();
42+
43+
var visitor = new TestVisitor();
44+
response.Accept(visitor);
45+
46+
visitor.CountsShouldContainKeyAndCountBe("type", 2);
47+
visitor.CountsShouldContainKeyAndCountBe("object", 3);
48+
visitor.CountsShouldContainKeyAndCountBe("date", 5);
49+
visitor.CountsShouldContainKeyAndCountBe("string", 18);
50+
visitor.CountsShouldContainKeyAndCountBe("ip", 2);
51+
visitor.CountsShouldContainKeyAndCountBe("number", 3);
52+
visitor.CountsShouldContainKeyAndCountBe("geo_point", 3);
53+
visitor.CountsShouldContainKeyAndCountBe("completion", 3);
54+
visitor.CountsShouldContainKeyAndCountBe("nested", 2);
55+
}
56+
}
57+
58+
internal class TestVisitor : IMappingVisitor
59+
{
60+
public TestVisitor()
61+
{
62+
Counts = new Dictionary<string, int>();
63+
}
64+
65+
public int Depth { get; set; }
66+
67+
public Dictionary<string, int> Counts { get; }
68+
69+
private void Increment(string key)
70+
{
71+
if (!Counts.ContainsKey(key))
72+
{
73+
Counts.Add(key, 1);
74+
}
75+
Counts[key] += 1;
76+
}
77+
78+
public void CountsShouldContainKeyAndCountBe(string key, int count)
79+
{
80+
this.Counts.ContainsKey(key).Should().BeTrue();
81+
this.Counts[key].Should().Be(count);
82+
}
83+
84+
public void Visit(DateProperty mapping)
85+
{
86+
Increment("date");
87+
}
88+
89+
public void Visit(BinaryProperty mapping)
90+
{
91+
Increment("binary");
92+
}
93+
94+
public void Visit(NestedProperty mapping)
95+
{
96+
Increment("nested");
97+
}
98+
99+
public void Visit(GeoPointProperty mapping)
100+
{
101+
Increment("geo_point");
102+
}
103+
104+
public void Visit(AttachmentProperty mapping)
105+
{
106+
Increment("attachment");
107+
}
108+
109+
public void Visit(CompletionProperty mapping)
110+
{
111+
Increment("completion");
112+
}
113+
114+
public void Visit(TokenCountProperty mapping)
115+
{
116+
Increment("token_count");
117+
}
118+
119+
public void Visit(Murmur3HashProperty mapping)
120+
{
121+
Increment("murmur3");
122+
}
123+
124+
public void Visit(NumberProperty mapping)
125+
{
126+
Increment("number");
127+
}
128+
129+
public void Visit(GeoShapeProperty mapping)
130+
{
131+
Increment("geo_shape");
132+
}
133+
134+
public void Visit(IpProperty mapping)
135+
{
136+
Increment("ip");
137+
}
138+
139+
public void Visit(ObjectProperty mapping)
140+
{
141+
Increment("object");
142+
}
143+
144+
public void Visit(BooleanProperty mapping)
145+
{
146+
Increment("boolean");
147+
}
148+
149+
public void Visit(NumberType mapping)
150+
{
151+
throw new InvalidOperationException("NumberType should never be called");
152+
}
153+
154+
public void Visit(StringProperty mapping)
155+
{
156+
Increment("string");
157+
}
158+
159+
public void Visit(TypeMapping mapping)
160+
{
161+
Increment("type");
162+
}
36163
}
37164
}

src/Tests/project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6406,7 +6406,7 @@
64066406
"runtime.win7.System.Net.Requests/4.0.11-beta-23516": {
64076407
"type": "package",
64086408
"serviceable": true,
6409-
"sha512": "HI99nCEekL4SNvkLmpqkOE0PuEF5B6xyDcnJesdjo06BrGYH3QCvqJt2VmzBVe6hDSo6FnGOlhMvLdCUpDXiXA==",
6409+
"sha512": "mqWBQUhXhzkiwb+zVUuKg+wswJUsnQtZkFtz6eISw8vWNXA9i2jkzYjU3pjjIVmtdopnhle9YaS4a/w4OuWGLw==",
64106410
"files": [
64116411
"ref/dotnet/_._",
64126412
"runtime.win7.System.Net.Requests.4.0.11-beta-23516.nupkg",

0 commit comments

Comments
 (0)