126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using StellaOps.Concelier.Documents;
|
|
using StellaOps.Concelier.Documents.IO;
|
|
using Contracts = StellaOps.Concelier.Storage.Contracts;
|
|
using LegacyContracts = StellaOps.Concelier.Storage;
|
|
|
|
namespace StellaOps.Concelier.Persistence.Postgres;
|
|
|
|
internal static class ContractsMappingExtensions
|
|
{
|
|
private static readonly JsonWriterSettings RelaxedJsonSettings = new()
|
|
{
|
|
OutputMode = JsonOutputMode.RelaxedExtendedJson
|
|
};
|
|
|
|
internal static Contracts.StorageDocument ToStorageDocument(this LegacyContracts.DocumentRecord record)
|
|
{
|
|
return new Contracts.StorageDocument(
|
|
record.Id,
|
|
record.SourceName,
|
|
record.Uri,
|
|
record.CreatedAt,
|
|
record.Sha256,
|
|
record.Status,
|
|
record.ContentType,
|
|
record.Headers,
|
|
record.Metadata,
|
|
record.Etag,
|
|
record.LastModified,
|
|
record.PayloadId,
|
|
record.ExpiresAt,
|
|
record.Payload,
|
|
record.FetchedAt);
|
|
}
|
|
|
|
internal static LegacyContracts.DocumentRecord ToLegacyDocumentRecord(this Contracts.StorageDocument record)
|
|
{
|
|
return new LegacyContracts.DocumentRecord(
|
|
record.Id,
|
|
record.SourceName,
|
|
record.Uri,
|
|
record.CreatedAt,
|
|
record.Sha256,
|
|
record.Status,
|
|
record.ContentType,
|
|
record.Headers,
|
|
record.Metadata,
|
|
record.Etag,
|
|
record.LastModified,
|
|
record.PayloadId,
|
|
record.ExpiresAt,
|
|
record.Payload,
|
|
record.FetchedAt);
|
|
}
|
|
|
|
internal static Contracts.StorageDto ToStorageDto(this LegacyContracts.DtoRecord record)
|
|
{
|
|
var json = record.Payload.ToJson(RelaxedJsonSettings);
|
|
var payload = JsonDocument.Parse(json);
|
|
return new Contracts.StorageDto(
|
|
record.Id,
|
|
record.DocumentId,
|
|
record.SourceName,
|
|
record.Format,
|
|
payload,
|
|
record.CreatedAt,
|
|
record.SchemaVersion,
|
|
record.ValidatedAt);
|
|
}
|
|
|
|
internal static LegacyContracts.DtoRecord ToLegacyDtoRecord(this Contracts.StorageDto record)
|
|
{
|
|
var json = record.Payload.RootElement.GetRawText();
|
|
var doc = DocumentObject.Parse(json);
|
|
return new LegacyContracts.DtoRecord(
|
|
record.Id,
|
|
record.DocumentId,
|
|
record.SourceName,
|
|
record.Format,
|
|
doc,
|
|
record.CreatedAt,
|
|
record.SchemaVersion,
|
|
record.ValidatedAt);
|
|
}
|
|
|
|
internal static Contracts.SourceCursorState ToStorageCursorState(this LegacyContracts.SourceStateRecord record)
|
|
{
|
|
var cursorJson = record.Cursor is null ? null : record.Cursor.ToJson(RelaxedJsonSettings);
|
|
var cursor = cursorJson is null ? null : JsonDocument.Parse(cursorJson);
|
|
return new Contracts.SourceCursorState(
|
|
record.SourceName,
|
|
record.Enabled,
|
|
record.Paused,
|
|
cursor,
|
|
record.LastSuccess,
|
|
record.LastFailure,
|
|
record.FailCount,
|
|
record.BackoffUntil,
|
|
record.UpdatedAt,
|
|
record.LastFailureReason);
|
|
}
|
|
|
|
internal static LegacyContracts.SourceStateRecord ToLegacySourceStateRecord(this Contracts.SourceCursorState record)
|
|
{
|
|
var docCursor = record.Cursor is null ? null : DocumentObject.Parse(record.Cursor.RootElement.GetRawText());
|
|
return new LegacyContracts.SourceStateRecord(
|
|
record.SourceName,
|
|
record.Enabled,
|
|
record.Paused,
|
|
docCursor,
|
|
record.LastSuccess,
|
|
record.LastFailure,
|
|
record.FailCount,
|
|
record.BackoffUntil,
|
|
record.UpdatedAt,
|
|
record.LastFailureReason);
|
|
}
|
|
|
|
internal static DocumentObject ToDocumentObject(this JsonDocument document)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(document);
|
|
return DocumentObject.Parse(document.RootElement.GetRawText());
|
|
}
|
|
}
|