Skip to content

Commit

Permalink
Add support for deprecated values in field select options
Browse files Browse the repository at this point in the history
(cherry picked from commit d9786887f3fe30ef60ad9c50b3272bf60dfef309)

Closes #3917
  • Loading branch information
mynameisbogdan committed Jul 23, 2023
1 parent 04aebc4 commit f270893
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/Lidarr.Http/ClientSchema/SchemaBuilder.cs
Expand Up @@ -107,7 +107,7 @@ private static FieldMapping[] GetFieldMapping(Type type, string prefix, Func<obj
Placeholder = fieldAttribute.Placeholder
};

if (fieldAttribute.Type == FieldType.Select || fieldAttribute.Type == FieldType.TagSelect)
if (fieldAttribute.Type is FieldType.Select or FieldType.TagSelect)
{
if (fieldAttribute.SelectOptionsProviderAction.IsNotNullOrWhiteSpace())
{
Expand Down Expand Up @@ -154,33 +154,40 @@ private static FieldMapping[] GetFieldMapping(Type type, string prefix, Func<obj

private static List<SelectOption> GetSelectOptions(Type selectOptions)
{
var options = selectOptions.GetFields().Where(v => v.IsStatic).Select(v =>
if (selectOptions.IsEnum)
{
var name = v.Name.Replace('_', ' ');
var value = Convert.ToInt32(v.GetRawConstantValue());
var attrib = v.GetCustomAttribute<FieldOptionAttribute>();
if (attrib != null)
{
return new SelectOption
{
Value = value,
Name = attrib.Label ?? name,
Order = attrib.Order,
Hint = attrib.Hint ?? $"({value})"
};
}
else
{
return new SelectOption
var options = selectOptions
.GetFields()
.Where(v => v.IsStatic && !v.GetCustomAttributes(false).OfType<ObsoleteAttribute>().Any())
.Select(v =>
{
Value = value,
Name = name,
Order = value
};
}
});
var name = v.Name.Replace('_', ' ');
var value = Convert.ToInt32(v.GetRawConstantValue());
var attrib = v.GetCustomAttribute<FieldOptionAttribute>();
if (attrib != null)
{
return new SelectOption
{
Value = value,
Name = attrib.Label ?? name,
Order = attrib.Order,
Hint = attrib.Hint ?? $"({value})"
};
}
return new SelectOption
{
Value = value,
Name = name,
Order = value
};
});

return options.OrderBy(o => o.Order).ToList();
}

return options.OrderBy(o => o.Order).ToList();
throw new NotSupportedException();
}

private static Func<object, object> GetValueConverter(Type propertyType)
Expand Down

0 comments on commit f270893

Please sign in to comment.