- pattern matching refactoring

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
This commit is contained in:
Vincent Biret 2021-11-19 13:51:05 -05:00
parent 094bf81732
commit 883ca922db
No known key found for this signature in database
GPG key ID: 32426322EDFFB7E3
2 changed files with 57 additions and 133 deletions

View file

@ -29,20 +29,10 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmIntegerConstantExpression value = property.Value as IEdmIntegerConstantExpression;
if (value != null)
{
return value.Value;
}
}
}
return null;
return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmIntegerConstantExpression value) ?
value.Value :
null;
}
/// <summary>
@ -56,20 +46,10 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmStringConstantExpression value = property.Value as IEdmStringConstantExpression;
if (value != null)
{
return value.Value;
}
}
}
return null;
return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmStringConstantExpression value) ?
value.Value :
null;
}
/// <summary>
@ -83,20 +63,10 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmBooleanConstantExpression value = property.Value as IEdmBooleanConstantExpression;
if (value != null)
{
return value.Value;
}
}
}
return null;
return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmBooleanConstantExpression value) ?
value.Value :
null;
}
/// <summary>
@ -112,25 +82,13 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmEnumMemberExpression value = property.Value as IEdmEnumMemberExpression;
if (value != null && value.EnumMembers != null && value.EnumMembers.Any())
{
IEdmEnumMember member = value.EnumMembers.First();
T result;
if (Enum.TryParse(member.Name, out result))
{
return result;
}
}
}
}
return null;
return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmEnumMemberExpression value &&
value.EnumMembers != null &&
value.EnumMembers.Any() &&
Enum.TryParse(value.EnumMembers.First().Name, out T result)) ?
result :
null;
}
/// <summary>
@ -146,16 +104,12 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmRecordExpression recordValue)
{
IEdmRecordExpression recordValue = property.Value as IEdmRecordExpression;
if (recordValue != null)
{
T a = new T();
a.Initialize(recordValue);
return a;
}
T a = new();
a.Initialize(recordValue);
return a;
}
return default;
@ -172,20 +126,10 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmPathExpression value = property.Value as IEdmPathExpression;
if (value != null)
{
return value.Path;
}
}
}
return null;
return (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmPathExpression value) ?
value.Path :
null;
}
/// <summary>
@ -199,25 +143,18 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
if (record.Properties != null)
if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression value && value.Elements != null)
{
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
{
IEdmCollectionExpression value = property.Value as IEdmCollectionExpression;
if (value != null && value.Elements != null)
{
IList<string> properties = new List<string>();
foreach (var path in value.Elements.Select(e => e as IEdmPathExpression))
{
properties.Add(path.Path);
}
IList<string> properties =
value.Elements
.OfType<IEdmPathExpression>()
.Select(x => x.Path)
.ToList();
if (properties.Any())
{
return properties;
}
}
if (properties.Any())
{
return properties;
}
}
@ -235,21 +172,14 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression collection && collection.Elements != null)
{
IEdmCollectionExpression collection = property.Value as IEdmCollectionExpression;
if (collection != null && collection.Elements != null)
{
IList<string> items = new List<string>();
foreach (var item in collection.Elements)
{
IEdmStringConstantExpression itemRecord = item as IEdmStringConstantExpression;
items.Add(itemRecord.Value);
}
return items;
}
IList<string> items = collection.Elements
.OfType<IEdmStringConstantExpression>()
.Select(x => x.Value)
.ToList();
return items;
}
return null;
@ -268,22 +198,18 @@ namespace Microsoft.OpenApi.OData.Edm
Utils.CheckArgumentNull(record, nameof(record));
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
if (property != null)
if (record.Properties?.FirstOrDefault(e => e.Name == propertyName) is IEdmPropertyConstructor property &&
property.Value is IEdmCollectionExpression collection && collection.Elements != null)
{
IEdmCollectionExpression collection = property.Value as IEdmCollectionExpression;
if (collection != null && collection.Elements != null)
IList<T> items = new List<T>();
foreach (IEdmRecordExpression item in collection.Elements.OfType<IEdmRecordExpression>())
{
IList<T> items = new List<T>();
foreach (IEdmRecordExpression item in collection.Elements.OfType<IEdmRecordExpression>())
{
T a = new T();
a.Initialize(item);
items.Add(a);
}
return items;
T a = new();
a.Initialize(item);
items.Add(a);
}
return items;
}
return null;

View file

@ -36,7 +36,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities
/// Test the target supports count.
/// </summary>
/// <returns>True/false.</returns>
public bool IsCountable => Countable == null || Countable.Value;
public bool IsCountable => !Countable.HasValue || Countable.Value;
/// <summary>
/// Test the input property path which do not allow /$count segments.
@ -45,7 +45,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities
/// <returns>True/False.</returns>
public bool IsNonCountableProperty(string propertyPath)
{
return NonCountableProperties != null ? NonCountableProperties.Any(a => a == propertyPath) : false;
return NonCountableProperties != null && NonCountableProperties.Any(a => a == propertyPath);
}
/// <summary>
@ -55,9 +55,7 @@ namespace Microsoft.OpenApi.OData.Vocabulary.Capabilities
/// <returns>True/False.</returns>
public bool IsNonCountableNavigationProperty(string navigationPropertyPath)
{
return NonCountableNavigationProperties != null ?
NonCountableNavigationProperties.Any(a => a == navigationPropertyPath) :
false;
return NonCountableNavigationProperties != null && NonCountableNavigationProperties.Any(a => a == navigationPropertyPath);
}
/// <summary>