Skip to content

Commit 70a4fa5

Browse files
committed
[options] small refactory to allow implement custom serializers as db, registry etc
1 parent 69513b4 commit 70a4fa5

6 files changed

Lines changed: 555 additions & 211 deletions

File tree

Quick.Options.Serializer.Json.pas

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,32 @@ interface
4545

4646
type
4747

48-
TJsonOptionsSerializer = class(TOptionsSerializer)
48+
TJsonOptionsSerializer = class(TOptionsFileSerializer)
4949
private
5050
fSerializer : TRTTIJson;
51-
function ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
51+
function ParseFile(out aJsonObj : TJsonObject) : Boolean;
5252
public
53-
constructor Create;
53+
constructor Create(const aFilename : string);
5454
destructor Destroy; override;
55-
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
56-
function LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean; override;
57-
procedure Save(const aFilename : string; aSections : TSectionList); override;
58-
function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
55+
function Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
56+
function LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean; override;
57+
procedure Save(aSections : TSectionList); override;
58+
function GetFileSectionNames(out oSections : TArray<string>) : Boolean; override;
59+
function ConfigExists : Boolean; override;
5960
end;
6061

6162
implementation
6263

6364
{ TJsonOptionsSerializer }
6465

65-
constructor TJsonOptionsSerializer.Create;
66+
function TJsonOptionsSerializer.ConfigExists: Boolean;
6667
begin
68+
Result := FileExists(Filename);
69+
end;
70+
71+
constructor TJsonOptionsSerializer.Create(const aFilename : string);
72+
begin
73+
Filename := aFilename;
6774
fSerializer := TRTTIJson.Create(TSerializeLevel.slPublishedProperty,True);
6875
end;
6976

@@ -73,14 +80,14 @@ destructor TJsonOptionsSerializer.Destroy;
7380
inherited;
7481
end;
7582

