Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c8cc85f

Browse files
committedOct 24, 2016
Minor revision on how LocalMetadata is accessed within the fluent API, added more tests and fixed build
1 parent 39a822c commit c8cc85f

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed
 

‎src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public abstract class ElasticsearchPropertyAttributeBase : Attribute, IProperty,
1515

1616
PropertyName IProperty.Name { get; set; }
1717
TypeName IProperty.Type { get; set; }
18+
IDictionary<string, object> IProperty.LocalMetadata { get; set; }
1819

1920
public string Name { get; set; }
2021
public bool Ignore { get; set; }
@@ -32,8 +33,6 @@ protected ElasticsearchPropertyAttributeBase(Type type)
3233
public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo)
3334
{
3435
return memberInfo.GetCustomAttribute<ElasticsearchPropertyAttributeBase>(true);
35-
}
36-
37-
IDictionary<string, object> IPropertyWithLocalMetadata.LocalMetadata { get; set; }
36+
}
3837
}
3938
}

‎src/Nest/Mapping/Types/PropertyDescriptorBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
1212
{
1313
PropertyName IProperty.Name { get; set; }
1414
TypeName IProperty.Type { get; set; }
15+
IDictionary<string, object> IProperty.LocalMetadata { get; set; }
1516

1617
protected PropertyDescriptorBase(string type) { Self.Type = type; }
1718

@@ -22,6 +23,7 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
2223
/// <summary>
2324
/// Local property metadata that will NOT be stored in Elasticsearch with the mappings
2425
/// </summary>
25-
public IDictionary<string, object> LocalMetadata { get; set; }
26+
public TDescriptor LocalMetadata(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
27+
Assign(a => a.LocalMetadata = selector?.Invoke(new FluentDictionary<string, object>()));
2628
}
2729
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Nest;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Tests.Framework;
8+
using Tests.Framework.MockData;
9+
10+
namespace Tests.Mapping.LocalMetadata
11+
{
12+
public class LocalMetdataUsageTests : UsageTestBase<ITypeMapping, TypeMappingDescriptor<Project>, TypeMapping>
13+
{
14+
// Ensure local metadata is never serialized
15+
protected override object ExpectJson => new
16+
{
17+
properties = new
18+
{
19+
numberOfCommits = new
20+
{
21+
type = "float"
22+
}
23+
}
24+
};
25+
26+
protected override Func<TypeMappingDescriptor<Project>, ITypeMapping> Fluent => f => f
27+
.Properties(ps => ps
28+
.Number(t => t
29+
.Name(p => p.NumberOfCommits)
30+
.LocalMetadata(m => m
31+
.Add("foo", "bar")
32+
)
33+
)
34+
);
35+
36+
37+
protected override TypeMapping Initializer => new TypeMapping
38+
{
39+
Properties = new Properties
40+
{
41+
{ "numberOfCommits", new NumberProperty { LocalMetadata = new Dictionary<string, object> { { "foo", "bar" } } } }
42+
}
43+
};
44+
}
45+
}

‎src/Tests/Mapping/Metafields/LocalMetadataTests.cs renamed to ‎src/Tests/Mapping/LocalMetadata/LocalMetadataVisitorTests.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
using Nest;
55
using Tests.Mapping.Types.Core.Text;
66
using Xunit;
7+
using Tests.Framework;
8+
using Tests.Framework.MockData;
9+
using Tests.Mapping.LocalMetadata.Extensions;
710

8-
namespace Tests.Mapping.Metafields
11+
namespace Tests.Mapping.LocalMetadata
912
{
10-
public class LocalMetadataTests
13+
public class LocalMetadataVisitorTests
1114
{
12-
[Fact]
13-
public void CanAssignAndAccessLocalMetadata()
15+
[U]
16+
public void CanAssignAndAccessLocalMetadataInitializer()
1417
{
1518
var descriptor = new TypeMappingDescriptor<TextTest>().Properties(p => p
1619
.Text(t => t
@@ -25,8 +28,30 @@ public void CanAssignAndAccessLocalMetadata()
2528

2629
visitor.MetadataCount.Should().Be(1);
2730
}
31+
32+
[U]
33+
public void CanAssignAndAccessLocalMetadataFluent()
34+
{
35+
var descriptor = new TypeMappingDescriptor<TextTest>().Properties(p => p
36+
.Text(t => t
37+
.Name(o => o.Full)
38+
.Norms()
39+
.LocalMetadata(m => m
40+
.Add("Test", "TestValue")
41+
)
42+
)) as ITypeMapping;
43+
44+
var visitor = new LocalMatadataVisitor();
45+
var walker = new MappingWalker(visitor);
46+
walker.Accept(descriptor.Properties);
47+
48+
visitor.MetadataCount.Should().Be(1);
49+
}
2850
}
51+
}
2952

53+
namespace Tests.Mapping.LocalMetadata.Extensions
54+
{
3055
public static class TestLocalMetadataMappingExtensions
3156
{
3257
public static TDescriptor AddTestLocalMetadata<TDescriptor>(this TDescriptor descriptor)

‎src/Tests/Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@
391391
<Compile Include="Ingest\PutPipeline\PutPipelineUrlTests.cs" />
392392
<Compile Include="Ingest\SimulatePipeline\SimulatePipelineApiTests.cs" />
393393
<Compile Include="Ingest\SimulatePipeline\SimulatePipelineUrlTests.cs" />
394-
<Compile Include="Mapping\Metafields\LocalMetadataTests.cs" />
394+
<Compile Include="Mapping\LocalMetadata\LocalMetadataUsageTests.cs" />
395+
<Compile Include="Mapping\LocalMetadata\LocalMetadataVisitorTests.cs" />
395396
<Compile Include="Mapping\Types\Core\Keyword\KeywordMappingTests.cs" />
396397
<Compile Include="Mapping\Types\Core\Percolator\PercolatorMappingTests.cs" />
397398
<Compile Include="Mapping\Types\Core\Text\TextMappingTests.cs" />

0 commit comments

Comments
 (0)
Please sign in to comment.