Quick.Options.Serializer.Yaml.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. { ***************************************************************************
  2. Copyright (c) 2015-2019 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 : 28/11/2019
  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. public
  37. constructor Create;
  38. destructor Destroy; override;
  39. function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
  40. procedure Save(const aFilename : string; aSections : TSectionList); override;
  41. end;
  42. implementation
  43. { TYamlOptionsSerializer }
  44. constructor TYamlOptionsSerializer.Create;
  45. begin
  46. fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
  47. end;
  48. destructor TYamlOptionsSerializer.Destroy;
  49. begin
  50. fSerializer.Free;
  51. inherited;
  52. end;
  53. function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
  54. var
  55. option : TOptions;
  56. fileoptions : string;
  57. yaml : TYamlObject;
  58. ypair : TYamlPair;
  59. begin
  60. Result := False;
  61. if FileExists(aFilename) then
  62. begin
  63. //read option file
  64. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  65. yaml := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
  66. for option in aSections do
  67. begin
  68. ypair := fSerializer.GetYamlPairByName(yaml,option.Name);
  69. if ypair = nil then
  70. begin
  71. if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
  72. else Continue;
  73. end;
  74. if ypair.Value <> nil then
  75. begin
  76. //deserialize option
  77. fSerializer.DeserializeObject(option,ypair.Value as TYamlObject);
  78. //validate loaded configuration
  79. option.ValidateOptions;
  80. end;
  81. end;
  82. end;
  83. end;
  84. procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  85. var
  86. option : TOptions;
  87. fileoptions : string;
  88. yaml : TYamlObject;
  89. jpair : TYamlPair;
  90. begin
  91. yaml := TYamlObject.Create;
  92. try
  93. for option in aSections do
  94. begin
  95. //validate configuration before save
  96. option.ValidateOptions;
  97. //serialize option
  98. jpair := fSerializer.Serialize(option.Name,option);
  99. yaml.AddPair(jpair);
  100. end;
  101. fileoptions := yaml.ToYaml;
  102. TFile.WriteAllText(aFilename,fileoptions);
  103. finally
  104. yaml.Free;
  105. end;
  106. end;
  107. end.