76-
function TJsonOptionsSerializer.GetFileSectionNames(const aFilename: string; out oSections: TArray<string>): Boolean;
83+
function TJsonOptionsSerializer.GetFileSectionNames(out oSections: TArray<string>): Boolean;
7784
var
7885
json : TJsonObject;
7986
i : Integer;
8087
begin
8188
Result := False;
8289
json := nil;
83-
if ParseFile(aFilename,json) then
90+
if ParseFile(json) then
8491
begin
8592
try
8693
for i := 0 to json.Count - 1 do
@@ -94,33 +101,33 @@ function TJsonOptionsSerializer.GetFileSectionNames(const aFilename: string; out
94101
end;
95102
end;
96103

97-
function TJsonOptionsSerializer.ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
104+
function TJsonOptionsSerializer.ParseFile(out aJsonObj : TJsonObject) : Boolean;
98105
var
99106
fileoptions : string;
100107
begin
101108
aJsonObj := nil;
102-
if FileExists(aFilename) then
109+
if FileExists(Filename) then
103110
begin
104111
{$IFDEF DELPHIRX102_UP}
105-
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
112+
fileoptions := TFile.ReadAllText(Filename,TEncoding.UTF8);
106113
{$ELSE}
107-
fileoptions := TFile.ReadAllText(aFilename);
114+
fileoptions := TFile.ReadAllText(fFilename);
108115
{$ENDIF}
109116
aJsonObj := TJsonObject.ParseJSONValue(fileoptions) as TJsonObject;
110-
if aJsonObj = nil then raise EOptionLoadError.CreateFmt('Config file "%s" is damaged or not well-formed Json format!',[ExtractFileName(aFilename)]);
117+
if aJsonObj = nil then raise EOptionLoadError.CreateFmt('Config file "%s" is damaged or not well-formed Json format!',[ExtractFileName(Filename)]);
111118
end;
112119
Result := aJsonObj <> nil;
113120
end;
114121

115-
function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
122+
function TJsonOptionsSerializer.Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
116123
var
117124
option : TOptions;
118125
json : TJsonObject;
119126
jpair : TJSONPair;
120127
found : Integer;
121128
begin
122129
Result := False;
123-
if ParseFile(aFilename,json) then
130+
if ParseFile(json) then
124131
begin
125132
found := 0;
126133
try
@@ -154,14 +161,14 @@ function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSect
154161
end;
155162
end;
156163

157-
function TJsonOptionsSerializer.LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean;
164+
function TJsonOptionsSerializer.LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean;
158165
var
159166
json : TJsonObject;
160167
jpair : TJSONPair;
161168
begin
162169
Result := False;
163170
//read option file
164-
if ParseFile(aFilename,json) then
171+
if ParseFile(json) then
165172
begin
166173
try
167174
jpair := fSerializer.GetJsonPairByName(json,aOptions.Name);
@@ -179,7 +186,7 @@ function TJsonOptionsSerializer.LoadSection(const aFilename : string; aSections
179186
end;
180187
end;
181188

182-
procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
189+
procedure TJsonOptionsSerializer.Save(aSections : TSectionList);
183190
var
184191
option : TOptions;
185192
fileoptions : string;
@@ -200,7 +207,7 @@ procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSec
200207
end;
201208
end;
202209
fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
203-
if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
210+
if not fileoptions.IsEmpty then TFile.WriteAllText(Filename,fileoptions);
204211
finally
205212
json.Free;
206213
end;

Quick.Options.Serializer.Yaml.pas

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,32 @@ interface
4444

4545
type
4646

47-
TYamlOptionsSerializer = class(TOptionsSerializer)
47+
TYamlOptionsSerializer = class(TOptionsFileSerializer)
4848
private
4949
fSerializer : TRTTIYaml;
50-
function ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
50+
function ParseFile(out aYamlObj : TYamlObject) : Boolean;
5151
public
52-
constructor Create;
52+
constructor Create(const aFilename : string);
5353
destructor Destroy; override;
54-
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
55-
function LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean; override;
56-
procedure Save(const aFilename : string; aSections : TSectionList); override;
57-
function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
54+
function Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
55+
function LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean; override;
56+
procedure Save(aSections : TSectionList); override;
57+
function GetFileSectionNames(out oSections : TArray<string>) : Boolean; override;
58+
function ConfigExists : Boolean; override;
5859
end;
5960

6061
implementation
6162

6263
{ TYamlOptionsSerializer }
6364

64-
constructor TYamlOptionsSerializer.Create;
65+
function TYamlOptionsSerializer.ConfigExists: Boolean;
6566
begin
67+
Result := FileExists(Filename);
68+
end;
69+
70+
constructor TYamlOptionsSerializer.Create(const aFilename : string);
71+
begin
72+
Filename := aFilename;
6673
fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
6774
end;
6875

@@ -72,14 +79,14 @@ destructor TYamlOptionsSerializer.Destroy;
7279
inherited;
7380
end;
7481

75-
function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean;
82+
function TYamlOptionsSerializer.GetFileSectionNames(out oSections : TArray<string>) : Boolean;
7683
var
7784
yaml : TYamlObject;
7885
i : Integer;
7986
begin
8087
Result := False;
8188
yaml := nil;
82-
if ParseFile(aFilename,yaml) then
89+
if ParseFile(yaml) then
8390
begin
8491
try
8592
for i := 0 to yaml.Count - 1 do
@@ -93,23 +100,23 @@ function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; ou
93100
end;
94101
end;
95102

96-
function TYamlOptionsSerializer.ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
103+
function TYamlOptionsSerializer.ParseFile(out aYamlObj : TYamlObject) : Boolean;
97104
var
98105
fileoptions : string;
99106
begin
100107
aYamlObj := nil;
101-
if FileExists(aFilename) then
108+
if FileExists(Filename) then
102109
begin
103-
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
104-
if fileoptions.IsEmpty then EOptionLoadError.CreateFmt('Config file "%s" is empty!',[ExtractFileName(aFilename)]);
110+
fileoptions := TFile.ReadAllText(Filename,TEncoding.UTF8);
111+
if fileoptions.IsEmpty then EOptionLoadError.CreateFmt('Config file "%s" is empty!',[ExtractFileName(Filename)]);
105112

106113
aYamlObj := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
107-
if aYamlObj = nil then raise EOptionLoadError.CreateFmt('Config file "%s" is damaged or not well-formed Yaml format!',[ExtractFileName(aFilename)]);
114+
if aYamlObj = nil then raise EOptionLoadError.CreateFmt('Config file "%s" is damaged or not well-formed Yaml format!',[ExtractFileName(Filename)]);
108115
end;
109116
Result := aYamlObj <> nil;
110117
end;
111118

112-
function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
119+
function TYamlOptionsSerializer.Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
113120
var
114121
option : TOptions;
115122
yaml : TYamlObject;
@@ -118,7 +125,7 @@ function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSect
118125
begin
119126
Result := False;
120127
//read option file
121-
if ParseFile(aFilename,yaml) then
128+
if ParseFile(yaml) then
122129
begin
123130
found := 0;
124131
try
@@ -152,14 +159,14 @@ function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSect
152159
end;
153160
end;
154161

155-
function TYamlOptionsSerializer.LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean;
162+
function TYamlOptionsSerializer.LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean;
156163
var
157164
yaml : TYamlObject;
158165
ypair : TYamlPair;
159166
begin
160167
Result := False;
161168
//read option file
162-
if ParseFile(aFilename,yaml) then
169+
if ParseFile(yaml) then
163170
begin
164171
try
165172
ypair := fSerializer.GetYamlPairByName(yaml,aOptions.Name);
@@ -177,7 +184,7 @@ function TYamlOptionsSerializer.LoadSection(const aFilename : string; aSections
177184
end;
178185
end;
179186

180-
procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
187+
procedure TYamlOptionsSerializer.Save(aSections : TSectionList);
181188
var
182189
option : TOptions;
183190
fileoptions : string;
@@ -198,7 +205,7 @@ procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSec
198205
end;
199206
end;
200207
fileoptions := yaml.ToYaml;
201-
if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
208+
if not fileoptions.IsEmpty then TFile.WriteAllText(Filename,fileoptions);
202209
finally
203210
yaml.Free;
204211
end;

0 commit comments

Comments
 (0)