#1 2019-08-09 12:46:19

jaclas
Member
Registered: 2014-09-12
Posts: 215

Enumerated types in method parameter

I use interface based service. The methods has parameters in the form of an array of records, some of the record fields are an enumerated type. When the client communicates with the server, the call parameters are converted to JSON, and the enumerated type are converted/casted to Int32.

Is it possible to set the server or service so that the parameters of the enumerated types are converted to a text string?

So e.g. instead of ftString => 1 to be ftString => 'ftString'

Offline

#2 2019-08-10 04:33:56

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Enumerated types in method parameter

I believe converting and transferring the enum values in their string presentations would significantly reduce the performance.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#3 2019-08-10 10:56:34

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Enumerated types in method parameter

I suppose that something (depending on your expectations) can be done with TEnumType (mORMot unit). There is a batch of pretty functions for working with string values of enumerated types.
As a sample of using you can look at TSQLTable.ExpandAsString function f.ex.

I'm using ExpandAsString in my projects and I didn't see any additional visible reduce of performance with enumerated types, even with big data tables. I admit that working with integer values is much faster, but I think that for most everyday tasks such an economy doesn't worth efforts.
Besides, jaclas asked the question "Is it possible ... ?", and not about performance wink

Offline

#4 2019-08-10 11:13:50

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Enumerated types in method parameter

@Vitaly,
Thanks for your comments.

It's good to know about the useful `TEnumType` type and your real-life experience with it.

But If I understand the OP correctly, he wants the parameters of type enum of an interface-based service to be transfer by the framework in the format of strings instead of integer values, as far as I know it's not possible?

I just checked the "Service Methods Parameters” chapter of the document, it states that all enum values are being transferred as JSON numbers, not sure if there is an option to configure/change that.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#5 2019-08-10 11:39:45

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Enumerated types in method parameter

I don't know details of the task - that's why I wrote: "depending on your expectations".
As well as don't know, why working with strings can be better than enumerated for some task smile But situations may be different and unexpected.
If jaclas wants string parameter instead of enumerated in some method, I don't see any problem to use it. For example, converting with TEnumType to string on some side (client or server, depending on needs) seems to me to be a rather simple and fastly implemented decision. How do you think?

I'm asking because I'm interested in everything concerning enumerated types in mORMot (or Delphi/FPC) - there are more and more of them in my projects, lately. And I want to use all options wink

Offline

#6 2019-08-10 11:55:30

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Enumerated types in method parameter

Thanks for replies...
I don't think that the transfer of parameters of the enumerated type is noticeably slower, although it is to be tested.
But I personally see three advantages of this enumeration type encoding:
- makes debugging services easier
- makes it easier to connect foreign clients to the server (in other languages, e.g. JS)
- eliminates the problem of extending the type in the future by new values (then e.g. ftTable = 5 soon becomes ftTable = 10 and all client code needs to be corrected)

@Vitaly for enumerated types, see Spring4D and TEnum type :-)

Offline

#7 2019-08-10 12:09:11

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Enumerated types in method parameter

jaclas wrote:

- eliminates the problem of extending the type in the future by new values (then e.g. ftTable = 5 soon becomes ftTable = 10 and all client code needs to be corrected)

You can use directly pointed values, so it will stay fixed until you change it. Like:

  TRegionLevel = (rlCountry = 0, rlCounty = 1, rlMunicipality = 2, rlBSU = 3);
jaclas wrote:

@Vitaly for enumerated types, see Spring4D and TEnum type :-)

I will, thanks! wink

Offline

#8 2019-08-10 12:35:49

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Enumerated types in method parameter

I try, like you suggest, enumerated type with assigned integer value:

  TTest = (ttOne = 1, ttTen = 10, ttHund = 100);

then I set two field to ttTen and ttHund value, now I get strange values in transport JSON (coded by mORMot):

{"Test":"0A00000000000000"},{"Test":"6400000000000000"}

So this way is blind :-)

Offline

#9 2019-08-10 13:23:54

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

Re: Enumerated types in method parameter

There is no RTTI generated by the compiler for such uncontiguous enumerated types, so they are not properly serialized.

About enums serialization as string, it is not possible yet for method parameter - but it is for record serialization IIRC.
Note that if you add some items in the middle of the enumerate, it will break ORM persistence - so as a general advice, new items are to appended at the end, not in the middle of the enumerate.

Offline

#10 2019-08-10 13:31:31

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Enumerated types in method parameter

Maybe it happens because of the order of values, so it becomes not traditional enumeration. Try standard numeration, beginning with 0:

 TTest = (ttOne = 0, ttTen = 1, ttHund = 2); 

So it will be the same as

 TTest = (ttOne, ttTen, ttHund); 

My idea from the above was just to avoid the problem "ftTable = 5 soon becomes ftTable = 10" through coding, but not to change the enumeration type wink It will be not easy to break the enumeration while adding some new member if all old members already have their directly set numbers. Of course, you can do it simpler, just remember to add values only to the end of the enumeration.

Although, maybe there is some another approach, and maybe somebody will share it here smile

Offline

#11 2019-08-10 20:00:11

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Enumerated types in method parameter

ab wrote:

About enums serialization as string, it is not possible yet for method parameter - but it is for record serialization IIRC.

@ab, yes, for record serialization enums can be serialized as string, but please consider adding options to the server/service configuration so that the parameters of the enumerated type (including complex types such as records or arrays) are serialized to a string

Offline

Board footer

Powered by FluxBB