* Some fixes in code generation

This commit is contained in:
Michaël Van Canneyt 2024-01-07 14:02:40 +01:00
parent c90503812a
commit 859fa9fe8f

View File

@ -62,6 +62,7 @@ type
FPasName: String;
FPasReturnType: String;
FReturnType: TJSONtype;
function GetPasReturnType: String;
procedure SetParams(AValue: TAPIMethodParams);
Public
Constructor Create(aCollection : TCollection) ; override;
@ -70,7 +71,7 @@ type
Property Name : String Read FName Write FName;
Property PasName : String Read FPasName Write FPasName;
Property ReturnType : TJSONtype Read FReturnType Write FReturnType;
Property PasReturnType : String Read FPasReturnType Write FPasReturnType;
Property PasReturnType : String Read GetPasReturnType Write FPasReturnType;
Property Params : TAPIMethodParams Read FParams Write SetParams;
end;
@ -120,7 +121,7 @@ type
protected
// Overrides
Function BaseUnits : String; override;
function StringToJSType(const S: String): TJSONtype;
function StringToJSType(S: String): TJSONtype;
// High-level decl
procedure GenerateServiceClassDeclarations(aServices: TAPIServices); virtual;
procedure GenerateServiceDeclaration(aService: TAPIService); virtual;
@ -198,6 +199,21 @@ begin
FParams.Assign(AValue);
end;
function TAPIServiceMethod.GetPasReturnType: String;
begin
Result:=FPasReturnType;
if Result='' then
Case ReturnType of
jtNull,
jtUnknown : Result:='JSValue';
jtNumber : Result:='Double';
jtString : Result:='String';
jtBoolean : Result:='Boolean';
jtArray : Result:='TJSArray';
jtObject : Result:='TJSObject';
end;
end;
constructor TAPIServiceMethod.Create(aCollection: TCollection);
begin
inherited Create(aCollection);
@ -472,14 +488,11 @@ begin
end;
end;
function TAPIClientCodeGen.StringToJSType(const S : String) : TJSONtype;
var
LS : String;
function TAPIClientCodeGen.StringToJSType(S : String) : TJSONtype;
begin
LS:=LowerCase(S);
Case LS of
S:=LowerCase(S);
Case S of
'jtunknown' : Result:=jtUnknown;
'jtnumber' : Result:=jtNumber;
'jtstring' : Result:=jtString;
@ -508,6 +521,8 @@ begin
aMeth.Name:=aJSON.Get('name','');
aMeth.PasName:=GetServiceMethodName(aSvc.Name,aMeth.Name);
aMeth.ReturnType:=StringToJSType(aJSON.Get('resulttype',''));
if (aMeth.ReturnType=jtNumber) and (ccoPreferNativeInt in Options) then
aMeth.PasReturnType:='NativeInt';
aParams:=aJSON.Get('paramdefs',TJSONarray(Nil));
if (aJSON.Get('len',0)>0) and Assigned(aParams) then
FillAPIMethod(aSvc,aMeth,aParams);
@ -604,7 +619,14 @@ begin
indent;
Addln('If Assigned(aOnSuccess) then');
Indent;
Addln('aOnSuccess(%s(aResult))',[aMeth.PasReturnType]);
if (ccoForceJSValueResult in options) then
Addln('aOnSuccess(aResult)')
else
begin
if (aMeth.PasReturnType='') then
Addln('// Return type is unknown, this will likely result in a compiler error. Set return type in API definition');
Addln('aOnSuccess(%s(aResult))',[aMeth.PasReturnType]);
end;
undent;
undent;
Addln('end;');
@ -637,7 +659,7 @@ Var
I : integer;
begin
ClassComment(aService.PasName);
ClassHeader(aService.PasName);
AddLn('%s = Class(TRPCCustomService)',[aService.PasName]);
Addln('Protected');
Indent;
@ -671,7 +693,7 @@ Var
I : integer;
begin
ClassComment(aService.PasName);
ClassHeader(aService.PasName);
Addln('');
GenerateRPCClassNameImplementation(aService);
For I:=0 to aService.Methods.Count-1 do
@ -683,7 +705,7 @@ end;
function TAPIClientCodeGen.BaseUnits: String;
begin
Result:='fprpcclient';
Result:='js, fprpcclient';
end;