Skip to content

Commit

Permalink
https://github.com/danieleteti/delphimvcframework/issues/553
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Aug 12, 2022
1 parent f79f472 commit f8501a8
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 40 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ The current beta release is named 3.2.2-nitrogen. If you want to stay on the-edg
- ✅ Improved! Now `TMVCStaticFileMiddleware` is able to manage high-level criteria to show/hide/mask specific files in the documetn web root. Check [Issue 548](https://github.com/danieleteti/delphimvcframework/issues/548) and the updated sample `samples\middleware_staticfiles\` for more info.
- ✅ Improved! In case of multiple MVCPath, Swagger consider only the first one (Thanks to V. Ferri and our sponsors)
- ✅ Improved! In case of multiple MVCPath, Swagger consider only the first one (Thanks to V. Ferri and our sponsors)
- ⚡New! Mechanism to customize the JWT claims setup using the client request as suggested in [issue495](https://github.com/danieleteti/delphimvcframework/issues/495)
Expand All @@ -540,6 +540,8 @@ The current beta release is named 3.2.2-nitrogen. If you want to stay on the-edg
- ⚡ New! Added partitioning for `TMVCActiveRecord descendants` (more info ASAP)
- ✅ Dramatically improved all "JSON-To-DataSet" operations (1 order of magnitude c.a.). Thanks to [MPannier](https://github.com/MPannier) and [David Moorhouse](https://github.com/fastbike) for their detailed analysis - More info [here](https://github.com/danieleteti/delphimvcframework/issues/553).
- ✅ Improved! After a big refactoring (*"I love to delete code" -- cit. Daniele Teti*), support a new SQLGenerator is just 2 (two) methods away! Just as example, this is the current version of `TMVCSQLGeneratorPostgreSQL`
```delphi
Expand Down
110 changes: 78 additions & 32 deletions sources/MVCFramework.Serializer.Abstract.pas
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ TMVCAbstractSerializer = class Abstract(TInterfacedObject)
FConfig: TMVCConfig;
function GetRttiContext: TRttiContext;
function GetSerializationType(const AObject: TObject; const ADefaultValue: TMVCSerializationType = stDefault): TMVCSerializationType;
function GetNameCase(const AObject: TObject; const ADefaultValue: TMVCNameCase = ncAsIs): TMVCNameCase; overload;
function GetNameCase(const AComponent: TComponent; const ADefaultValue: TMVCNameCase = ncAsIs): TMVCNameCase; overload;
function GetDataType(const AOwner: TComponent; const AComponentName: string; const ADefaultValue: TMVCDataType): TMVCDataType;
function GetNameAs(const AOwner: TComponent; const AComponentName: string; const ADefaultValue: string): string;
function IsIgnoredAttribute(const AAttributes: TMVCIgnoredList; const AName: string): Boolean;
function IsIgnoredComponent(const AOwner: TComponent; const AComponentName: string): Boolean;
public
class function IsIgnoredAttribute(const AAttributes: TMVCIgnoredList; const AName: string): Boolean;
class function IsIgnoredComponent(const AOwner: TComponent; const AComponentName: string): Boolean;
class function GetNameCase(const AComponent: TComponent; const ADefaultValue: TMVCNameCase = ncAsIs): TMVCNameCase; overload;
class function GetNameCase(const AObject: TObject; const ADefaultValue: TMVCNameCase = ncAsIs): TMVCNameCase; overload;
class function GetNameAs(const AOwner: TComponent; const AComponentName: string; const ADefaultValue: string): string;
function GetTypeSerializers: TDictionary<PTypeInfo, IMVCTypeSerializer>;
procedure RegisterTypeSerializer(const ATypeInfo: PTypeInfo; AInstance: IMVCTypeSerializer);
function GetObjectTypeOfGenericList(const ATypeInfo: PTypeInfo; out ARttiType: TRttiType): Boolean; overload;
Expand Down Expand Up @@ -93,16 +93,26 @@ destructor TMVCAbstractSerializer.Destroy;
inherited Destroy;
end;

function TMVCAbstractSerializer.GetNameCase(const AObject: TObject; const ADefaultValue: TMVCNameCase): TMVCNameCase;
class function TMVCAbstractSerializer.GetNameCase(const AObject: TObject; const ADefaultValue: TMVCNameCase): TMVCNameCase;
var
ObjType: TRttiType;
Att: TCustomAttribute;
RTTIContext: TRttiContext;
begin
Result := ADefaultValue;
ObjType := GetRttiContext.GetType(AObject.ClassType);
for Att in ObjType.GetAttributes do
if Att is MVCNameCaseAttribute then
Exit(MVCNameCaseAttribute(Att).KeyCase);
RTTIContext := TRttiContext.Create;
try
ObjType := RTTIContext.GetType(AObject.ClassType);
for Att in ObjType.GetAttributes do
begin
if Att is MVCNameCaseAttribute then
begin
Exit(MVCNameCaseAttribute(Att).KeyCase);
end;
end;
finally
RTTIContext.Free;
end;
end;

function TMVCAbstractSerializer.GetDataType(const AOwner: TComponent; const AComponentName: string; const ADefaultValue: TMVCDataType)
Expand All @@ -124,39 +134,63 @@ function TMVCAbstractSerializer.GetDataType(const AOwner: TComponent; const ACom
end;
end;

function TMVCAbstractSerializer.GetNameAs(const AOwner: TComponent; const AComponentName, ADefaultValue: string): string;
class function TMVCAbstractSerializer.GetNameAs(const AOwner: TComponent; const AComponentName, ADefaultValue: string): string;
var
ObjType: TRttiType;
ObjFld: TRttiField;
Att: TCustomAttribute;
RTTIContext: TRttiContext;
begin
Result := ADefaultValue;
if Assigned(AOwner) then
begin
ObjType := GetRttiContext.GetType(AOwner.ClassType);
ObjFld := ObjType.GetField(AComponentName);
if Assigned(ObjFld) then
for Att in ObjFld.GetAttributes do
if Att is MVCNameAsAttribute then
Exit(MVCNameAsAttribute(Att).Name);
RTTIContext := TRttiContext.Create;
try
ObjType := RTTIContext.GetType(AOwner.ClassType);
ObjFld := ObjType.GetField(AComponentName);
if Assigned(ObjFld) then
begin
for Att in ObjFld.GetAttributes do
begin
if Att is MVCNameAsAttribute then
begin
Exit(MVCNameAsAttribute(Att).Name);
end;
end;
end;
finally
RTTIContext.Free;
end;
end;
end;

function TMVCAbstractSerializer.GetNameCase(const AComponent: TComponent; const ADefaultValue: TMVCNameCase): TMVCNameCase;
class function TMVCAbstractSerializer.GetNameCase(const AComponent: TComponent; const ADefaultValue: TMVCNameCase): TMVCNameCase;
var
ObjType: TRttiType;
ObjFld: TRttiField;
Att: TCustomAttribute;
RTTIContext: TRttiContext;
begin
Result := ADefaultValue;
if Assigned(AComponent) and Assigned(AComponent.Owner) then
begin
ObjType := GetRttiContext.GetType(AComponent.Owner.ClassType);
ObjFld := ObjType.GetField(AComponent.Name);
if Assigned(ObjFld) then
for Att in ObjFld.GetAttributes do
if Att is MVCNameCaseAttribute then
Exit(MVCNameCaseAttribute(Att).KeyCase);
RTTIContext := TRttiContext.Create;
try
ObjType := RTTIContext.GetType(AComponent.Owner.ClassType);
ObjFld := ObjType.GetField(AComponent.Name);
if Assigned(ObjFld) then
begin
for Att in ObjFld.GetAttributes do
begin
if Att is MVCNameCaseAttribute then
begin
Exit(MVCNameCaseAttribute(Att).KeyCase);
end;
end;
end;
finally
RTTIContext.Free;
end;
end;
end;

Expand Down Expand Up @@ -276,7 +310,7 @@ function TMVCAbstractSerializer.GetTypeSerializers: TDictionary<PTypeInfo, IMVCT
Result := FTypeSerializers;
end;

function TMVCAbstractSerializer.IsIgnoredAttribute(const AAttributes: TMVCIgnoredList; const AName: string): Boolean;
class function TMVCAbstractSerializer.IsIgnoredAttribute(const AAttributes: TMVCIgnoredList; const AName: string): Boolean;
var
I: Integer;
begin
Expand All @@ -286,21 +320,33 @@ function TMVCAbstractSerializer.IsIgnoredAttribute(const AAttributes: TMVCIgnore
Exit(True);
end;

function TMVCAbstractSerializer.IsIgnoredComponent(const AOwner: TComponent; const AComponentName: string): Boolean;
class function TMVCAbstractSerializer.IsIgnoredComponent(const AOwner: TComponent; const AComponentName: string): Boolean;
var
ObjType: TRttiType;
ObjFld: TRttiField;
Att: TCustomAttribute;
RTTIContext: TRttiContext;
begin
Result := False;
if Assigned(AOwner) then
begin
ObjType := GetRttiContext.GetType(AOwner.ClassType);
ObjFld := ObjType.GetField(AComponentName);
if Assigned(ObjFld) then
for Att in ObjFld.GetAttributes do
if Att is MVCDoNotSerializeAttribute then
Exit(True);
RTTIContext := TRttiContext.Create;
try
ObjType := RTTIContext.GetType(AOwner.ClassType);
ObjFld := ObjType.GetField(AComponentName);
if Assigned(ObjFld) then
begin
for Att in ObjFld.GetAttributes do
begin
if Att is MVCDoNotSerializeAttribute then
begin
Exit(True);
end;
end;
end;
finally
RTTIContext.Free;
end;
end;
end;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ interface
MVCFramework.Serializer.Intf,
MVCFramework.Serializer.Commons,
JsonDataObjects,
MVCFramework.Commons, MVCFramework.Serializer.JsonDataObjects;
MVCFramework.Commons,
MVCFramework.Serializer.JsonDataObjects;

type

Expand Down
Loading

0 comments on commit f8501a8

Please sign in to comment.