Browse Source

* Allow to read YAML files.

Michaël Van Canneyt 7 months ago
parent
commit
498ff5c56c
2 changed files with 47 additions and 3 deletions
  1. 1 0
      utils/openapi/fpmake.pp
  2. 46 3
      utils/openapi/openapi2pas.pp

+ 1 - 0
utils/openapi/fpmake.pp

@@ -34,6 +34,7 @@ begin
 
     P.Dependencies.Add('fcl-base');
     P.Dependencies.Add('fcl-json');
+    P.Dependencies.Add('fcl-yaml');
     P.Dependencies.Add('fcl-openapi');
     P.Dependencies.Add('fcl-jsonschema');
 

+ 46 - 3
utils/openapi/openapi2pas.pp

@@ -20,6 +20,10 @@ uses
   Classes,
   SysUtils,
   CustApp,
+  fpjson,
+  fpyaml.parser,
+  fpyaml.data,
+  fpyaml.json,
   fpopenapi.objects,
   fpopenapi.reader,
   fpopenapi.codegen;
@@ -30,10 +34,12 @@ type
 
   TGenDTOApplication = class(TCustomApplication)
   private
+    FInputIsYAML : Boolean;
     FQuiet : Boolean;
     FCodeGen : TOpenAPICodeGen;
     FServiceMapFile,
     FUUIDMapFile : String;
+    procedure ReadYAML(const aInputFile: string; aApi: TOpenAPI);
   protected
     Procedure DoLog(EventType : TEventType; const Msg : String); override;
     procedure DoRun; override;
@@ -63,11 +69,44 @@ begin
     FCodeGen.UUIDMap.SavetoFile(FUUIDMapFile);
 end;
 
+procedure TGenDTOApplication.ReadYAML(const aInputFile : string; aApi: TOpenAPI);
+
+var
+  lParser : TYAMLParser;
+  lYAML : TYAMLStream;
+  lJSON : TJSONData;
+  lJSONString : TJSONStringType;
+  lReader : TOpenAPIReader;
+
+begin
+  lYAML:=Nil;
+  lParser:=TYAMLParser.Create(aInputFile);
+  try
+    lYAML:=lParser.Parse;
+  finally
+    lParser.Free;
+  end;
+  lJSON:=Nil;
+  try
+    lJSON:=YAMLtoJSON(lYAML);
+    lJSONString:=lJSON.FormatJSON();
+  finally
+    lJSON.Free;
+  end;
+  lReader:=TOpenAPIReader.Create(Self);
+  try
+    lReader.ReadFromString(aAPI,lJSONString);
+  finally
+    lReader.Free;
+  end;
+end;
+
 procedure TGenDTOApplication.ReadOpenAPi(const aInputFile : string; aApi: TOpenAPI);
 
 var
   lReader : TOpenAPIReader;
 
+
 begin
   lReader:=TOpenAPIReader.Create(Self);
   try
@@ -104,8 +143,8 @@ end;
 procedure TGenDTOApplication.DoRun;
 
 const
-  shortOpts = 'hi:o:dequ:s:varcC:bnw:';
-  LongOpts : Array of string = ('help','input:','output:','delphi','uuid-map:','quiet','service-map','verbose-header','enumerated','async','server','client','config:','abstract','no-implementation','write-config:');
+  shortOpts = 'hi:o:dequ:s:varcC:bnw:y';
+  LongOpts : Array of string = ('help','input:','output:','delphi','uuid-map:','quiet','service-map','verbose-header','enumerated','async','server','client','config:','abstract','no-implementation','write-config:','yaml');
 
 var
   lAPI : TOpenAPI;
@@ -134,6 +173,7 @@ begin
   FCodeGen.GenerateClient:=HasOption('c','client');
   FCodeGen.AbstractServiceCalls:=HasOption('b','abstract');
   FCodeGen.SkipServerServiceImplementationModule:=HasOption('n','no-implementation');
+  FInputIsYAML:=HasOption('y','yaml') or SameText(ExtractFileExt(lInputFile),'.yaml');
   FQuiet:=HasOption('q','quiet');
 
   if HasOption('w','write-config') then
@@ -149,7 +189,10 @@ begin
       lOutputFile:=ChangeFileExt(lInputFile,'');
     lAPI:=TOpenAPI.Create;
     try
-      ReadOpenAPi(lInputFile,lAPI);
+      if FInputIsYAML then
+        ReadYAML(lInputFile,lAPI)
+      else
+        ReadOpenAPi(lInputFile,lAPI);
       WriteApi(lApi,lOutputFile);
     finally
       lApi.Free;