Skip to content

Commit

Permalink
https://github.com/danieleteti/delphimvcframework/issues/366
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Apr 19, 2020
1 parent f853b36 commit 08f01e6
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 177 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ end;

- Fixed! [issue363](https://github.com/danieleteti/delphimvcframework/issues/363)

- Fixed! [issue366](https://github.com/danieleteti/delphimvcframework/issues/366)

- **Breaking Change!** In `MVCActiveRecord` attribute `MVCPrimaryKey` has been removed and merged with `MVCTableField`, so now `TMVCActiveRecordFieldOption` is a set of `foPrimaryKey`, `foAutoGenerated`, `foTransient` (check `activerecord_showcase.dproj` sample).

- **Breaking Change!** Middleware `OnAfterControllerAction` are now invoked in the same order of `OnBeforeControllerAction` (previously were invoked in reversed order).
Expand Down
10 changes: 6 additions & 4 deletions sources/MVCFramework.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ TMVCObjectDictionary = class
procedure SetItems(const Key: string; const Value: TObject);
protected
fDict: TObjectDictionary<string, TObject>;
{
TMVCSerializationAction = reference to procedure(const AObject: TObject; const Links: IMVCLinks);
TMVCDataSetSerializationAction = reference to procedure(const ADataSet: TDataset; const Links: IMVCLinks);
{
TMVCSerializationAction = reference to procedure(const AObject: TObject; const Links: IMVCLinks);
TMVCDataSetSerializationAction = reference to procedure(const ADataSet: TDataset; const Links: IMVCLinks);
}
}
public
constructor Create(const OwnsValues: Boolean = True); overload; virtual;
constructor Create(const aKey: string; const Value: TObject; const OwnsValues: Boolean = True); overload; virtual;
Expand Down Expand Up @@ -648,6 +648,8 @@ TMVCParseAuthentication = class
VPassword: string; var VHandled: Boolean);
end;



implementation

uses
Expand Down
79 changes: 40 additions & 39 deletions sources/MVCFramework.DataSet.Utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,31 @@ interface
TDataSetHelper = class helper for TDataSet
public
procedure LoadFromTValue(const Value: TValue; const aNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase);
function AsJSONArray: string;
function AsJDOJSONArray: TJDOJsonArray;
function AsJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): string;
function AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): TJDOJsonArray;
function AsJSONArrayOfValues: TJDOJsonArray;
function AsJSONArrayString: string; deprecated 'Use AsJSONArray';
function AsJSONObject(AFieldNamePolicy: TFieldNamePolicy = fpLowerCase): string;
function AsJSONObject(FieldNameCase: TMVCNameCase = ncLowerCase; const IgnoredFields: TArray<string> = []): string;
function AsJSONObjectString: string; deprecated 'Use AsJSONObject';
procedure LoadFromJSONObject(AJSONObject: TJSONObject; AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload;
procedure LoadFromJSONObject(AJSONObject: TJSONObject; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy = fpLowerCase); overload;
procedure LoadFromJSONObject(const JSONObject: TJSONObject; const FieldNameCase: TMVCNameCase); overload;
procedure LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string> = [];
const FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: string;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONArrayString(AJSONArrayString: string;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONArray(AJSONArray: TJSONArray;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadFromJSONObjectString(AJSONObjectString: string); overload;
procedure LoadFromJSONObjectString(AJSONObjectString: string; AIgnoredFields: TArray<string>); overload;
procedure LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase = ncLowerCase); overload;
procedure LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string; const aPropertyName: string;
const AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase);
const FieldNameCase: TMVCNameCase = ncLowerCase);
procedure AppendFromJSONArrayString(AJSONArrayString: string); overload;
procedure AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy = TFieldNamePolicy.fpLowerCase); overload;
FieldNameCase: TMVCNameCase = TMVCNameCase.ncLowerCase); overload;
function AsObjectList<T: class, constructor>(CloseAfterScroll: boolean = false; OwnsObjects: boolean = true)
: TObjectList<T>;
function AsObject<T: class, constructor>(CloseAfterScroll: boolean = false): T;
Expand Down Expand Up @@ -188,20 +189,20 @@ procedure TDataSetHelper.LoadFromTValue(const Value: TValue; const aNameCase: TM
end;

procedure TDataSetHelper.LoadJSONArrayFromJSONObjectProperty(const AJSONObjectString: string;
const aPropertyName: string; const AFieldNamePolicy: TFieldNamePolicy);
const aPropertyName: string; const FieldNameCase: TMVCNameCase);
var
lJson: TJSONObject;
begin
lJson := TJSONObject.Create;
try
lJson.FromJSON(AJSONObjectString);
LoadFromJSONArray(lJson.A[aPropertyName], AFieldNamePolicy);
LoadFromJSONArray(lJson.A[aPropertyName], FieldNameCase);
finally
lJson.Free;
end;
end;

