unit XData.JSchema.Builder;

interface

uses
  TypInfo,
  XData.Aurelius.Model,
  XData.JSchema.Classes,
  XData.JSchema.Generators;

type
  TJSchemaBuilder = class
  strict private
    FGenerators: TJSchemaGenerators;
    function GetGenerators: TJSchemaGenerators;
  protected
    function CreateGenerators: TJSchemaGenerators; virtual;
  public
    destructor Destroy; override;
    function Build(ATypeInfo: PTypeInfo): TJsonSchema; overload;
    function Build<T>: TJsonSchema; overload;
    property Generators: TJSchemaGenerators read GetGenerators;
  end;

  TXDataJSchemaBuilder = class(TJSchemaBuilder)
  strict private
    FModel: TXDataAureliusModel;
  protected
    property Model: TXDataAureliusModel read FModel;
  strict protected
    function CreateGenerators: TJSchemaGenerators; override;
  public
    constructor Create(AModel: TXDataAureliusModel);
  end;

implementation

{ TJSchemaBuilder }

function TJSchemaBuilder.Build(ATypeInfo: PTypeInfo): TJsonSchema;
begin
  Result := Generators.Get(ATypeInfo).CreateSchema;
end;

function TJSchemaBuilder.Build<T>: TJsonSchema;
begin
  Result := Build(TypeInfo(T));
end;

function TJSchemaBuilder.CreateGenerators: TJSchemaGenerators;
begin
  Result := TJSchemaGenerators.Create;
end;

destructor TJSchemaBuilder.Destroy;
begin
  FGenerators.Free;
  inherited;
end;

function TJSchemaBuilder.GetGenerators: TJSchemaGenerators;
begin
  if FGenerators = nil then
    FGenerators := CreateGenerators;
  Result := FGenerators;
end;

{ TXDataJSchemaBuilder }

constructor TXDataJSchemaBuilder.Create(AModel: TXDataAureliusModel);
begin
  FModel := AModel;
end;

function TXDataJSchemaBuilder.CreateGenerators: TJSchemaGenerators;
begin
  Result := TXDataJSchemaGenerators.Create(Self.Model);
end;

end.
