#1 2026-01-03 17:39:50

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 495

Include a check if a record type is registered

Is it possible to have a function in mormot.core.base to use in assertions for checking if a record type is registered in rtti?
Perhaps something like the following:

function IsRecordProperlyRegistered(TypeInfo: PRttiInfo): Boolean;
var RttiCustom: TRttiCustom;
begin
  Result := False;
  if TypeInfo = nil then Exit;
   
  RttiCustom := Rtti.FindType(TypeInfo);
  Result := (RttiCustom <> nil) and (rcfHasNestedProperties in RttiCustom.Flags);
end;

Thank you in advance

Offline

#2 2026-01-04 18:52:59

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 495

Re: Include a check if a record type is registered

Also the following code from Claude check if you change the field order in the definition of the record comparing with the definition from TRttiJson.RegisterFromText(
https://gist.github.com/dkounal/2d64c97 … 853cd30388
It runs in delphi to check if you are going to use it with fpc 3.2.x that does not have rtti for records

After each TRttiJson.RegisterFromText I have something like the following.
{$ifndef fpc}{$ifdef DEBUG} s:=AutoCheckRecordRegistration(TypeInfo(Dvname)); Assert(s='',utf8tostring(s)); {$endif}{$endif}
I see it as a kind of test that runs to find mismatches.
I believe it will be helpful to be added to the TRttiJson.RegisterFromText as additional argument or option.
Thank you in advance

Last edited by dcoun (2026-01-04 19:07:36)

Offline

#3 2026-01-05 08:42:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,364
Website

Re: Include a check if a record type is registered

I don't get the interest of AutoValidateRecordOffsets() if the field names are checked to be equal. If they are equal, just use the RTTI.
There is already some checks at registering, especially about the whole record field size. We may enhance them, but sometimes the exact field types are not expected to match, on purpose (e.g. defining PUtf8Char on a RawUtf8 field for faster process, or when fields are grouped into another nested record).

About HasCompilerRTTI() it is funny how it is overcomplicated.
Just call (PRttiInfo(TypeInfo(TSomeRecord)).RecordAllFields <> nil) and it is done.

About your other function, it won't work on Delphi 2010+ or FPC trunk with extended RTTI as we could expect, since FindType() would return nil, but there are some fields information available.
Here is my proposal (with tests):
https://github.com/synopse/mORMot2/commit/c803e4d92

Offline

#4 2026-01-05 13:05:38

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 495

Re: Include a check if a record type is registered

Thank you a lot @ab. I usually use the Delphi IDE to write code and to test as I am more familiar and with cntools it is really very productive. With Claude, I do minimal things for the moment
I compile to both Delphi and FPC and with Delphi I can check if everything is OK with these definitions.

AutoValidateRecordOffsets checks if the order of the fields are the same in the record definition and in the text definition in TRttiJson.RegisterFromText
If I understand well, the order is crucial for field mapping, or not?

Offline

#5 2026-01-05 13:39:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,364
Website

Re: Include a check if a record type is registered

Read again my previous answer.

Offline

#6 2026-01-05 17:23:57

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 495

Re: Include a check if a record type is registered

I did not understand. Perhaps I did not explain it well. It is the following (that gives exception in recordsavejson), I am trying to avoid:

type
  Dtest=packed record
    id:integer;
    lab:rawutf8;
    i64:int64;
    op:double;
    chk:boolean;
    b:byte;
    f:tdatetime;
    ed:integer;
  end;

procedure TForm3.Button1Click(Sender: TObject);
var a:dtest; s:rawutf8;
begin a.id:=1111111; a.lab:='a testing'; a.i64:=13234 shl 23; a.op:=pi;
a.chk:=true; a.b:=136; a.f:=now; a.ed:=222222;
s:=RecordSaveJson(a,typeinfo(dtest));
showmessage(utf8tostring(s));
end;

initialization
TRttiJson.RegisterFromText(TypeInfo(Dtest),'f:tdatetime id:integer b:byte lab:rawutf8 chk:boolean ed:integer i64:int64 op:double',[jpoIgnoreUnknownProperty, jpoIgnoreUnknownEnum],[woDontStoreDefault]);

Offline

Board footer

Powered by FluxBB