Skip to content

Commit

Permalink
enhanced TSynInvokeableVariantType to iterate over dvObject values
Browse files Browse the repository at this point in the history
used e.g. for Mustache
  • Loading branch information
Arnaud Bouchez committed Oct 19, 2022
1 parent e8e156e commit 59aa002
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/core/mormot.core.mustache.pas
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ procedure TSynMustacheContextVariant.PushContext(const aDoc: TVarData);
ListCount := -1
else
begin
ListCount := DocumentType.IterateCount(aDoc);
ListCount := DocumentType.IterateCount(aDoc, {GetObjectAsValues=}true);
if fContextCount = 0 then
ListCurrentDocument := aDoc; // allow {#.}...{/.} at first level
end;
Expand Down
23 changes: 14 additions & 9 deletions src/core/mormot.core.variants.pas
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ TSynInvokeableVariantType = class(TInvokeableVariantType)
// - if the document is an array, will return the items count (0 meaning
// void array) - used e.g. by TSynMustacheContextVariant
// - this default implementation will return -1 (meaning this is not an array)
// - overridden method could implement it, e.g. for TDocVariant of kind dvArray
function IterateCount(const V: TVarData): integer; virtual;
// - overridden method could implement it, e.g. for TDocVariant of kind
// dvArray - or dvObject (ignoring names) if GetObjectAsValues is true
function IterateCount(const V: TVarData; GetObjectAsValues: boolean): integer; virtual;
/// allow to loop over an array document
// - Index should be in 0..IterateCount-1 range
// - this default implementation will do nothing
Expand Down Expand Up @@ -731,7 +732,8 @@ TDocVariant = class(TSynInvokeableVariantType)
// - if the document is an array, will return the items count (0 meaning
// void array) - used e.g. by TSynMustacheContextVariant
// - this overridden method will implement it for dvArray instance kind
function IterateCount(const V: TVarData): integer; override;
function IterateCount(const V: TVarData;
GetObjectAsValues: boolean): integer; override;
/// allow to loop over an array document
// - Index should be in 0..IterateCount-1 range
// - this default implementation will do handle dvArray instance kind
Expand Down Expand Up @@ -3372,7 +3374,8 @@ constructor TSynInvokeableVariantType.Create;
inherited Create; // call RegisterCustomVariantType(self)
end;

function TSynInvokeableVariantType.IterateCount(const V: TVarData): integer;
function TSynInvokeableVariantType.IterateCount(const V: TVarData;
GetObjectAsValues: boolean): integer;
begin
result := -1; // this is not an array
end;
Expand Down Expand Up @@ -3915,11 +3918,14 @@ function TDocVariant.IntSet(const Instance, Value: TVarData;
dv.InternalSetValue(ndx, variant(Value));
end;

function TDocVariant.IterateCount(const V: TVarData): integer;
function TDocVariant.IterateCount(const V: TVarData;
GetObjectAsValues: boolean): integer;
var
Data: TDocVariantData absolute V;
begin
if Data.IsArray then
if Data.IsArray or
(GetObjectAsValues and
Data.IsObject) then
result := Data.VCount
else
result := -1;
Expand All @@ -3929,9 +3935,8 @@ procedure TDocVariant.Iterate(var Dest: TVarData;
const V: TVarData; Index: integer);
var
Data: TDocVariantData absolute V;
begin
if Data.IsArray and
(cardinal(Index) < cardinal(Data.VCount)) then
begin // note: IterateCount() may accept IsObject values[]
if cardinal(Index) < cardinal(Data.VCount) then
Dest := TVarData(Data.VValue[Index])
else
TRttiVarData(Dest).VType := varEmpty;
Expand Down
2 changes: 1 addition & 1 deletion src/mormot.commit.inc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
'2.0.4217'
'2.0.4218'
6 changes: 4 additions & 2 deletions src/orm/mormot.orm.base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,8 @@ TOrmTableRowVariant = class(TSynInvokeableVariantType)
procedure CastTo(var Dest: TVarData; const Source: TVarData;
const AVarType: TVarType); override;
/// return the number of TOrmTable rows to browse
function IterateCount(const V: TVarData): integer; override;
function IterateCount(const V: TVarData;
GetObjectAsValues: boolean): integer; override;
/// allow to loop over the mapped TOrmTable rows
procedure Iterate(var Dest: TVarData; const V: TVarData;
Index: integer); override;
Expand Down Expand Up @@ -10231,7 +10232,8 @@ procedure TOrmTableRowVariant.ToJson(W: TJsonWriter; Value: PVarData);
W.AddVariant(tmp, twJsonEscape);
end;

function TOrmTableRowVariant.IterateCount(const V: TVarData): integer;
function TOrmTableRowVariant.IterateCount(const V: TVarData;
GetObjectAsValues: boolean): integer;
begin
result := TOrmTableRowVariantData(V).VTable.fRowCount;
end;
Expand Down

0 comments on commit 59aa002

Please sign in to comment.