Quick.Options.Serializer.Yaml.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 : 05/04/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. function LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean; override;
  42. procedure Save(const aFilename : string; aSections : TSectionList); override;
  43. function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
  44. end;
  45. implementation
  46. { TYamlOptionsSerializer }
  47. constructor TYamlOptionsSerializer.Create;
  48. begin
  49. fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
  50. end;
  51. destructor TYamlOptionsSerializer.Destroy;
  52. begin
  53. fSerializer.Free;
  54. inherited;
  55. end;
  56. function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean;
  57. var
  58. yaml : TYamlObject;
  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. if fileoptions.IsEmpty then EOptionLoadError.CreateFmt('Config file "%s" is empty!',[ExtractFileName(aFilename)]);
  85. aYamlObj := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
  86. if aYamlObj = nil then raise EOptionLoadError.CreateFmt('Config file "%s" is damaged or not well-formed Yaml format!',[ExtractFileName(aFilename)]);
  87. end;
  88. Result := aYamlObj <> nil;
  89. end;
  90. function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
  91. var
  92. option : TOptions;
  93. yaml : TYamlObject;
  94. ypair : TYamlPair;
  95. found : Integer;
  96. begin
  97. Result := False;
  98. //read option file
  99. if ParseFile(aFilename,yaml) then
  100. begin
  101. found := 0;
  102. try
  103. for option in aSections do
  104. begin
  105. ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
  106. if ypair = nil then
  107. begin
  108. if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
  109. else
  110. begin
  111. //count as found if hidden
  112. if option.HideOptions then Inc(found);
  113. Continue;
  114. end;
  115. end;
  116. if ypair.Value <> nil then
  117. begin
  118. //deserialize option
  119. fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
  120. //validate loaded configuration
  121. option.ValidateOptions;
  122. Inc(found);
  123. end;
  124. end;
  125. finally
  126. yaml.Free;
  127. end;
  128. //returns true if all sections located into file
  129. Result := found = aSections.Count;
  130. end;
  131. end;
  132. function TYamlOptionsSerializer.LoadSection(const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean;
  133. var
  134. yaml : TYamlObject;
  135. ypair : TYamlPair;
  136. begin
  137. Result := False;
  138. //read option file
  139. if ParseFile(aFilename,yaml) then
  140. begin
  141. try
  142. ypair := fSerializer.GetYamlPairByName(yaml,aOptions.Name);
  143. if (ypair <> nil) and (ypair.Value <> nil) then
  144. begin
  145. //deserialize option
  146. fSerializer.DeserializeObject(aOptions,ypair.Value as TYamlObject);
  147. //validate loaded configuration
  148. aOptions.ValidateOptions;
  149. Result := True;
  150. end
  151. finally
  152. yaml.Free;
  153. end;
  154. end;
  155. end;
  156. procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  157. var
  158. option : TOptions;
  159. fileoptions : string;
  160. yaml : TYamlObject;
  161. jpair : TYamlPair;
  162. begin
  163. yaml := TYamlObject.Create;
  164. try
  165. for option in aSections do
  166. begin
  167. if not option.HideOptions then
  168. begin
  169. //validate configuration before save
  170. option.ValidateOptions;
  171. //serialize option
  172. jpair := fSerializer.Serialize(option.Name,option);
  173. yaml.AddPair(jpair);
  174. end;
  175. end;
  176. fileoptions := yaml.ToYaml;
  177. if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
  178. finally
  179. yaml.Free;
  180. end;
  181. end;
  182. end.