Skip to content

Commit ba6292a

Browse files
committed
Quick.Options defaults if section not found in file
1 parent 52671c4 commit ba6292a

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

Quick.Options.Serializer.Json.pas

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Author : Kike Pérez
88
Version : 1.0
99
Created : 18/10/2019
10-
Modified : 22/10/2019
10+
Modified : 28/11/2019
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -51,7 +51,7 @@ TJsonOptionsSerializer = class(TOptionsSerializer)
5151
public
5252
constructor Create;
5353
destructor Destroy; override;
54-
procedure Load(const aFilename : string; aSections : TSectionList); override;
54+
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
5555
procedure Save(const aFilename : string; aSections : TSectionList); override;
5656
end;
5757

@@ -70,13 +70,14 @@ destructor TJsonOptionsSerializer.Destroy;
7070
inherited;
7171
end;
7272

73-
procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
73+
function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
7474
var
7575
option : TOptions;
7676
fileoptions : string;
7777
json : TJsonObject;
7878
jpair : TJSONPair;
7979
begin
80+
Result := False;
8081
if FileExists(aFilename) then
8182
begin
8283
//read option file
@@ -85,7 +86,11 @@ procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSec
8586
for option in aSections do
8687
begin
8788
jpair := fSerializer.GetJsonPairByName(json,option.Name);
88-
if jpair = nil then raise Exception.CreateFmt('Config section "%s" not found',[option.Name]);
89+
if jpair = nil then
90+
begin
91+
if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
92+
else Continue;
93+
end;
8994
if jpair.JsonValue <> nil then
9095
begin
9196
//deserialize option

Quick.Options.Serializer.Yaml.pas

+15-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Author : Kike Pérez
88
Version : 1.0
99
Created : 18/10/2019
10-
Modified : 22/10/2019
10+
Modified : 28/11/2019
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -50,7 +50,7 @@ TYamlOptionsSerializer = class(TOptionsSerializer)
5050
public
5151
constructor Create;
5252
destructor Destroy; override;
53-
procedure Load(const aFilename : string; aSections : TSectionList); override;
53+
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
5454
procedure Save(const aFilename : string; aSections : TSectionList); override;
5555
end;
5656

@@ -69,25 +69,31 @@ destructor TYamlOptionsSerializer.Destroy;
6969
inherited;
7070
end;
7171

72-
procedure TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
72+
function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
7373
var
7474
option : TOptions;
7575
fileoptions : string;
76-
json : TYamlObject;
77-
jpair : TYamlPair;
76+
yaml : TYamlObject;
77+
ypair : TYamlPair;
7878
begin
79+
Result := False;
7980
if FileExists(aFilename) then
8081
begin
8182
//read option file
8283
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
83-
json := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
84+
yaml := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
8485
for option in aSections do
8586
begin
86-
jpair := fSerializer.GetYamlPairByName(json,option.Name);
87-
if jpair.Value <> nil then
87+
ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
88+
if ypair = nil then
89+
begin
90+
if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
91+
else Continue;
92+
end;
93+
if ypair.Value <> nil then
8894
begin
8995
//deserialize option
90-
fSerializer.DeserializeObject(option,jpair.Value as TYamlObject);
96+
fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
9197
//validate loaded configuration
9298
option.ValidateOptions;
9399
end;

Quick.Options.pas

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Author : Kike Pérez
88
Version : 1.0
99
Created : 18/10/2019
10-
Modified : 29/10/2019
10+
Modified : 28/11/2019
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -129,13 +129,13 @@ TOptionsClass = class of TOptions;
129129

130130
IOptionsSerializer = interface
131131
['{7DECE203-4AAE-4C9D-86C8-B3D583DF7C8B}']
132-
procedure Load(const aFilename : string; aSections : TSectionList);
132+
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
133133
procedure Save(const aFilename : string; aSections : TSectionList);
134134
end;
135135

136136
TOptionsSerializer = class(TInterfacedObject,IOptionsSerializer)
137137
public
138-
procedure Load(const aFilename : string; aSections : TSectionList); virtual; abstract;
138+
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; virtual; abstract;
139139
procedure Save(const aFilename : string; aSections : TSectionList); virtual; abstract;
140140
end;
141141

@@ -184,7 +184,7 @@ TOptionsContainer = class(TInterfacedObject,IOptionsContainer)
184184
function GetSectionInterface<T : TOptions> : IOptions<T>;
185185
function GetSection<T : TOptions>(const aSectionName : string = '') : T; overload;
186186
function Count : Integer;
187-
procedure Load;
187+
procedure Load(aFailOnSectionNotExists : Boolean = False);
188188
procedure Save;
189189
end;
190190

@@ -245,7 +245,7 @@ procedure TOptionsContainer.FileModifiedNotify(MonitorNotify: TMonitorNotify);
245245
if Assigned(fOnFileModified) then fOnFileModified;
246246
if fReloadIfFileChanged then
247247
begin
248-
Load;
248+
Load(False);
249249
end;
250250
end;
251251
end;
@@ -330,13 +330,13 @@ function TOptionsContainer.GetSectionInterface<T>: IOptions<T>;
330330
Result := TOptionValue<T>.Create(Self.GetSection<T>);
331331
end;
332332

333-
procedure TOptionsContainer.Load;
333+
procedure TOptionsContainer.Load(aFailOnSectionNotExists : Boolean = False);
334334
var
335335
option : TOptions;
336336
begin
337337
if FileExists(fFilename) then
338338
begin
339-
fSerializer.Load(fFilename,fSections);
339+
if not fSerializer.Load(fFilename,fSections,aFailOnSectionNotExists) then Save;
340340
if not fLoaded then
341341
begin
342342
fLoaded := True;

0 commit comments

Comments
 (0)