TValueConverterFactory does not return a converter for a Nullable<EnumType>
We have the following type defined:
TDocumentItemType = (
ditNone, //none
ditSale, //Sale
ditReturn, //Return
ditOrder, //Order
ditExchange, //Exchange
ditVoid //Item was voided
);
And then a Field in one of our ORM Model files as follows:
[Column('ITEM_TYPE',[],1,0)]
FItemType: Nullable<TDocumentItemType>;
When the ORM attempts to read the data and convert it the following error occurs:
It would seem that it is valid to have a Nullable Enumerated type as the database can certaintily have no value.
After tracing into the code it seems that the TValueConverterFactory does not return a Converter for a Nullable<TEnumType> type.
Is it possible to add a converter for nullable enum types.
Fails here with no converter.
function TDefaultConverter.TryConvertToParam(const value: TValue;
const targetTypeInfo: PTypeInfo;
out targetValue: TValue;
const parameter: TValue): Boolean;
var
converter: IValueConverter;
begin
converter := TValueConverterFactory.GetConverter(value.TypeInfo, targetTypeInfo);
Result := Assigned(converter)
and converter.TryConvertTo(value, targetTypeInfo, targetValue, parameter);
if not Result then
{$IFNDEF DELPHIXE2_UP}
// workaround for wrong TValue.TryCast for string to float (it calls ConvStr2Str by mistake)
if not ((value.Kind in [tkString, tkLString, tkWString, tkUString])
and (targetTypeInfo.Kind = tkFloat)) then
{$ENDIF}
Result := value.TryCast(targetTypeInfo, targetValue);
end;
Comments (10)
-
reporter -
reporter - changed version to 1.1
-
repo owner If there is no converter registered, it cannot return one, simple. Write a converter for Integer<->TDocumentItemType and register it.
-
reporter We use enum types a lot. It would be better to have one converter that knew how to convert any enum. Can you suggest how to do this?
-
repo owner Look into Spring.ValueConverters.pas - there are multiple converters for nullable to inner type and vice versa and integer<->enum. You just need one for integer <-> nullable<enum>. The trick however will be the correct registration if you want to make it for all nullable enums in general and not for each enum type explicitly. But I guess you'll figure it out.
-
repo owner - changed version to 1.2 (develop)
- marked as enhancement
- marked as minor
- edited description
- changed component to Base
-
repo owner - changed status to resolved
fixed
#207→ <<cset 60d25d6df012>>
-
repo owner fixed
#207→ <<cset 5660b1ff0939>>
-
repo owner fixed
#207→ <<cset 0a1daf980ae0>>
-
repo owner - changed version to 1.2
- Log in to comment
So I write a test for this to hopefully help you see the issue. Into the Spring.Tests.ValueConverters unit TTestFromInteger fixture.