Explorar o código

Quick.Options defaults if section not found in file

Exilon %!s(int64=5) %!d(string=hai) anos
pai
achega
ba6292a75a
Modificáronse 3 ficheiros con 31 adicións e 20 borrados
  1. 9 4
      Quick.Options.Serializer.Json.pas
  2. 15 9
      Quick.Options.Serializer.Yaml.pas
  3. 7 7
      Quick.Options.pas

+ 9 - 4
Quick.Options.Serializer.Json.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Version     : 1.0
   Created     : 18/10/2019
-  Modified    : 22/10/2019
+  Modified    : 28/11/2019
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
@@ -51,7 +51,7 @@ type
   public
     constructor Create;
     destructor Destroy; override;
-    procedure Load(const aFilename : string; aSections : TSectionList); override;
+    function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
     procedure Save(const aFilename : string; aSections : TSectionList); override;
   end;
 
@@ -70,13 +70,14 @@ begin
   inherited;
 end;
 
-procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
+function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
 var
   option : TOptions;
   fileoptions : string;
   json : TJsonObject;
   jpair : TJSONPair;
 begin
+  Result := False;
   if FileExists(aFilename) then
   begin
     //read option file
@@ -85,7 +86,11 @@ begin
     for option in aSections do
     begin
       jpair := fSerializer.GetJsonPairByName(json,option.Name);
-      if jpair = nil then raise Exception.CreateFmt('Config section "%s" not found',[option.Name]);
+      if jpair = nil then
+      begin
+        if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
+          else Continue;
+      end;
       if jpair.JsonValue <> nil then
       begin
         //deserialize option

+ 15 - 9
Quick.Options.Serializer.Yaml.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Version     : 1.0
   Created     : 18/10/2019
-  Modified    : 22/10/2019
+  Modified    : 28/11/2019
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
@@ -50,7 +50,7 @@ type
   public
     constructor Create;
     destructor Destroy; override;
-    procedure Load(const aFilename : string; aSections : TSectionList); override;
+    function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
     procedure Save(const aFilename : string; aSections : TSectionList); override;
   end;
 
@@ -69,25 +69,31 @@ begin
   inherited;
 end;
 
-procedure TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
+function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
 var
   option : TOptions;
   fileoptions : string;
-  json : TYamlObject;
-  jpair : TYamlPair;
+  yaml : TYamlObject;
+  ypair : TYamlPair;
 begin
+  Result := False;
   if FileExists(aFilename) then
   begin
     //read option file
     fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
-    json := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
+    yaml := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
     for option in aSections do
     begin
-      jpair := fSerializer.GetYamlPairByName(json,option.Name);
-      if jpair.Value <> nil then
+      ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
+      if ypair = nil then
+      begin
+        if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
+          else Continue;
+      end;
+      if ypair.Value <> nil then
       begin
         //deserialize option
-        fSerializer.DeserializeObject(option,jpair.Value as TYamlObject);
+        fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
         //validate loaded configuration
         option.ValidateOptions;
       end;

+ 7 - 7
Quick.Options.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Version     : 1.0
   Created     : 18/10/2019
-  Modified    : 29/10/2019
+  Modified    : 28/11/2019
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
@@ -129,13 +129,13 @@ type
 
   IOptionsSerializer = interface
   ['{7DECE203-4AAE-4C9D-86C8-B3D583DF7C8B}']
-    procedure Load(const aFilename : string; aSections : TSectionList);
+    function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
     procedure Save(const aFilename : string; aSections : TSectionList);
   end;
 
   TOptionsSerializer = class(TInterfacedObject,IOptionsSerializer)
   public
-    procedure Load(const aFilename : string; aSections : TSectionList); virtual; abstract;
+    function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; virtual; abstract;
     procedure Save(const aFilename : string; aSections : TSectionList); virtual; abstract;
   end;
 
@@ -184,7 +184,7 @@ type
     function GetSectionInterface<T : TOptions> : IOptions<T>;
     function GetSection<T : TOptions>(const aSectionName : string = '') : T; overload;
     function Count : Integer;
-    procedure Load;
+    procedure Load(aFailOnSectionNotExists : Boolean = False);
     procedure Save;
   end;
 
@@ -245,7 +245,7 @@ begin
     if Assigned(fOnFileModified) then fOnFileModified;
     if fReloadIfFileChanged then
     begin
-      Load;
+      Load(False);
     end;
   end;
 end;
@@ -330,13 +330,13 @@ begin
   Result := TOptionValue<T>.Create(Self.GetSection<T>);
 end;
 
-procedure TOptionsContainer.Load;
+procedure TOptionsContainer.Load(aFailOnSectionNotExists : Boolean = False);
 var
   option : TOptions;
 begin
   if FileExists(fFilename) then
   begin
-    fSerializer.Load(fFilename,fSections);
+    if not fSerializer.Load(fFilename,fSections,aFailOnSectionNotExists) then Save;
     if not fLoaded then
     begin
       fLoaded := True;