Skip to content

Commit

Permalink
feat(): clean up validations
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Feser <[email protected]>
  • Loading branch information
joefeser committed Aug 10, 2024
1 parent 38966ce commit 185b712
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 1,757 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ internal static object CreateInstance(this Type t, params object[] args)
where p.Length == args.Length
select c;


/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
*/
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
CachedActivators.TryAdd(key, activator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,13 @@ public string Resolve(PropertyName property)
private string Resolve(Expression expression, MemberInfo member, bool toLastToken = false)
{
var visitor = new FieldExpressionVisitor(_settings);

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var name = expression != null
? visitor.Resolve(expression, toLastToken)
: member != null
? visitor.Resolve(member)
: null;
After:
var name = (expression != null
? visitor.Resolve(expression, toLastToken)
: member != null
? visitor.Resolve(member)
: null) ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
*/
var name = (expression != null
var name = expression != null
? visitor.Resolve(expression, toLastToken)
: member != null
? visitor.Resolve(member)
: null) ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
return name;
: null;

return name ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ internal static object GetFormatter(Type t)

Type implementationType;
if (t.IsInterface)

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
if (readAsAttribute == null)
throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
After:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
*/
{
// need an implementation to deserialize interface to
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
Expand All @@ -99,31 +87,6 @@ internal static object GetFormatter(Type t)
orderby p.Length descending
select c;


/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting "
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
+ $"or a public parameterless constructor");
// construct a delegate for the ctor
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting "
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
+ $"or a public parameterless constructor");
// construct a delegate for the ctor
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
*/
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting "
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
Expand Down Expand Up @@ -173,18 +136,6 @@ internal static object GetFormatter(Type t)

Type implementationType;
if (t.IsInterface)

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
if (readAsAttribute == null)
throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
After:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
*/
{
// need an implementation to deserialize interface to
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
Expand All @@ -204,27 +155,6 @@ internal static object GetFormatter(Type t)
orderby p.Length descending
select c;


/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");
// construct a delegate for the ctor
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");
// construct a delegate for the ctor
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
*/
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");

Expand Down Expand Up @@ -297,16 +227,6 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe
// TODO mutator is not used, should it?
var mutator = formatterResolver.GetConnectionSettings().DefaultFieldNameInferrer;
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
writer.WriteBeginObject();
After:
writer.WriteBeginObject();
*/

writer.WriteBeginObject();

var e = GetSourceEnumerator(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ internal static object GetFormatter(Type t)

Type implementationType;
if (t.IsInterface)

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
if (readAsAttribute == null)
throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
After:
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
implementationType = readAsAttribute.Type.IsGenericType
*/
{
// need an implementation to deserialize interface to
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
Expand All @@ -95,31 +83,6 @@ internal static object GetFormatter(Type t)
orderby p.Length descending
select c;


/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a constructor accepting "
+ $"{genericDictionaryInterface.FullName} argument "
+ $"or a parameterless constructor");
// construct a delegate for the ctor
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs.ToArray(), activator, ctor.GetParameters().Length == 0);
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a constructor accepting "
+ $"{genericDictionaryInterface.FullName} argument "
+ $"or a parameterless constructor");
// construct a delegate for the ctor
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
var activator = activatorMethod.Invoke(null, new object[] { ctor });
return CreateInstance(genericTypeArgs.ToArray(), activator, ctor.GetParameters().Length == 0);
*/
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
+ $"have a constructor accepting "
+ $"{genericDictionaryInterface.FullName} argument "
Expand All @@ -144,25 +107,6 @@ internal class IsADictionaryBaseFormatter<TKey, TValue, TDictionary>
private readonly bool _parameterlessCtor;

public IsADictionaryBaseFormatter(TypeExtensions.ObjectActivator<TDictionary> activator, bool parameterlessCtor)

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>() as IObjectPropertyNameFormatter<TKey>;
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
writer.WriteBeginObject();
After:
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
writer.WriteBeginObject();
*/

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
if (keyFormatter != null)
After:
if (formatterResolver.GetFormatterWithVerify<TKey>() is IObjectPropertyNameFormatter<TKey> keyFormatter)
*/
{
_activator = activator;
_parameterlessCtor = parameterlessCtor;
Expand Down
9 changes: 8 additions & 1 deletion src/OpenSearch.Client/CommonOptions/Geo/Distance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ public Distance(string distanceUnit)
return;
}

var unitMeasure = unit.ToEnum<DistanceUnit>() ?? throw new InvalidCastException($"cannot parse {typeof(DistanceUnit).Name} from string '{unit}'");
var unitMeasure = unit.ToEnum<DistanceUnit>();
#pragma warning disable IDE0270 // Use coalesce expression
if (unitMeasure == null)
{
throw new InvalidCastException($"cannot parse {typeof(DistanceUnit).Name} from string '{unit}'");
}
#pragma warning restore IDE0270 // Use coalesce expression

Unit = unitMeasure.Value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@ private BulkAllObservable<IHitMetadata<TTarget>> BulkAll(IEnumerable<IHitMetadat
{
var bulkAllRequest = (_reindexRequest.BulkAll?.Invoke(scrollDocuments)) ?? throw new Exception("BulkAll must set on ReindexRequest in order to get the target of a Reindex operation");

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
if (bulkAllRequest == null)
throw new Exception("BulkAll must set on ReindexRequest in order to get the target of a Reindex operation");
bulkAllRequest.BackPressure = backPressure;
After:
bulkAllRequest.BackPressure = backPressure;
*/
bulkAllRequest.BackPressure = backPressure;
bulkAllRequest.BufferToBulk = (bulk, hits) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,6 @@ public IDisposable Subscribe(IObserver<SnapshotStatusResponse> observer)
}

private void Snapshot(object state)

/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
Before:
var observer = state as IObserver<SnapshotStatusResponse>;
if (observer == null) throw new ArgumentException("state");
try
{
After:
var observer = state as IObserver<SnapshotStatusResponse> ?? throw new ArgumentException("state");
try
{
*/
{
var observer = state as IObserver<SnapshotStatusResponse> ?? throw new ArgumentException("state");
try
Expand Down
36 changes: 0 additions & 36 deletions src/OpenSearch.Net/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,42 +57,6 @@ internal static object CreateInstance(this Type t, params object[] args)
where p.Length == args.Length
select c;


/* Unmerged change from project 'OpenSearch.Net(netstandard2.1)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
*/

/* Unmerged change from project 'OpenSearch.Net(net6.0)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
*/

/* Unmerged change from project 'OpenSearch.Net(net8.0)'
Before:
var ctor = constructors.FirstOrDefault();
if (ctor == null)
throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
After:
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
*/
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
CachedActivators.TryAdd(key, activator);
Expand Down
Loading

0 comments on commit 185b712

Please sign in to comment.