| | 1 | | using Fluorite.Strainer.Services.Conversion; |
| | 2 | |
|
| | 3 | | namespace Fluorite.Strainer.Services.Filtering.Steps; |
| | 4 | |
|
| | 5 | | public class ConvertFilterValueToStringStep : IConvertFilterValueToStringStep |
| | 6 | | { |
| | 7 | | private readonly ITypeConverterProvider _typeConverterProvider; |
| | 8 | | private readonly IStringValueConverter _stringValueConverter; |
| | 9 | |
|
| 5 | 10 | | public ConvertFilterValueToStringStep( |
| 5 | 11 | | ITypeConverterProvider typeConverterProvider, |
| 5 | 12 | | IStringValueConverter stringValueConverter) |
| | 13 | | { |
| 5 | 14 | | _typeConverterProvider = Guard.Against.Null(typeConverterProvider); |
| 5 | 15 | | _stringValueConverter = Guard.Against.Null(stringValueConverter); |
| 5 | 16 | | } |
| | 17 | |
|
| | 18 | | public void Execute(FilterExpressionWorkflowContext context) |
| | 19 | | { |
| 4 | 20 | | Guard.Against.Null(context); |
| 4 | 21 | | Guard.Against.Null(context.Term); |
| 4 | 22 | | Guard.Against.Null(context.Term.Operator); |
| 4 | 23 | | Guard.Against.Null(context.PropertyMetadata); |
| 4 | 24 | | Guard.Against.Null(context.PropertyMetadata.PropertyInfo); |
| 4 | 25 | | Guard.Against.Null(context.FilterTermValue); |
| | 26 | |
|
| 4 | 27 | | if (context.Term.Operator.IsStringBased) |
| | 28 | | { |
| 1 | 29 | | return; |
| | 30 | | } |
| | 31 | |
|
| 3 | 32 | | var propertyType = context.PropertyMetadata!.PropertyInfo!.PropertyType; |
| 3 | 33 | | var typeConverter = _typeConverterProvider.GetTypeConverter(propertyType); |
| 3 | 34 | | var canConvertFromString = |
| 3 | 35 | | context.PropertyMetadata.PropertyInfo.PropertyType != typeof(string) |
| 3 | 36 | | && typeConverter.CanConvertFrom(typeof(string)); |
| | 37 | |
|
| 3 | 38 | | if (canConvertFromString == true) |
| | 39 | | { |
| 1 | 40 | | context.FilterTermConstant = _stringValueConverter.Convert( |
| 1 | 41 | | context.FilterTermValue, |
| 1 | 42 | | context.PropertyMetadata.PropertyInfo.PropertyType, |
| 1 | 43 | | typeConverter); |
| | 44 | | } |
| 3 | 45 | | } |
| | 46 | | } |