Skip to content

Commit

Permalink
https://github.com/danieleteti/delphimvcframework/issues/485
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Aug 1, 2022
1 parent 8a6bd1c commit 40f1f21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
30 changes: 19 additions & 11 deletions samples/activerecord_restful_crud/Entities.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface
uses
MVCFramework.Serializer.Commons,
MVCFramework.ActiveRecord,
MVCFramework.Nullables,
System.Classes,
MVCFramework, System.Generics.Collections;

Expand All @@ -22,25 +23,25 @@ TPerson = class(TMVCActiveRecord)
[MVCTableField('FIRST_NAME')]
fFirstName: string;
[MVCTableField('DOB')]
fDOB: TDate;
fDOB: NullableTDate;
[MVCTableField('FULL_NAME')]
fFullName: string;
[MVCTableField('IS_MALE')]
fIsMale: Boolean;
fIsMale: NullableBoolean;
[MVCTableField('NOTE')]
fNote: string;
[MVCTableField('PHOTO')]
fPhoto: TStream;

// transient fields
fAge: Integer;
fAge: NullableInt32;

procedure SetLastName(const Value: string);
procedure SetID(const Value: Int64);
procedure SetFirstName(const Value: string);
procedure SetDOB(const Value: TDate);
procedure SetDOB(const Value: NullableTDate);
function GetFullName: string;
procedure SetIsMale(const Value: Boolean);
procedure SetIsMale(const Value: NullableBoolean);
procedure SetNote(const Value: string);
protected
procedure OnAfterLoad; override;
Expand All @@ -53,10 +54,10 @@ TPerson = class(TMVCActiveRecord)
property ID: Int64 read fID write SetID;
property LastName: string read fLastName write SetLastName;
property FirstName: string read fFirstName write SetFirstName;
property Age: Integer read fAge;
property DOB: TDate read fDOB write SetDOB;
property Age: NullableInt32 read fAge;
property DOB: NullableTDate read fDOB write SetDOB;
property FullName: string read GetFullName;
property IsMale: Boolean read fIsMale write SetIsMale;
property IsMale: NullableBoolean read fIsMale write SetIsMale;
property Note: string read fNote write SetNote;
property Photo: TStream read fPhoto;
end;
Expand Down Expand Up @@ -141,7 +142,14 @@ function TPerson.GetFullName: string;
procedure TPerson.OnAfterLoad;
begin
inherited;
fAge := Yearsbetween(fDOB, now);
if fDOB.HasValue then
begin
fAge := Yearsbetween(fDOB, now);
end
else
begin
fAge.Clear;
end;
end;

procedure TPerson.OnBeforeInsert;
Expand Down Expand Up @@ -176,7 +184,7 @@ procedure TPerson.SetNote(const Value: string);
fNote := Value;
end;

procedure TPerson.SetDOB(const Value: TDate);
procedure TPerson.SetDOB(const Value: NullableTDate);
begin
fDOB := Value;
end;
Expand All @@ -186,7 +194,7 @@ procedure TPerson.SetID(const Value: Int64);
fID := Value;
end;

procedure TPerson.SetIsMale(const Value: Boolean);
procedure TPerson.SetIsMale(const Value: NullableBoolean);
begin
fIsMale := Value;
end;
Expand Down
15 changes: 15 additions & 0 deletions sources/MVCFramework.Serializer.JsonDataObjects.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,9 @@ procedure TMVCJsonDataObjectsSerializer.JSONObjectPropertyToTValue(
jdtObject:
begin
if (AValue.TypeInfo = System.TypeInfo(TValue)) then
begin
AValue := TValue.FromVariant(AJsonObject[APropertyName].O['value'].VariantValue)
end
else
begin
// dt: if a key is null, jsondataobjects assign it the type jdtObject
Expand Down Expand Up @@ -1657,6 +1659,19 @@ procedure TMVCJsonDataObjectsSerializer.JSONObjectPropertyToTValue(
end;
end
end;
end
else if AValue.Kind = tkRecord then
begin
if String(AValue.TypeInfo.Name).StartsWith('Nullable') then
begin
//The json prop is "null", we have to check if the object prop type to do the right thing
//this is an hack, necessary for speed reason.
//Instead of check the type of each nullable type I "guess" that is the
//typename starts with "Nullable" it's a nullable defined in the unit MVCFramework.Nullables.pas.
//Dealing with raw memory address, the following line consider a pointer as a NullableInt32.
//The underline code works for all nullable types and save me to do type checking.
NullableInt32(AValue.GetReferenceToRawData^).SetNull;
end;
end;
end;
end;
Expand Down

0 comments on commit 40f1f21

Please sign in to comment.