Quick.Options.Serializer.Yaml.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. { ***************************************************************************
  2. Copyright (c) 2015-2020 Kike Pérez
  3. Unit : Quick.Options.Serializer.Yaml
  4. Description : Configuration groups Yaml Serializer
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 18/10/2019
  8. Modified : 07/02/2020
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Options.Serializer.Yaml;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. System.IOUtils,
  27. System.Generics.Collections,
  28. Quick.YAML,
  29. Quick.Commons,
  30. Quick.YAML.Serializer,
  31. Quick.Options;
  32. type
  33. TYamlOptionsSerializer = class(TOptionsSerializer)
  34. private
  35. fSerializer : TRTTIYaml;
  36. function ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
  37. public
  38. constructor Create;
  39. destructor Destroy; override;
  40. function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
  41. procedure Save(const aFilename : string; aSections : TSectionList); override;
  42. function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
  43. end;
  44. implementation
  45. { TYamlOptionsSerializer }
  46. constructor TYamlOptionsSerializer.Create;
  47. begin
  48. fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
  49. end;
  50. destructor TYamlOptionsSerializer.Destroy;
  51. begin
  52. fSerializer.Free;
  53. inherited;
  54. end;
  55. function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean;
  56. var
  57. yaml : TYamlObject;
  58. ypair : TYamlPair;
  59. i : Integer;
  60. begin
  61. Result := False;
  62. yaml := nil;
  63. if ParseFile(aFilename,yaml) then
  64. begin
  65. try
  66. for i := 0 to yaml.Count - 1 do
  67. begin
  68. oSections := oSections + [yaml.Pairs[i].Name];
  69. end;
  70. Result := True;
  71. finally
  72. yaml.Free;
  73. end;
  74. end;
  75. end;
  76. function TYamlOptionsSerializer.ParseFile(const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
  77. var
  78. fileoptions : string;
  79. begin
  80. aYamlObj := nil;
  81. if FileExists(aFilename) then
  82. begin
  83. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  84. aYamlObj := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
  85. end;
  86. Result := aYamlObj <> nil;
  87. end;
  88. function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
  89. var
  90. option : TOptions;
  91. fileoptions : string;
  92. yaml : TYamlObject;
  93. ypair : TYamlPair;
  94. begin
  95. Result := False;
  96. //read option file
  97. if ParseFile(aFilename,yaml) then
  98. begin
  99. try
  100. for option in aSections do
  101. begin
  102. ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
  103. if ypair = nil then
  104. begin
  105. if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
  106. else Continue;
  107. end;
  108. if ypair.Value <> nil then
  109. begin
  110. //deserialize option
  111. fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
  112. //validate loaded configuration
  113. option.ValidateOptions;
  114. end;
  115. end;
  116. finally
  117. yaml.Free;
  118. end;
  119. end;
  120. end;
  121. procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  122. var
  123. option : TOptions;
  124. fileoptions : string;
  125. yaml : TYamlObject;
  126. jpair : TYamlPair;
  127. begin
  128. yaml := TYamlObject.Create;
  129. try
  130. for option in aSections do
  131. begin
  132. if not option.HideOptions then
  133. begin
  134. //validate configuration before save
  135. option.ValidateOptions;
  136. //serialize option
  137. jpair := fSerializer.Serialize(option.Name,option);
  138. yaml.AddPair(jpair);
  139. end;
  140. end;
  141. fileoptions := yaml.ToYaml;
  142. if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
  143. finally
  144. yaml.Free;
  145. end;
  146. end;
  147. end.