@ -15,6 +15,7 @@ namespace Prowlarr.Http.ClientSchema
{
public static class SchemaBuilder
{
private const string PRIVATE_VALUE = "********" ;
private static Dictionary < Type , FieldMapping [ ] > _mappings = new Dictionary < Type , FieldMapping [ ] > ( ) ;
private static ILocalizationService _localizationService ;
@ -36,13 +37,19 @@ namespace Prowlarr.Http.ClientSchema
var field = mapping . Field . Clone ( ) ;
field . Value = mapping . GetterFunc ( model ) ;
if ( field . Value ! = null & & ! field . Value . Equals ( string . Empty ) & &
( field . Privacy = = PrivacyLevel . ApiKey | | field . Privacy = = PrivacyLevel . Password ) )
{
field . Value = PRIVATE_VALUE ;
}
result . Add ( field ) ;
}
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 ( ) ;
@ -57,18 +64,25 @@ namespace Prowlarr.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 )
@ -127,6 +141,7 @@ namespace Prowlarr.Http.ClientSchema
Advanced = fieldAttribute . Advanced ,
Type = fieldAttribute . Type . ToString ( ) . FirstCharToLower ( ) ,
Section = fieldAttribute . Section ,
Privacy = fieldAttribute . Privacy ,
Placeholder = fieldAttribute . Placeholder
} ;
@ -159,7 +174,7 @@ namespace Prowlarr.Http.ClientSchema
Field = field ,
PropertyType = propertyInfo . PropertyType ,
GetterFunc = t = > propertyInfo . GetValue ( targetSelector ( t ) , null ) ,
SetterFunc = ( t , v ) = > propertyInfo . SetValue ( targetSelector ( t ) , v alueConverter( v ) , null )
SetterFunc = ( t , v ) = > propertyInfo . SetValue ( targetSelector ( t ) , v ? . GetType ( ) = = propertyInfo . PropertyType ? v : v alueConverter( v ) , null )
} ) ;
}
else