function TDataSetHelper.AsJDOJSONArray: TJDOJsonArray;
function TDataSetHelper.AsJDOJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): TJDOJsonArray;
var
lSerializer: TMVCJsonDataObjectsSerializer;
begin
Expand All @@ -211,7 +212,7 @@ function TDataSetHelper.AsJDOJSONArray: TJDOJsonArray;
begin
lSerializer := TMVCJsonDataObjectsSerializer.Create;
try
lSerializer.DataSetToJsonArray(Self, Result, ncLowerCase, []);
lSerializer.DataSetToJsonArray(Self, Result, FieldNameCase, []);
finally
lSerializer.Free;
end;
Expand Down Expand Up @@ -243,15 +244,15 @@ function TDataSetHelper.AsJSONArrayOfValues: TJDOJsonArray;
end;
end;

function TDataSetHelper.AsJSONArray: string;
function TDataSetHelper.AsJSONArray(FieldNameCase: TMVCNameCase = ncLowerCase): string;
var
lSerializer: IMVCSerializer;
begin
Result := '[]';
if not Eof then
begin
lSerializer := TMVCJsonDataObjectsSerializer.Create;
Result := lSerializer.SerializeDataSet(Self, [], ncLowerCase);
Result := lSerializer.SerializeDataSet(Self, [], FieldNameCase);
end;
end;

Expand All @@ -260,18 +261,17 @@ function TDataSetHelper.AsJSONArrayString: string;
Result := AsJSONArray;
end;

function TDataSetHelper.AsJSONObject(AFieldNamePolicy: TFieldNamePolicy): string;
function TDataSetHelper.AsJSONObject(FieldNameCase: TMVCNameCase; const IgnoredFields: TArray<string>): string;
var
lSerializer: IMVCSerializer;
begin
lSerializer := TMVCJsonDataObjectsSerializer.Create;
Result := lSerializer.SerializeDataSetRecord(Self, [], ncAsIs);
// Mapper.DataSetToJSONObject(Self, JObj, false);
Result := lSerializer.SerializeDataSetRecord(Self, TMVCIgnoredList(IgnoredFields), FieldNameCase);
end;

function TDataSetHelper.AsJSONObjectString: string;
begin
Result := AsJSONObject(fpLowerCase);
Result := AsJSONObject(ncLowerCase);
end;

function TDataSetHelper.AsObject<T>(CloseAfterScroll: boolean): T;
Expand Down Expand Up @@ -307,26 +307,26 @@ function TDataSetHelper.AsObjectList<T>(CloseAfterScroll: boolean; OwnsObjects:
end;
end;

procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; AFieldNamePolicy: TFieldNamePolicy);
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: string; FieldNameCase: TMVCNameCase);
var
lSerializer: IMVCSerializer;
begin
Self.DisableControls;
try
lSerializer := TMVCJsonDataObjectsSerializer.Create;
lSerializer.DeserializeDataSet(AJSONArray, Self, nil, ncAsIs);
lSerializer.DeserializeDataSet(AJSONArray, Self, nil, FieldNameCase);
finally
Self.EnableControls;
end;
end;

procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy);
FieldNameCase: TMVCNameCase);
begin
AppendFromJSONArrayString(AJSONArrayString, AIgnoredFields, AFieldNamePolicy);
AppendFromJSONArrayString(AJSONArrayString, AIgnoredFields, FieldNameCase);
end;

procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; AFieldNamePolicy: TFieldNamePolicy);
procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; FieldNameCase: TMVCNameCase);
var
lSerializer: TMVCJsonDataObjectsSerializer;
lBookmark: TArray<Byte>;
Expand All @@ -336,7 +336,7 @@ procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; AFieldNamePol
try
lSerializer := TMVCJsonDataObjectsSerializer.Create;
try
lSerializer.JsonArrayToDataSet(AJSONArray, Self, nil, ncAsIs);
lSerializer.JsonArrayToDataSet(AJSONArray, Self, nil, FieldNameCase);
finally
lSerializer.Free;
end;
Expand All @@ -347,41 +347,42 @@ procedure TDataSetHelper.LoadFromJSONArray(AJSONArray: TJSONArray; AFieldNamePol
end;
end;

procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; AFieldNamePolicy: TFieldNamePolicy);
procedure TDataSetHelper.LoadFromJSONArrayString(AJSONArrayString: string; FieldNameCase: TMVCNameCase);
begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), AFieldNamePolicy);
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create(), FieldNameCase);
end;

procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy);
FieldNameCase: TMVCNameCase);
begin
LoadFromJSONArray(AJSONArrayString, AFieldNamePolicy);
LoadFromJSONArray(AJSONArrayString, FieldNameCase);
end;

procedure TDataSetHelper.AppendFromJSONArrayString(AJSONArrayString: string);
begin
AppendFromJSONArrayString(AJSONArrayString, TArray<string>.Create());
end;

procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; AIgnoredFields: TArray<string>;
AFieldNamePolicy: TFieldNamePolicy);
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject; const AIgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase);
begin
raise Exception.Create('Not Implemented');
// Mapper.JSONObjectToDataSet(AJSONObject, Self, AIgnoredFields, false,
// AFieldNamePolicy);
end;

procedure TDataSetHelper.LoadFromJSONObjectString(AJSONObjectString: string; AIgnoredFields: TArray<string>);
procedure TDataSetHelper.LoadFromJSONObjectString(const JSONObjectString: string; const IgnoredFields: TArray<string>;
const FieldNameCase: TMVCNameCase);
var
lSerializer: IMVCSerializer;
begin
lSerializer := TMVCJsonDataObjectsSerializer.Create;
lSerializer.DeserializeDataSetRecord(AJSONObjectString, Self, nil, ncAsIs);
lSerializer.DeserializeDataSetRecord(JSONObjectString, Self, TMVCIgnoredList(IgnoredFields), FieldNameCase);
end;

procedure TDataSetHelper.LoadFromJSONObject(AJSONObject: TJSONObject; AFieldNamePolicy: TFieldNamePolicy);
procedure TDataSetHelper.LoadFromJSONObject(const JSONObject: TJSONObject; const FieldNameCase: TMVCNameCase);
begin
LoadFromJSONObject(AJSONObject, TArray<string>.Create());
LoadFromJSONObject(JSONObject, TArray<string>.Create(), FieldNameCase);
end;

procedure TDataSetHelper.LoadFromJSONObjectString(AJSONObjectString: string);
Expand Down
3 changes: 1 addition & 2 deletions sources/MVCFramework.Serializer.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,10 @@ function NewCollectionHolder(const AList: TObject; const AMetaFiller: TProc<TMVC
function MakeResponseData(const AObject: TObject; const AMeta: TMVCStringDictionary = nil;
const AOwns: boolean = True): IMVCResponseData;


implementation

uses
MVCFramework.Serializer.JsonDataObjects,
MVCFramework.Serializer.Intf,
Data.FmtBcd,
MVCFramework.Nullables,
System.Generics.Defaults;
Expand Down
Loading

0 comments on commit 08f01e6

Please sign in to comment.