Skip to content

Commit

Permalink
reduce static function members
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed Mar 24, 2022
1 parent 536d7bf commit 080f53b
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/Utils/ParameterUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ internal static IEnumerable<KeyValuePair<string, string>> GetParameter<T>(object
// check if the type we've got is an IEnumerable of anything AND we have a valid collection handler mode
if (attribute.CollectionHandling.HasValue && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
Func<IEnumerable, string, CultureInfo, IEnumerable<KeyValuePair<string, string>>> entityConverter = attribute.CollectionHandling switch
Func<IEnumerable, IEnumerable<KeyValuePair<string, string>>> entityConverter = attribute.CollectionHandling switch
{
CollectionConversionMode.Recursive => ApplyRecursiveConversion,
CollectionConversionMode.Unordered => ApplyUnorderedConversion,
CollectionConversionMode.Ordered => ApplyOrderedConversion,
CollectionConversionMode.Concatenated => (a, b, c) => ApplyConcatenation(a, b, c, attribute.CollectionSeparator ?? DefaultConcatenationCharacter),
CollectionConversionMode.Ordered => values => ApplyOrderedConversion(values, keyName, culture),
CollectionConversionMode.Recursive => values => values.Cast<object>().Select(x => x.ToKeyValuePair(keyName, culture)),
CollectionConversionMode.Unordered => values => values.Cast<object>().Select(x => x.ToKeyValuePair($"{keyName}[]", culture)),
CollectionConversionMode.Concatenated => values => ApplyConcatenation(values, keyName, culture, attribute.CollectionSeparator ?? DefaultConcatenationCharacter),

_ => throw new ArgumentOutOfRangeException()
};

foreach (var entry in entityConverter.Invoke((IEnumerable)propertyValue, keyName, culture))
// we purposely keep nulls in here, as it might affect the ordering.
// we purposely keep nulls in here, as it might affect the ordering.
foreach (var entry in entityConverter.Invoke((IEnumerable)propertyValue))
{
yield return entry;
}
Expand Down Expand Up @@ -102,16 +102,6 @@ internal static object GetSingleParameterObject<T>(object host) where T : Attrib
return attributedProperty.GetValue(host);
}

private static IEnumerable<KeyValuePair<string, string>> ApplyRecursiveConversion(IEnumerable values, string keyName, CultureInfo culture)
{
return values.Cast<object>().Select(x => x.ToKeyValuePair(keyName, culture));
}

private static IEnumerable<KeyValuePair<string, string>> ApplyUnorderedConversion(IEnumerable values, string keyName, CultureInfo culture)
{
return values.Cast<object>().Select(x => x.ToKeyValuePair($"{keyName}[]", culture));
}

private static IEnumerable<KeyValuePair<string, string>> ApplyOrderedConversion(IEnumerable values, string keyName, CultureInfo culture)
{
var counter = 0;
Expand Down

0 comments on commit 080f53b

Please sign in to comment.