Skip to content

Commit

Permalink
Clear up Name and Id parameters (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebrain authored Jun 12, 2019
1 parent 2ced72f commit a9b2fb1
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 366 deletions.
54 changes: 31 additions & 23 deletions src/CodeGeneration/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Transactions;

namespace ApiGenerator.Domain.Specification
{
Expand Down Expand Up @@ -36,49 +37,56 @@ public string ClrTypeName

switch (Name)
{
case "category_id": return "LongId";
case "timestamp": return "Timestamp";
case "index_metric": return "IndexMetrics";
case "metric": return "Metrics";

case "node_id" when Type == "list":
return "NodeIds";

case "fields" when Type == "list":
return "Fields";

case "parent_task_id":
case "task_id":
return "TaskId";

case "forecast_id":
case "action_id":
return "Ids";

case "index":
case "new_index":
case "target":
return Type == "string" ? "IndexName" : "Indices";
case "target":
return "IndexName";
case "type": return Type == "string" ? "Name" : "Names";

case "watch_id":
case "job_id":
case "calendar_id":
case "event_id":
case "datafeed_id":
case "snapshot_id":
case "filter_id":
case "id": return Type == "string" ? "Id" : "Ids";
case "category_id": return "CategoryId";
case "policy_id": return "PolicyId";
case "forecast_id": return "ForecastIds";
case "nodes":
case "node_id": return Type == "string" ? "NodeId" : "NodeIds";
case "field":
case "fields": return Type == "string" ? "Field" : "Fields";
case "index_metric": return "IndexMetrics";
case "metric":
return "Metrics";
case "feature": return "Features";
case "action_id": return "ActionIds";
case "policy_id":
case "id":
return "Id";

case "application":
case "repository":
case "snapshot":
case "lang":
case "user":
case "username":
case "usernames":
case "realm":
case "realms":
case "alias":
case "context":
case "name":
case "user":
case "thread_pool_patterns":
case "type":
return Type == "string" ? "Name" : "Names";
case "parent_task_id":
case "task_id": return "TaskId";
case "timestamp": return "Timestamp";


//This forces a compilation error post code generation as intended
default: return Type + "_";
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Elasticsearch.Net/Serialization/IUrlParameter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Globalization;

namespace Elasticsearch.Net
{
public interface IUrlParameter
Expand Down
61 changes: 0 additions & 61 deletions src/Nest/CommonAbstractions/Infer/ActionIds/ActionIds.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/Nest/CommonAbstractions/Infer/CategoryId/CategoryId.cs

This file was deleted.

61 changes: 0 additions & 61 deletions src/Nest/CommonAbstractions/Infer/ForecastIds/ForecastIds.cs

This file was deleted.

61 changes: 61 additions & 0 deletions src/Nest/CommonAbstractions/Infer/Id/Ids.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Elasticsearch.Net;

namespace Nest
{
[DebuggerDisplay("{DebugDisplay,nq}")]
public class Ids : IUrlParameter, IEquatable<Ids>
{
private readonly List<string> _ids;

public Ids(IEnumerable<string> value) => _ids = value?.ToList();

public Ids(string value)
{
if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
_ids = arr.ToList();
}

private string DebugDisplay => ((IUrlParameter)this).GetString(null);

public bool Equals(Ids other)
{
if (other == null) return false;
if (_ids == null && other._ids == null) return true;
if (_ids == null || other._ids == null) return false;

return _ids.Count == other._ids.Count &&
_ids.OrderBy(id => id).SequenceEqual(other._ids.OrderBy(id => id));
}

string IUrlParameter.GetString(IConnectionConfigurationValues settings) =>
string.Join(",", _ids ?? Enumerable.Empty<string>());

public static implicit operator Ids(string value) =>
value.IsNullOrEmptyCommaSeparatedList(out var arr) ? null : new Ids(arr);

public static implicit operator Ids(string[] value) =>
value.IsEmpty() ? null : new Ids(value);

public override bool Equals(object obj) => obj is Ids other && Equals(other);

public override int GetHashCode()
{
if (_ids == null) return 0;
unchecked
{
var hc = 0;
foreach (var id in _ids.OrderBy(id => id))
hc = hc * 17 + id.GetHashCode();
return hc;
}
}

public static bool operator ==(Ids left, Ids right) => Equals(left, right);

public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
}
}
39 changes: 39 additions & 0 deletions src/Nest/CommonAbstractions/Infer/LongId/LongId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Globalization;
using Elasticsearch.Net;

namespace Nest
{
public class LongId : IUrlParameter, IEquatable<LongId>
{
internal readonly long Value;

public LongId(long value) => Value = value;

public bool Equals(LongId other) => Value == other.Value;

// ReSharper disable once ImpureMethodCallOnReadonlyValueField
public string GetString(IConnectionConfigurationValues settings) => Value.ToString(CultureInfo.InvariantCulture);

public static implicit operator LongId(long value) => new LongId(value);

public static implicit operator long(LongId value) => value.Value;

public override bool Equals(object obj)
{
switch (obj)
{
case int l: return Value == l;
case long l: return Value == l;
case LongId i: return Value == i.Value;
default: return false;
}
}

public override int GetHashCode() => Value.GetHashCode();

public static bool operator ==(LongId left, LongId right) => Equals(left, right);

public static bool operator !=(LongId left, LongId right) => !Equals(left, right);
}
}
38 changes: 0 additions & 38 deletions src/Nest/CommonAbstractions/Infer/Policy/Policy.cs

This file was deleted.

Loading

0 comments on commit a9b2fb1

Please sign in to comment.