|
|
|
@ -13,6 +13,7 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
{
|
|
|
|
|
public static class SchemaBuilder
|
|
|
|
|
{
|
|
|
|
|
private static readonly string PRIVATE_VALUE = "********";
|
|
|
|
|
private static Dictionary<Type, FieldMapping[]> _mappings = new Dictionary<Type, FieldMapping[]>();
|
|
|
|
|
|
|
|
|
|
public static List<Field> ToSchema(object model)
|
|
|
|
@ -26,7 +27,15 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
foreach (var mapping in mappings)
|
|
|
|
|
{
|
|
|
|
|
var field = mapping.Field.Clone();
|
|
|
|
|
field.Value = mapping.GetterFunc(model);
|
|
|
|
|
|
|
|
|
|
if (field.Privacy == PrivacyLevel.ApiKey || field.Privacy == PrivacyLevel.Password)
|
|
|
|
|
{
|
|
|
|
|
field.Value = PRIVATE_VALUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
field.Value = mapping.GetterFunc(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Add(field);
|
|
|
|
|
}
|
|
|
|
@ -34,7 +43,7 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
return result.OrderBy(r => r.Order).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object ReadFromSchema(List<Field> fields, Type targetType)
|
|
|
|
|
public static object ReadFromSchema(List<Field> fields, Type targetType, object model)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(targetType, () => targetType).IsNotNull();
|
|
|
|
|
|
|
|
|
@ -49,18 +58,25 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
|
|
|
|
|
if (field != null)
|
|
|
|
|
{
|
|
|
|
|
mapping.SetterFunc(target, field.Value);
|
|
|
|
|
// Use the Privacy property from the mapping's field as Privacy may not be set in the API request (nor is it required)
|
|
|
|
|
if ((mapping.Field.Privacy == PrivacyLevel.ApiKey || mapping.Field.Privacy == PrivacyLevel.Password) &&
|
|
|
|
|
(field.Value?.ToString()?.Equals(PRIVATE_VALUE) ?? false) &&
|
|
|
|
|
model != null)
|
|
|
|
|
{
|
|
|
|
|
var existingValue = mapping.GetterFunc(model);
|
|
|
|
|
|
|
|
|
|
mapping.SetterFunc(target, existingValue);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mapping.SetterFunc(target, field.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T ReadFromSchema<T>(List<Field> fields)
|
|
|
|
|
{
|
|
|
|
|
return (T)ReadFromSchema(fields, typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ideally this function should begin a System.Linq.Expression expression tree since it's faster.
|
|
|
|
|
// But it's probably not needed till performance issues pop up.
|
|
|
|
|
public static FieldMapping[] GetFieldMappings(Type type)
|
|
|
|
@ -104,7 +120,8 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
Order = fieldAttribute.Order,
|
|
|
|
|
Advanced = fieldAttribute.Advanced,
|
|
|
|
|
Type = fieldAttribute.Type.ToString().FirstCharToLower(),
|
|
|
|
|
Section = fieldAttribute.Section
|
|
|
|
|
Section = fieldAttribute.Section,
|
|
|
|
|
Privacy = fieldAttribute.Privacy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (fieldAttribute.Type == FieldType.Select || fieldAttribute.Type == FieldType.TagSelect)
|
|
|
|
@ -131,7 +148,7 @@ namespace Sonarr.Http.ClientSchema
|
|
|
|
|
Field = field,
|
|
|
|
|
PropertyType = propertyInfo.PropertyType,
|
|
|
|
|
GetterFunc = t => propertyInfo.GetValue(targetSelector(t), null),
|
|
|
|
|
SetterFunc = (t, v) => propertyInfo.SetValue(targetSelector(t), valueConverter(v), null)
|
|
|
|
|
SetterFunc = (t, v) => propertyInfo.SetValue(targetSelector(t), v?.GetType() == propertyInfo.PropertyType ? v : valueConverter(v), null)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|