-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathQuick.Options.pas
552 lines (476 loc) · 15.8 KB
/
Quick.Options.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
{ ***************************************************************************
Copyright (c) 2015-2019 Kike Pérez
Unit : Quick.Options
Description : Configuration group settings
Author : Kike Pérez
Version : 1.0
Created : 18/10/2019
Modified : 28/11/2019
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Options;
{$i QuickLib.inc}
interface
uses
Classes,
RTTI,
Quick.RTTI.Utils,
System.TypInfo,
System.SysUtils,
System.Generics.Collections,
System.Json,
Quick.Commons,
Quick.FileMonitor;
type
Required = class(TCustomAttribute);
TValidationCustomAttribute = class(TCustomAttribute)
protected
fErrorMsg : string;
public
property ErrorMsg : string read fErrorMsg write fErrorMsg;
end;
Range = class(TValidationCustomAttribute)
private
fRangeMin : Double;
fRangeMax : Double;
public
constructor Create(aMin, aMax : Integer; const aErrorMsg : string = ''); overload;
constructor Create(aMin, aMax : Double; const aErrorMsg : string = ''); overload;
property Min : Double read fRangeMin write fRangeMax;
property Max : Double read fRangeMax write fRangeMax;
end;
StringLength = class(TValidationCustomAttribute)
private
fMaxLength : Integer;
public
constructor Create(aMaxLength : Integer; const aErrorMsg : string = '');
property MaxLength : Integer read fMaxLength write fMaxLength;
end;
TOptions = class;
TConfigureOptionsProc<T : TOptions> = reference to procedure(aOptions : T);
IOptionsValidator = interface
['{C6A09F78-8E34-4689-B943-83620437B9EF}']
procedure ValidateOptions;
end;
IOptionsConfigure<T> = interface
['{49258BEB-A21D-4C64-BA71-767B8DBD4D92}']
//function ConfigureOptions(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
end;
TOptions = class(TInterfacedObject,IOptionsValidator)
private
fName : string;
procedure ValidateRequired(aProperty : TRttiProperty);
procedure ValidateStringLength(aProperty: TRttiProperty; aValidation : StringLength);
procedure ValidateRange(aProperty : TRttiProperty; aValidation : Range);
procedure DoValidateOptions; virtual;
public
constructor Create;
property Name : string read fName write fName;
procedure DefaultValues; virtual; abstract;
function ConfigureOptions<T : TOptions>(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
procedure ValidateOptions;
end;
TOptions<T : TOptions> = record
private
fOptions : T;
public
constructor Create(aOptions : T);
function ConfigureOptions(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
end;
IOptions<T : TOptions> = interface
['{2779F946-2692-4F74-88AD-F35F5137057A}']
function GetSectionValue : T;
property Value : T read GetSectionValue;
end;
TOptionsClass = class of TOptions;
IOptionsContainer = interface
['{A593C8BB-53CF-4AA4-9641-BF974E45CBD1}']
function AddSection(aOption : TOptionsClass; const aOptionsName : string = '') : TOptions;
function GetOptions(aOptionClass : TOptionsClass): TOptions;
function GetSection(aOptionsSection : TOptionsClass; aOptions : TOptions) : Boolean; overload;
end;
TSectionList = TObjectList<TOptions>;
IOptionsSerializer = interface
['{7DECE203-4AAE-4C9D-86C8-B3D583DF7C8B}']
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
procedure Save(const aFilename : string; aSections : TSectionList);
end;
TOptionsSerializer = class(TInterfacedObject,IOptionsSerializer)
public
function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; virtual; abstract;
procedure Save(const aFilename : string; aSections : TSectionList); virtual; abstract;
end;
TFileModifiedEvent = reference to procedure;
TLoadConfigEvent = reference to procedure;
TOptionValue<T : TOptions> = class(TInterfacedObject,IOptions<T>)
private
fValue : T;
function GetSectionValue : T;
public
constructor Create(aValue : T);
property Value : T read GetSectionValue;
end;
TOptionsContainer = class(TInterfacedObject,IOptionsContainer)
private
fFilename : string;
fSerializer : IOptionsSerializer;
fSections : TSectionList;
fFileMonitor : TFileMonitor;
fOnFileModified : TFileModifiedEvent;
fLoaded : Boolean;
fReloadIfFileChanged : Boolean;
fOnConfigLoaded : TLoadConfigEvent;
fOnConfigReloaded : TLoadConfigEvent;
procedure CreateFileMonitor;
procedure FileModifiedNotify(MonitorNotify : TMonitorNotify);
procedure SetReloadIfFileChanged(const Value: Boolean);
function GetOptions(aOptionClass : TOptionsClass): TOptions; overload;
function GetOptions(aIndex : Integer) : TOptions; overload;
function GetSection(aOptionsSection : TOptionsClass; aOptions : TOptions) : Boolean; overload;
public
constructor Create(const aFilename : string; aOptionsSerializer : IOptionsSerializer; aReloadIfFileChanged : Boolean = False);
destructor Destroy; override;
property FileName : string read fFilename write fFilename;
property ReloadIfFileChanged : Boolean read fReloadIfFileChanged write SetReloadIfFileChanged;
property IsLoaded : Boolean read fLoaded;
property OnFileModified : TFileModifiedEvent read fOnFileModified write fOnFileModified;
property OnConfigLoaded : TLoadConfigEvent read fOnConfigLoaded write fOnConfigLoaded;
property OnConfigReloaded : TLoadConfigEvent read fOnConfigReloaded write fOnConfigReloaded;
property Items[aOptionClass : TOptionsClass] : TOptions read GetOptions; default;
property Items[aIndex : Integer] : TOptions read GetOptions; default;
function AddSection(aOption : TOptionsClass; const aSectionName : string = '') : TOptions; overload;
function AddSection<T : TOptions>(const aSectionName : string = '') : TOptions<T>; overload;
function GetSectionInterface<T : TOptions> : IOptions<T>;
function GetSection<T : TOptions>(const aSectionName : string = '') : T; overload;
function Count : Integer;
procedure Load(aFailOnSectionNotExists : Boolean = False);
procedure Save;
end;
IOptionsBuilder<T : TOptions> = interface
['{1A1DC9A9-7F2D-4CC4-A772-6C7DBAB34424}']
function Options : T;
end;
TOptionsBuilder<T : TOptions> = class(TInterfacedObject,IOptionsBuilder<T>)
protected
fOptions : T;
public
constructor Create;
function Options : T;
end;
EOptionConfigureError = class(Exception);
EOptionLoadError = class(Exception);
EOptionSaveError = class(Exception);
EOptionValidationError = class(Exception);
implementation
{ TOptionsContainer }
constructor TOptionsContainer.Create(const aFilename : string; aOptionsSerializer : IOptionsSerializer; aReloadIfFileChanged : Boolean = False);
begin
fSerializer := aOptionsSerializer;
fSections := TSectionList.Create(True);
fFilename := aFilename;
fLoaded := False;
fReloadIfFileChanged := aReloadIfFileChanged;
if aReloadIfFileChanged then CreateFileMonitor;
end;
procedure TOptionsContainer.CreateFileMonitor;
begin
fFileMonitor := TQuickFileMonitor.Create;
fFileMonitor.FileName := fFilename;
fFileMonitor.Interval := 2000;
fFileMonitor.Notifies := [TMonitorNotify.mnFileModified];
fFileMonitor.OnFileChange := FileModifiedNotify;
fFileMonitor.Enabled := True;
end;
destructor TOptionsContainer.Destroy;
begin
if Assigned(fFileMonitor) then fFileMonitor.Free;
fSerializer := nil;
fSections.Free;
inherited;
end;
procedure TOptionsContainer.FileModifiedNotify(MonitorNotify: TMonitorNotify);
begin
if MonitorNotify = TMonitorNotify.mnFileModified then
begin
if Assigned(fOnFileModified) then fOnFileModified;
if fReloadIfFileChanged then
begin
Load(False);
end;
end;
end;
function TOptionsContainer.AddSection(aOption : TOptionsClass; const aSectionName : string = '') : TOptions;
var
option : TOptions;
begin
option := aOption.Create;
if aSectionName.IsEmpty then option.Name := Copy(aOption.ClassName,2,aOption.ClassName.Length)
else option.Name := aSectionName;
fSections.Add(option);
Result := option;
end;
function TOptionsContainer.AddSection<T>(const aSectionName: string): TOptions<T>;
var
option : TOptions;
begin
option := TRTTI.CreateInstance<T>;
if aSectionName.IsEmpty then option.Name := Copy(T.ClassName,2,T.ClassName.Length)
else option.Name := aSectionName;
fSections.Add(option);
Result.Create(option);
end;
function TOptionsContainer.Count: Integer;
begin
Result := fSections.Count;
end;
function TOptionsContainer.GetOptions(aIndex: Integer): TOptions;
begin
Result := fSections[aIndex];
end;
function TOptionsContainer.GetSection(aOptionsSection: TOptionsClass; aOptions: TOptions): Boolean;
var
option : TOptions;
begin
Result := False;
for option in fSections do
begin
if option is TOptionsClass then
begin
aOptions := option as TOptionsClass;
Exit;
end;
end;
end;
function TOptionsContainer.GetOptions(aOptionClass : TOptionsClass) : TOptions;
var
option : TOptions;
begin
Result := nil;
for option in fSections do
begin
if option is TOptionsClass then Result := option as TOptionsClass;
end;
end;
function TOptionsContainer.GetSection<T>(const aSectionName : string = '') : T;
var
option : TOptions;
begin
for option in fSections do
begin
if option is T then
begin
if (aSectionName.IsEmpty) or (CompareText(option.Name,aSectionName) = 0) then
begin
Result := option as T;
Exit;
end;
end;
end;
end;
function TOptionsContainer.GetSectionInterface<T>: IOptions<T>;
begin
Result := TOptionValue<T>.Create(Self.GetSection<T>);
end;
procedure TOptionsContainer.Load(aFailOnSectionNotExists : Boolean = False);
var
option : TOptions;
begin
if FileExists(fFilename) then
begin
if not fSerializer.Load(fFilename,fSections,aFailOnSectionNotExists) then Save;
if not fLoaded then
begin
fLoaded := True;
if Assigned(fOnConfigLoaded) then fOnConfigLoaded;
end
else if Assigned(fOnConfigReloaded) then fOnConfigReloaded;
end
else
begin
//if not exists file get default values
for option in fSections do option.DefaultValues;
//creates default file
Save;
end;
end;
procedure TOptionsContainer.Save;
var
laststate : Boolean;
begin
//disable filemonitor to avoid detect manual save as a external file change
laststate := fFileMonitor.Enabled;
fFileMonitor.Enabled := False;
try
//save config file
fSerializer.Save(fFilename,fSections);
finally
//set last state
fFileMonitor.Enabled := laststate;
end;
end;
procedure TOptionsContainer.SetReloadIfFileChanged(const Value: Boolean);
begin
if Value = fReloadIfFileChanged then Exit;
fReloadIfFileChanged := Value;
if Assigned(fFileMonitor) then fFileMonitor.Free;
if fReloadIfFileChanged then CreateFileMonitor;
end;
{ TOptions }
function TOptions.ConfigureOptions<T>(aOptionsFunc: TConfigureOptionsProc<T>): IOptionsValidator;
var
value : TValue;
begin
Result := Self;
if Assigned(aOptionsFunc) then
begin
value := Self;
aOptionsFunc(value.AsType<T>);
end;
end;
constructor TOptions.Create;
begin
fName := '';
end;
procedure TOptions.DoValidateOptions;
var
ctx : TRttiContext;
rtype : TRttiType;
rprop : TRttiProperty;
attrib : TCustomAttribute;
begin
ctx := TRttiContext.Create;
try
rtype := ctx.GetType(Self.ClassInfo);
for rprop in rtype.GetProperties do
begin
//check only published properties
if rprop.Visibility = TMemberVisibility.mvPublished then
begin
//check validation option attributes
for attrib in rprop.GetAttributes do
begin
if attrib is Required then ValidateRequired(rprop)
else if attrib is StringLength then ValidateStringLength(rprop,StringLength(attrib))
else if attrib is Range then ValidateRange(rprop,Range(attrib));
end;
end;
end;
finally
ctx.Free;
end;
end;
procedure TOptions.ValidateOptions;
begin
try
DoValidateOptions;
except
on E : Exception do
begin
raise EOptionConfigureError.CreateFmt('Validation Options Error : %s',[e.Message]);
end;
end;
end;
procedure TOptions.ValidateRange(aProperty: TRttiProperty; aValidation : Range);
var
value : TValue;
msg : string;
begin
value := aProperty.GetValue(Self);
if not value.IsEmpty then
begin
if value.Kind = tkFloat then
begin
if (value.AsExtended < aValidation.Min) or (value.AsExtended > aValidation.Max) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option "%s.%s" exceeds predefined range (%2f - %2f)',[Self.Name,aProperty.Name,aValidation.Min,aValidation.Max])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end
else if value.Kind in [tkInteger,tkInt64] then
begin
if (value.AsInt64 < aValidation.Min) or (value.AsInt64 > aValidation.Max) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option "%s.%s" exceeds predefined range (%d - %d)',[Self.Name,aProperty.Name,Round(aValidation.Min),Round(aValidation.Max)])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end;
end;
end;
procedure TOptions.ValidateRequired(aProperty: TRttiProperty);
begin
if aProperty.GetValue(Self).IsEmpty then raise EOptionValidationError.CreateFmt('Option "%s.%s" is required',[Self.Name,aProperty.Name]);
end;
procedure TOptions.ValidateStringLength(aProperty: TRttiProperty; aValidation : StringLength);
var
value : TValue;
msg : string;
begin
value := aProperty.GetValue(Self);
if (not value.IsEmpty) and (value.AsString.Length > aValidation.MaxLength) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option "%s.%s" exceeds max length (%d)',[Self.Name,aProperty.Name,aValidation.MaxLength])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end;
{ Range }
constructor Range.Create(aMin, aMax: Integer; const aErrorMsg : string = '');
begin
fRangeMin := aMin;
fRangeMax := aMax;
fErrorMsg := aErrorMsg;
end;
constructor Range.Create(aMin, aMax: Double; const aErrorMsg: string);
begin
fRangeMin := aMin;
fRangeMax := aMax;
fErrorMsg := aErrorMsg;
end;
{ StringLength }
constructor StringLength.Create(aMaxLength: Integer; const aErrorMsg : string = '');
begin
fMaxLength := aMaxLength;
fErrorMsg := aErrorMsg;
end;
{ TOptionValue<T> }
constructor TOptionValue<T>.Create(aValue: T);
begin
fValue := aValue;
end;
function TOptionValue<T>.GetSectionValue: T;
begin
Result := fValue;
end;
{ TOptions<T> }
function TOptions<T>.ConfigureOptions(aOptionsFunc: TConfigureOptionsProc<T>): IOptionsValidator;
begin
if Assigned(aOptionsFunc) then Result := fOptions.ConfigureOptions<T>(aOptionsFunc)
else Result := fOptions;
fOptions._AddRef;
end;
constructor TOptions<T>.Create(aOptions: T);
begin
fOptions := aOptions;
end;
{ TOptionsBuilder<T> }
constructor TOptionsBuilder<T>.Create;
begin
fOptions := (PTypeInfo(TypeInfo(T)).TypeData.ClassType.Create) as T;
end;
function TOptionsBuilder<T>.Options: T;
begin
Result := fOptions;
end;
end.