From ba3884f2da0589445efd46a0b319d9f0b5fe9630 Mon Sep 17 00:00:00 2001 From: Thulio Bittencourt Date: Fri, 9 Apr 2021 21:20:26 -0300 Subject: [PATCH] BugFix #01 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Falha no GetFieldsByType em condição única #1 --- src/Bind4D.Attributes.pas | 71 +++++++++++++++++++++++++++++++++ src/Bind4D.Interfaces.pas | 1 + src/Bind4D.Types.Delete.pas | 16 ++++++++ src/Bind4D.Types.Get.pas | 16 ++++++++ src/Bind4D.Types.Interfaces.pas | 1 + src/Bind4D.Types.Post.pas | 16 ++++++++ src/Bind4D.Types.Put.pas | 16 ++++++++ src/Bind4D.Utils.Rtti.pas | 29 +++++--------- src/Bind4D.pas | 13 +++++- 9 files changed, 158 insertions(+), 21 deletions(-) diff --git a/src/Bind4D.Attributes.pas b/src/Bind4D.Attributes.pas index 4920d1e..e9cacf1 100644 --- a/src/Bind4D.Attributes.pas +++ b/src/Bind4D.Attributes.pas @@ -11,6 +11,7 @@ interface fvNotNull = class(TCustomAttribute) private FMsg: String; + FKey : String; procedure SetMsg(const Value: String); public constructor Create(aMsg : String); @@ -22,6 +23,7 @@ fvNotNull = class(TCustomAttribute) FormDefault = class(TCustomAttribute) private FTitle: String; + FKey : String; procedure SetTitle(const Value: String); public constructor Create ( aTitle : String = '' ); @@ -34,6 +36,7 @@ FormRest = class(TCustomAttribute) FEndPoint: String; FOrder: String; FSort: String; + FKey : String; procedure SetEndPoint(const Value: String); procedure SetPK(const Value: String); procedure SetOrderBy(const Value: String); @@ -52,6 +55,7 @@ S3Storage = class(TCustomAttribute) private FFileExtension: String; FContentType: String; + FKey : String; procedure SetContentType(const Value: String); procedure SetFileExtension(const Value: String); public @@ -63,6 +67,7 @@ S3Storage = class(TCustomAttribute) Translation = class(TCustomAttribute) private FQuery: String; + FKey : String; procedure SetQuery(const Value: String); public constructor Create(aQuery : String); @@ -73,6 +78,10 @@ Translation = class(TCustomAttribute) {$region 'Components Attributes'} AdjustResponsive = class(TCustomAttribute) + private + FKey : String; + public + constructor Create; end; ImageAttribute = class(TCustomAttribute) @@ -80,6 +89,7 @@ ImageAttribute = class(TCustomAttribute) FDefaultResourceImage: String; FWidth: Integer; FHeigth: Integer; + FKey : String; procedure SetDefaultResourceImage(const Value: String); procedure SetHeigth(const Value: Integer); procedure SetWidth(const Value: Integer); @@ -97,6 +107,7 @@ ComponentBindStyle = class(TCustomAttribute) FFontColor: TColor; FFontName: String; FEspecialType: TEspecialType; + FKey : String; procedure SetColor(const Value: TColor); procedure SetFontColor(const Value: TColor); procedure SetFontSize(const Value: Integer); @@ -114,6 +125,7 @@ ComponentBindStyle = class(TCustomAttribute) ComponentBindFormat = class(TCustomAttribute) private FEspecialType: TEspecialType; + FKey : String; procedure SetEspecialType(const Value: TEspecialType); public constructor Create(aEspecialType : TEspecialType = teNull); @@ -125,6 +137,7 @@ ComponentBindFormat = class(TCustomAttribute) FieldJsonBind = class(TCustomAttribute) private FJsonName: string; + FKey : String; procedure SetJsonName(const Value: string); public constructor Create(aJsonName: string); @@ -132,15 +145,31 @@ FieldJsonBind = class(TCustomAttribute) end; FbIgnorePut = class(TCustomAttribute) + private + FKey : String; + public + constructor Create; end; FbIgnorePost = class(TCustomAttribute) + private + FKey : String; + public + constructor Create; end; FbIgnoreDelete = class(TCustomAttribute) + private + FKey : String; + public + constructor Create; end; FbIgnoreGet = class(TCustomAttribute) + private + FKey : String; + public + constructor Create; end; {$endregion} @@ -155,6 +184,7 @@ FieldDataSetBind = class(TCustomAttribute) FAlignment: TAlignment; FEditMask: String; FFLimitWidth: Integer; + FKey : String; procedure SetFieldName(const Value: String); procedure SetDisplayName(const Value: String); procedure SetWidth(const Value: Integer); @@ -179,11 +209,14 @@ FieldDataSetBind = class(TCustomAttribute) implementation +uses + System.SysUtils; { fvNotNull } constructor fvNotNull.Create(aMsg: String); begin FMsg := aMsg; + FKey := TGuid.NewGuid.ToString; end; procedure fvNotNull.SetMsg(const Value: String); @@ -196,6 +229,7 @@ procedure fvNotNull.SetMsg(const Value: String); constructor FormDefault.Create(aTitle: String); begin FTitle := aTitle; + FKey := TGuid.NewGuid.ToString; end; procedure FormDefault.SetTitle(const Value: String); @@ -211,6 +245,7 @@ constructor FormRest.Create(aEndPoint, aPK, aSort, aOrder: String); FPK := aPK; FOrder := aOrder; FSort := aSort; + FKey := TGuid.NewGuid.ToString; end; procedure FormRest.SetEndPoint(const Value: String); @@ -238,6 +273,7 @@ procedure FormRest.SetSort(const Value: String); constructor Translation.Create(aQuery : String); begin FQuery := aQuery; + FKey := TGuid.NewGuid.ToString; end; procedure Translation.SetQuery(const Value: String); @@ -251,6 +287,7 @@ constructor S3Storage.Create(aFileExtension : String; aContentType : String); begin FFileExtension := aFileExtension; FContentType := aContentType; + FKey := TGuid.NewGuid.ToString; end; procedure S3Storage.SetContentType(const Value: String); @@ -268,6 +305,7 @@ procedure S3Storage.SetFileExtension(const Value: String); constructor ComponentBindFormat.Create(aEspecialType: TEspecialType); begin FEspecialType := aEspecialType; + FKey := TGuid.NewGuid.ToString; end; procedure ComponentBindFormat.SetEspecialType(const Value: TEspecialType); @@ -285,6 +323,7 @@ constructor ComponentBindStyle.Create(aColor: TColor; aFontSize: Integer; FFontColor := aFontColor; FFontName := aFontName; FEspecialType := aEspecialType; + FKey := TGuid.NewGuid.ToString; end; procedure ComponentBindStyle.SetColor(const Value: TColor); @@ -319,6 +358,7 @@ constructor ImageAttribute.Create( aDefaultResourceImage : String; aWidth : Inte FDefaultResourceImage := aDefaultResourceImage; FWidth := aWidth; FHeigth := aHeigth; + FKey := TGuid.NewGuid.ToString; end; procedure ImageAttribute.SetDefaultResourceImage(const Value: String); @@ -341,6 +381,7 @@ procedure ImageAttribute.SetWidth(const Value: Integer); constructor FieldJsonBind.Create(aJsonName: string); begin FJsonName := aJsonName; + FKey := TGuid.NewGuid.ToString; end; procedure FieldJsonBind.SetJsonName(const Value: string); @@ -362,6 +403,7 @@ constructor FieldDataSetBind.Create(aFieldName: String; aFdType: TFieldType; FEditMask := aEditMask; FFdType := aFdType; FLimitWidth := aLimitWidth; + FKey := TGuid.NewGuid.ToString; end; procedure FieldDataSetBind.SetAlignment(const Value: TAlignment); @@ -404,4 +446,33 @@ procedure FieldDataSetBind.SetWidth(const Value: Integer); FWidth := Value; end; +{ AdjustResponsive } + +constructor FbIgnorePut.Create; +begin + FKey := TGuid.NewGuid.ToString; +end; + +constructor AdjustResponsive.Create; +begin + FKey := TGuid.NewGuid.ToString; +end; + +constructor FbIgnorePost.Create; +begin + FKey := TGuid.NewGuid.ToString; +end; + +{ FbIgnoreGet } + +constructor FbIgnoreGet.Create; +begin + FKey := TGuid.NewGuid.ToString; +end; + +constructor FbIgnoreDelete.Create; +begin + FKey := TGuid.NewGuid.ToString; +end; + end. diff --git a/src/Bind4D.Interfaces.pas b/src/Bind4D.Interfaces.pas index 26562c4..e3d0901 100644 --- a/src/Bind4D.Interfaces.pas +++ b/src/Bind4D.Interfaces.pas @@ -40,6 +40,7 @@ interface function SetStyleComponents : iBind4D; function SetCaptionComponents : iBind4D; function SetImageComponents : iBind4D; + function ClearCacheComponents : iBind4D; function Translator : iTranslator4D; function AWSService : iAWS4D; end; diff --git a/src/Bind4D.Types.Delete.pas b/src/Bind4D.Types.Delete.pas index 3b2c577..098dbf2 100644 --- a/src/Bind4D.Types.Delete.pas +++ b/src/Bind4D.Types.Delete.pas @@ -13,6 +13,7 @@ TBind4DTypesDelete = class(TInterfacedObject, iBind4DTypesInterface) constructor Create; destructor Destroy; override; class function New : iBind4DTypesInterface; + function TryGetJsonName(aComponent : TComponent; out aJsonName : String ) : Boolean; function GetJsonName (aComponent : TComponent) : String; procedure TryAddJsonPair( aComponent : TComponent; var aJsonObject : TJsonObject); end; @@ -61,4 +62,19 @@ procedure TBind4DTypesDelete.TryAddJsonPair(aComponent: TComponent; aJsonObject.AddPair(aAttrJson.JsonName, TBind4DComponentUtils.GetValueString(aComponent)); end; +function TBind4DTypesDelete.TryGetJsonName(aComponent: TComponent; + out aJsonName: String): Boolean; +var + aAttrIg : FbIgnoreDelete; + aAttrFJBind : FieldJsonBind; +begin + Result := False; + if not RttiUtils.TryGet(aComponent, aAttrIg) then + if RttiUtils.TryGet(aComponent, aAttrFJBind) then + begin + Result := True; + aJsonName := aAttrFJBind.JsonName; + end; +end; + end. diff --git a/src/Bind4D.Types.Get.pas b/src/Bind4D.Types.Get.pas index 5997402..57bca16 100644 --- a/src/Bind4D.Types.Get.pas +++ b/src/Bind4D.Types.Get.pas @@ -14,6 +14,7 @@ TBind4DTypesGet = class(TInterfacedObject, iBind4DTypesInterface) destructor Destroy; override; class function New : iBind4DTypesInterface; function GetJsonName (aComponent : TComponent) : String; + function TryGetJsonName(aComponent : TComponent; out aJsonName : String ) : Boolean; procedure TryAddJsonPair( aComponent : TComponent; var aJsonObject : TJsonObject); end; @@ -59,4 +60,19 @@ procedure TBind4DTypesGet.TryAddJsonPair(aComponent: TComponent; aJsonObject.AddPair(aAttrFJBind.JsonName, TBind4DComponentUtils.GetValueString(aComponent)); end; +function TBind4DTypesGet.TryGetJsonName(aComponent: TComponent; + out aJsonName: String): Boolean; +var + aAttrIgGet : FbIgnoreGet; + aAttrFJBind : FieldJsonBind; +begin + Result := False; + if not RttiUtils.TryGet(aComponent, aAttrIgGet) then + if RttiUtils.TryGet(aComponent, aAttrFJBind) then + begin + Result := True; + aJsonName := aAttrFJBind.JsonName; + end; +end; + end. diff --git a/src/Bind4D.Types.Interfaces.pas b/src/Bind4D.Types.Interfaces.pas index 20cf036..8d4a143 100644 --- a/src/Bind4D.Types.Interfaces.pas +++ b/src/Bind4D.Types.Interfaces.pas @@ -9,6 +9,7 @@ interface iBind4DTypesInterface = interface ['{EA313825-7246-4171-8E8C-FCA8C5B54880}'] function GetJsonName (aComponent : TComponent) : String; + function TryGetJsonName(aComponent : TComponent; out aJsonName : String ) : Boolean; procedure TryAddJsonPair( aComponent : TComponent; var aJsonObject : TJsonObject); end; diff --git a/src/Bind4D.Types.Post.pas b/src/Bind4D.Types.Post.pas index f2912a7..2bd3bf0 100644 --- a/src/Bind4D.Types.Post.pas +++ b/src/Bind4D.Types.Post.pas @@ -14,6 +14,7 @@ TBind4DTypesPost = class(TInterfacedObject, iBind4DTypesInterface) destructor Destroy; override; class function New : iBind4DTypesInterface; function GetJsonName (aComponent : TComponent) : String; + function TryGetJsonName(aComponent : TComponent; out aJsonName : String ) : Boolean; procedure TryAddJsonPair( aComponent : TComponent; var aJsonObject : TJsonObject); end; @@ -61,4 +62,19 @@ procedure TBind4DTypesPost.TryAddJsonPair(aComponent: TComponent; aJsonObject.AddPair(aAttrJson.JsonName, TBind4DComponentUtils.GetValueString(aComponent)); end; +function TBind4DTypesPost.TryGetJsonName(aComponent: TComponent; + out aJsonName: String): Boolean; +var + aAttrIg : FbIgnorePost; + aAttrFJBind : FieldJsonBind; +begin + Result := False; + if not RttiUtils.TryGet(aComponent, aAttrIg) then + if RttiUtils.TryGet(aComponent, aAttrFJBind) then + begin + Result := True; + aJsonName := aAttrFJBind.JsonName; + end; +end; + end. diff --git a/src/Bind4D.Types.Put.pas b/src/Bind4D.Types.Put.pas index 3b85b1f..be23585 100644 --- a/src/Bind4D.Types.Put.pas +++ b/src/Bind4D.Types.Put.pas @@ -13,6 +13,7 @@ TBind4DTypesPut = class(TInterfacedObject, iBind4DTypesInterface) constructor Create; destructor Destroy; override; class function New : iBind4DTypesInterface; + function TryGetJsonName(aComponent : TComponent; out aJsonName : String ) : Boolean; function GetJsonName (aComponent : TComponent) : String; procedure TryAddJsonPair( aComponent : TComponent; var aJsonObject : TJsonObject); end; @@ -61,4 +62,19 @@ procedure TBind4DTypesPut.TryAddJsonPair(aComponent: TComponent; aJsonObject.AddPair(aAttrJson.JsonName, TBind4DComponentUtils.GetValueString(aComponent)); end; +function TBind4DTypesPut.TryGetJsonName(aComponent: TComponent; + out aJsonName: String): Boolean; +var + aAttrIg : FbIgnorePut; + aAttrFJBind : FieldJsonBind; +begin + Result := False; + if not RttiUtils.TryGet(aComponent, aAttrIg) then + if RttiUtils.TryGet(aComponent, aAttrFJBind) then + begin + Result := True; + aJsonName := aAttrFJBind.JsonName; + end; +end; + end. diff --git a/src/Bind4D.Utils.Rtti.pas b/src/Bind4D.Utils.Rtti.pas index 0b2e07a..05a9fb0 100644 --- a/src/Bind4D.Utils.Rtti.pas +++ b/src/Bind4D.Utils.Rtti.pas @@ -1,7 +1,5 @@ unit Bind4D.Utils.Rtti; - interface - uses {$IFDEF HAS_FMX} FMX.Forms, @@ -11,46 +9,46 @@ interface System.Classes, System.Rtti, System.Generics.Collections; - type - TBind4DUtilsRtti = class private FComponentList : TDictionary; FprpRttiList : TDictionary; public - constructor Create; + constructor Create; destructor Destroy; override; function Get(aForm : TForm) : TArray; function GetAttClass(aForm : TForm) : TArray; function GetComponent(aAttribute : TCustomAttribute) : TComponent; function TryGet(aComponent : TComponent; out Attribute : T) : Boolean; function GetComponents(aForm : TForm) : TArray; + function ClearCache : TBind4DUtilsRtti; end; - var RttiUtils : TBind4DUtilsRtti; - -implementation +implementation uses Bind4D.Helpers; - { TBind4DUtilsRtti } +function TBind4DUtilsRtti.ClearCache: TBind4DUtilsRtti; +begin + Result := Self; + FComponentList.Clear; + FprpRttiList.Clear; +end; constructor TBind4DUtilsRtti.Create; begin FComponentList := TDictionary.Create; FprpRttiList := TDictionary.Create; end; - destructor TBind4DUtilsRtti.Destroy; begin FprpRttiList.Free; FComponentList.Free; inherited; end; - function TBind4DUtilsRtti.Get(aForm : TForm) : TArray; var ctxRtti : TRttiContext; @@ -67,10 +65,8 @@ function TBind4DUtilsRtti.Get(aForm : TForm) : TArray; for prpRtti in typRtti.GetFields do begin aComponent := aForm.FindComponent(prpRtti.Name); - if not FprpRttiList.TryGetValue(aComponent, vprpRtti) then FprpRttiList.Add(aComponent, prpRtti); - if prpRtti.Tem then begin Inc(dec); @@ -84,7 +80,6 @@ function TBind4DUtilsRtti.Get(aForm : TForm) : TArray; ctxRtti.Free; end; end; - function TBind4DUtilsRtti.GetAttClass(aForm: TForm): TArray; var vCtxRtti: TRttiContext; @@ -105,13 +100,11 @@ function TBind4DUtilsRtti.GetAttClass(aForm: TForm): TArray; vCtxRtti.Free; end; end; - function TBind4DUtilsRtti.GetComponent( aAttribute: TCustomAttribute): TComponent; begin FComponentList.TryGetValue(aAttribute, Result); end; - function TBind4DUtilsRtti.GetComponents(aForm: TForm): TArray; var ctxRtti : TRttiContext; @@ -133,7 +126,6 @@ function TBind4DUtilsRtti.GetComponents(aForm: TForm): TArray; ctxRtti.Free; end; end; - function TBind4DUtilsRtti.TryGet(aComponent : TComponent; out Attribute : T) : Boolean; var prpRtti: TRttiField; @@ -146,11 +138,8 @@ function TBind4DUtilsRtti.TryGet(aComponent : TComponent; out Attribute : T) Result := not (Attribute = nil); end; end; - initialization RttiUtils := TBind4DUtilsRtti.Create; - finalization RttiUtils.Free; - end. diff --git a/src/Bind4D.pas b/src/Bind4D.pas index ad8e420..dd2acd2 100644 --- a/src/Bind4D.pas +++ b/src/Bind4D.pas @@ -72,6 +72,7 @@ TBind4D = class(TInterfacedObject, iBind4D) function SetStyleComponents : iBind4D; function SetCaptionComponents : iBind4D; function SetImageComponents : iBind4D; + function ClearCacheComponents : iBind4D; function Translator : iTranslator4D; function AWSService : iAWS4D; end; @@ -266,6 +267,12 @@ function TBind4D.BindFormDefault(var aTitle : String) : iBind4D; aTitle := aAttr.Title; end; +function TBind4D.ClearCacheComponents: iBind4D; +begin + Result := Self; + RttiUtils.ClearCache; +end; + function TBind4D.ClearFieldForm: iBind4D; var aComp : TComponent; @@ -298,6 +305,8 @@ function TBind4D.Form( aValue : TForm) : iBind4D; Self.ResponsiveAdjustment; end ); + + end; function TBind4D.FormToJson(aType : TTypeBindFormJson) : TJsonObject; @@ -319,9 +328,11 @@ function TBind4D.FormToJson(aType : TTypeBindFormJson) : TJsonObject; function TBind4D.GetFieldsByType(aType : TTypeBindFormJson) : String; var aAttr : FieldJsonBind; + aJsonName : string; begin for aAttr in RttiUtils.Get(FForm) do - Result := Result + aType.This.GetJsonName(aAttr.Component) + ','; + if aType.This.TryGetJsonName(aAttr.Component, aJsonName) then + Result := Result + aJsonName + ','; Result := Copy(Result, 1, Length(Result) -1); end;