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 1c49649

Browse files
committedOct 24, 2016
allow implicit conversion from string[] to Types and Indices making it easier to use without forcing an explicit cast map on the users #2289 (#2292)
1 parent 70a0c9b commit 1c49649

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed
 

‎src/Nest/CommonAbstractions/Infer/Indices/Indices.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ public class ManyIndices
1616
{
1717
private readonly List<IndexName> _indices = new List<IndexName>();
1818
public IReadOnlyList<IndexName> Indices => _indices;
19-
internal ManyIndices(IEnumerable<IndexName> indices) { this._indices.AddRange(indices); }
19+
internal ManyIndices(IEnumerable<IndexName> indices)
20+
{
21+
indices.ThrowIfEmpty(nameof(indices));
22+
this._indices.AddRange(indices);
23+
}
24+
internal ManyIndices(IEnumerable<string> indices)
25+
{
26+
indices.ThrowIfEmpty(nameof(indices));
27+
this._indices.AddRange(indices.Select(s=>(IndexName)s));
28+
}
2029

2130
public ManyIndices And<T>()
2231
{
@@ -34,6 +43,9 @@ internal Indices(IEnumerable<IndexName> indices) : base(new ManyIndices(indices)
3443
public static ManyIndices Index(IEnumerable<IndexName> indices) => new ManyIndices(indices);
3544
public static ManyIndices Index(params IndexName[] indices) => new ManyIndices(indices);
3645

46+
public static ManyIndices Index(IEnumerable<string> indices) => new ManyIndices(indices);
47+
public static ManyIndices Index(params string[] indices) => new ManyIndices(indices);
48+
3749
public static Indices Parse(string indicesString)
3850
{
3951
if (indicesString.IsNullOrEmpty()) throw new Exception("can not parse an empty string to Indices");
@@ -44,6 +56,7 @@ public static Indices Parse(string indicesString)
4456

4557
public static implicit operator Indices(string indicesString) => Parse(indicesString);
4658
public static implicit operator Indices(ManyIndices many) => new Indices(many);
59+
public static implicit operator Indices(string[] many) => new ManyIndices(many);
4760
public static implicit operator Indices(IndexName[] many) => new ManyIndices(many);
4861
public static implicit operator Indices(IndexName index) => new ManyIndices(new[] { index });
4962
public static implicit operator Indices(Type type) => new ManyIndices(new IndexName[] { type });

‎src/Nest/CommonAbstractions/Infer/Types/Types.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ public class ManyTypes
1616
{
1717
private readonly List<TypeName> _types = new List<TypeName>();
1818
public IReadOnlyList<TypeName> Types => _types;
19-
internal ManyTypes(IEnumerable<TypeName> types) { this._types.AddRange(types); }
19+
internal ManyTypes(IEnumerable<TypeName> types)
20+
{
21+
types.ThrowIfEmpty(nameof(types));
22+
this._types.AddRange(types);
23+
}
24+
25+
internal ManyTypes(IEnumerable<string> types)
26+
{
27+
types.ThrowIfEmpty(nameof(types));
28+
this._types.AddRange(types.Select(s=>(TypeName)s));
29+
}
2030

2131
public ManyTypes And<T>()
2232
{
@@ -33,6 +43,9 @@ internal Types(IEnumerable<TypeName> types) : base(new ManyTypes(types)) { }
3343
public static TypeName Type<T>() => typeof(T);
3444
public static ManyTypes Type(IEnumerable<TypeName> types) => new ManyTypes(types);
3545
public static ManyTypes Type(params TypeName[] types) => new ManyTypes(types);
46+
public static ManyTypes Type(IEnumerable<string> indices) => new ManyTypes(indices);
47+
public static ManyTypes Type(params string[] indices) => new ManyTypes(indices);
48+
3649

3750
public static Types Parse(string typesString)
3851
{
@@ -46,6 +59,7 @@ public static Types Parse(string typesString)
4659
public static implicit operator Types(ManyTypes many) => new Types(many);
4760
public static implicit operator Types(TypeName type) => new ManyTypes(new[] { type });
4861
public static implicit operator Types(TypeName[] type) => new ManyTypes(type);
62+
public static implicit operator Types(string[] many) => new ManyTypes(many);
4963
public static implicit operator Types(Type type) => new ManyTypes(new TypeName[] { type });
5064

5165
string IUrlParameter.GetString(IConnectionConfigurationValues settings)

‎src/Tests/ClientConcepts/HighLevel/Inference/IndicesPaths.doc.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ [U] public void ImplicitConversionFromString()
2121
{
2222
Nest.Indices singleIndexFromString = "name";
2323
Nest.Indices multipleIndicesFromString = "name1, name2";
24+
Nest.Indices multipleIndicesFromStringArray = new [] { "name1", "name2" };
2425
Nest.Indices allFromString = "_all";
2526
Nest.Indices allWithOthersFromString = "_all, name2";
2627

@@ -43,6 +44,10 @@ [U] public void ImplicitConversionFromString()
4344
all => all.Should().NotBeNull(),
4445
many => many.Indices.Should().BeNull()
4546
);
47+
multipleIndicesFromStringArray.Match(
48+
all => all.Should().BeNull(),
49+
many => many.Indices.Should().HaveCount(2).And.Contain("name2")
50+
);
4651
}
4752

4853
/**[[nest-indices]]
@@ -83,6 +88,12 @@ [U] public void MultipleIndices()
8388

8489
((IUrlParameter)manyStringRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");
8590
((IUrlParameter)manyTypedRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("project,devs"); // <3> The index names here come from the Connection Settings passed to `TestClient`. See the documentation on <<index-name-inference, Index Name Inference>> for more details.
91+
92+
manyStringRequest = new SearchDescriptor<Project>().Index(new[] { "name1", "name2" });
93+
((IUrlParameter)manyStringRequest.Index).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");
94+
95+
manyStringRequest = new SearchDescriptor<Project>().Type(new[] { "name1", "name2" });
96+
((IUrlParameter)manyStringRequest.Type).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");
8697
}
8798

8899
/**==== Specifying All Indices

0 commit comments

Comments
 (0)
Please sign in to comment.