Skip to content

Commit 2ce2193

Browse files
committed
[Quick.Options] Some improvements
- Validator refactorized - Hidden options - GetFileNameSections
1 parent 7255cbe commit 2ce2193

File tree

3 files changed

+238
-101
lines changed

3 files changed

+238
-101
lines changed

Diff for: Quick.Options.Serializer.Json.pas

+67-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{ ***************************************************************************
22
3-
Copyright (c) 2015-2019 Kike Pérez
3+
Copyright (c) 2015-2020 Kike Pérez
44
55
Unit : Quick.Options.Serializer.Json
66
Description : Configuration groups Json Serializer
77
Author : Kike Pérez
88
Version : 1.0
99
Created : 18/10/2019
10-
Modified : 28/11/2019
10+
Modified : 07/02/2020
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -48,11 +48,13 @@ interface
4848
TJsonOptionsSerializer = class(TOptionsSerializer)
4949
private
5050
fSerializer : TRTTIJson;
51+
function ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
5152
public
5253
constructor Create;
5354
destructor Destroy; override;
5455
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
5556
procedure Save(const aFilename : string; aSections : TSectionList); override;
57+
function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
5658
end;
5759

5860
implementation
@@ -70,6 +72,41 @@ destructor TJsonOptionsSerializer.Destroy;
7072
inherited;
7173
end;
7274

75+
function TJsonOptionsSerializer.GetFileSectionNames(const aFilename: string; out oSections: TArray<string>): Boolean;
76+
var
77+
json : TJsonObject;
78+
jpair : TJSONPair;
79+
i : Integer;
80+
begin
81+
Result := False;
82+
json := nil;
83+
if ParseFile(aFilename,json) then
84+
begin
85+
try
86+
for i := 0 to json.Count - 1 do
87+
begin
88+
oSections := oSections + [json.Pairs[i].JsonString.Value];
89+
end;
90+
Result := True;
91+
finally
92+
json.Free;
93+
end;
94+
end;
95+
end;
96+
97+
function TJsonOptionsSerializer.ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
98+
var
99+
fileoptions : string;
100+
begin
101+
aJsonObj := nil;
102+
if FileExists(aFilename) then
103+
begin
104+
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
105+
aJsonObj := TJsonObject.ParseJSONValue(fileoptions) as TJsonObject;
106+
end;
107+
Result := aJsonObj <> nil;
108+
end;
109+
73110
function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
74111
var
75112
option : TOptions;
@@ -78,26 +115,28 @@ function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSect
78115
jpair : TJSONPair;
79116
begin
80117
Result := False;
81-
if FileExists(aFilename) then
118+
if ParseFile(aFilename,json) then
82119
begin
83-
//read option file
84-
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
85-
json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject;
86-
for option in aSections do
87-
begin
88-
jpair := fSerializer.GetJsonPairByName(json,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;
94-
if jpair.JsonValue <> nil then
120+
try
121+
for option in aSections do
95122
begin
96-
//deserialize option
97-
fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
98-
//validate loaded configuration
99-
option.ValidateOptions;
123+
jpair := fSerializer.GetJsonPairByName(json,option.Name);
124+
if jpair = nil then
125+
begin
126+
if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
127+
else Continue;
128+
end;
129+
if jpair.JsonValue <> nil then
130+
begin
131+
//deserialize option
132+
fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
133+
//validate loaded configuration
134+
option.ValidateOptions;
135+
end;
100136
end;
137+
Result := True;
138+
finally
139+
json.Free;
101140
end;
102141
end;
103142
end;
@@ -113,14 +152,17 @@ procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSec
113152
try
114153
for option in aSections do
115154
begin
116-
//validate configuration before save
117-
option.ValidateOptions;
118-
//serialize option
119-
jpair := fSerializer.Serialize(option.Name,option);
120-
json.AddPair(jpair);
155+
if not option.HideOptions then
156+
begin
157+
//validate configuration before save
158+
option.ValidateOptions;
159+
//serialize option
160+
jpair := fSerializer.Serialize(option.Name,option);
161+
json.AddPair(jpair);
162+
end;
121163
end;
122164
fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
123-
TFile.WriteAllText(aFilename,fileoptions);
165+
if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
124166
finally
125167
json.Free;
126168
end;

Diff for: Quick.Options.Serializer.Yaml.pas

+67-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{ ***************************************************************************
22
3-
Copyright (c) 2015-2019 Kike Pérez
3+
Copyright (c) 2015-2020 Kike Pérez
44
55
Unit : Quick.Options.Serializer.Yaml
66
Description : Configuration groups Yaml Serializer
77
Author : Kike Pérez
88
Version : 1.0
99
Created : 18/10/2019
10-
Modified : 28/11/2019
10+
Modified : 07/02/2020
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -47,11 +47,13 @@ interface
4747
TYamlOptionsSerializer = class(TOptionsSerializer)
4848
private
4949
fSerializer : TRTTIYaml;
50+
function ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
5051
public
5152
constructor Create;
5253
destructor Destroy; override;
5354
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
5455
procedure Save(const aFilename : string; aSections : TSectionList); override;
56+
function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
5557
end;
5658

5759
implementation
@@ -69,6 +71,41 @@ destructor TYamlOptionsSerializer.Destroy;
6971
inherited;
7072
end;
7173

74+
function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean;
75+
var
76+
yaml : TYamlObject;
77+
ypair : TYamlPair;
78+
i : Integer;
79+
begin
80+
Result := False;
81+
yaml := nil;
82+
if ParseFile(aFilename,yaml) then
83+
begin
84+
try
85+
for i := 0 to yaml.Count - 1 do
86+
begin
87+
oSections := oSections + [yaml.Pairs[i].Name];
88+
end;
89+
Result := True;
90+
finally
91+
yaml.Free;
92+
end;
93+
end;
94+
end;
95+
96+
function TYamlOptionsSerializer.ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
97+
var
98+
fileoptions : string;
99+
begin
100+
aYamlObj := nil;
101+
if FileExists(aFilename) then
102+
begin
103+
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
104+
aYamlObj := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
105+
end;
106+
Result := aYamlObj <> nil;
107+
end;
108+
72109
function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
73110
var
74111
option : TOptions;
@@ -77,26 +114,28 @@ function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSect
77114
ypair : TYamlPair;
78115
begin
79116
Result := False;
80-
if FileExists(aFilename) then
117+
//read option file
118+
if ParseFile(aFilename,yaml) then
81119
begin
82-
//read option file
83-
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
84-
yaml := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
85-
for option in aSections do
86-
begin
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
120+
try
121+
for option in aSections do
94122
begin
95-
//deserialize option
96-
fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
97-
//validate loaded configuration
98-
option.ValidateOptions;
123+
ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
124+
if ypair = nil then
125+
begin
126+
if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
127+
else Continue;
128+
end;
129+
if ypair.Value <> nil then
130+
begin
131+
//deserialize option
132+
fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
133+
//validate loaded configuration
134+
option.ValidateOptions;
135+
end;
99136
end;
137+
finally
138+
yaml.Free;
100139
end;
101140
end;
102141
end;
@@ -112,14 +151,17 @@ procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSec
112151
try
113152
for option in aSections do
114153
begin
115-
//validate configuration before save
116-
option.ValidateOptions;
117-
//serialize option
118-
jpair := fSerializer.Serialize(option.Name,option);
119-
yaml.AddPair(jpair);
154+
if not option.HideOptions then
155+
begin
156+
//validate configuration before save
157+
option.ValidateOptions;
158+
//serialize option
159+
jpair := fSerializer.Serialize(option.Name,option);
160+
yaml.AddPair(jpair);
161+
end;
120162
end;
121163
fileoptions := yaml.ToYaml;
122-
TFile.WriteAllText(aFilename,fileoptions);
164+
if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
123165
finally
124166
yaml.Free;
125167
end;

0 commit comments

Comments
 (0)