#1 2021-12-13 08:45:48

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 44

Problem with the function "GetSetNameValue" in mORMot2

Good day. I found the following problem when working with sets. Change a directive VER2 and see result.

program Project;

{$APPTYPE CONSOLE}

{$DEFINE VER2}

uses
  SysUtils
  {$IFnDEF VER2}
  ,SynCommons
  {$ELSE}
  ,mormot.core.base, mormot.core.json
  {$ENDIF}
  ;

type
  TMyEnum = (enOne, enTwo, enThree, enFour, enFive);
  TMyEnumPart =enTwo..enFour;

  TSetMyEnumPart = set of TMyEnumPart;


var pEnum:PUTF8Char; s:RawUTF8;endofobject:Char;

begin
  s:='["enTwo"]';
  pEnum := PUTF8Char(s);
  GetSetNameValue(TypeInfo(TSetMyEnumPart), pEnum, endofobject);
  if pEnum <> nil then
    Writeln('Error')
  else
    Writeln('Success');
  readln;  
end.

Last edited by Alek (2021-12-13 08:47:08)

Offline

#2 2021-12-13 10:06:50

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Problem with the function "GetSetNameValue" in mORMot2

What is documented is that it should follow GetJsonField() behavior, which is to point to the ending #0, not to return nil, in mORMot 2.

In fact, there was a change in respect to mORMot 1, in GetJsonField() returned PDest.
In mORMot 2, it results PDest <> nil, and pointing to ending #0.
And therefore, GetSetNameValue() behaves just as GetJsonField() does now.
So you are right, there was something changed.

I have just modified GetSetNameValue() so that it behaves like in mORMot 1.
But I won't be able to change GetJSonField() behavior, because it would break a lot of new code...

Offline

#3 2021-12-13 11:19:34

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 44

Re: Problem with the function "GetSetNameValue" in mORMot2

Thanks for the clarification. But when I ran your a new test, I get an error. I am using BDS 2006.

Offline

#4 2021-12-13 11:58:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Problem with the function "GetSetNameValue" in mORMot2

In Delphi, it seems that the RTTI is not generated as expected.

With FPC, TMyEnumPart has a dedicated RTTI with proper values.
Whereas on Delphi, the RTTI is incorrect: it points to TMyEnum.

So even if on mORMot 1, it did compile and run, but the value returned by GetSetNameValue() was incorrect, because it pointed on TMyEnum and did not take into account the MinValue index.

My recommendation is to NOT use partial types like TMyEnumPart =enTwo..enFour.

Offline

#5 2021-12-13 13:25:19

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 44

Re: Problem with the function "GetSetNameValue" in mORMot2

Ok. I will be use

GetSetNameValue(TypeInfo(TSetMyEnum), pEnum, endofobject);

instead TypeInfo(TSetMyEnumPart), where TSetMyEnum = set of TMyEnum;

And all passes.

Offline

#6 2021-12-13 18:40:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Problem with the function "GetSetNameValue" in mORMot2

I have fixed the RTTI problem with Delphi.

It should work now with both TSetMyENum and TSetMyEnumPart types.

Offline

#7 2021-12-14 06:08:33

Alek
Member
From: Russia
Registered: 2014-07-04
Posts: 44

Re: Problem with the function "GetSetNameValue" in mORMot2

Yes, it is. Everything works perfectly in Delphi and Lazarus

Offline

Board footer

Powered by FluxBB