refactor(archival): ♻ extract ListFileName ValueObject

This commit is contained in:
Collin M. Barrett 2020-11-14 14:00:08 -06:00
parent 74f22b822b
commit 32aff9916c
4 changed files with 28 additions and 8 deletions

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
@ -85,7 +84,7 @@ private ListArchive GetList(
CancellationToken cancellationToken)
{
var segmentsAsync = GetSegmentsAsync(segmentUrls, cancellationToken);
var target = listId.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0');
var target = new ListFileName(listId);
return new ListArchive(segmentsAsync, target);
}

View file

@ -4,13 +4,13 @@ namespace FilterLists.Archival.Domain.Lists
{
public class ListArchive
{
public ListArchive(IAsyncEnumerable<ListArchiveSegment> segments, string targetFileName)
public ListArchive(IAsyncEnumerable<ListArchiveSegment> segments, ListFileName targetFileName)
{
Segments = segments;
TargetFileName = targetFileName;
}
public IAsyncEnumerable<ListArchiveSegment> Segments { get; }
public string TargetFileName { get; }
public ListFileName TargetFileName { get; }
}
}

View file

@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Globalization;
using FilterLists.Archival.Domain.SeedWork;
namespace FilterLists.Archival.Domain.Lists
{
public sealed class ListFileName : ValueObject
{
public ListFileName(int listId)
{
Value = listId.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0');
}
public string Value { get; }
protected override IEnumerable<object> GetEqualityComponents()
{
return new[] {Value};
}
}
}

View file

@ -41,7 +41,7 @@ public async Task AddAsync(ListArchive listArchive, CancellationToken cancellati
_logger.LogWarning(
"No stream to plain text conversion strategy found for extension {Extension} for target {Target}. Skipping list",
segment.Extension,
listArchive.TargetFileName);
listArchive.TargetFileName.Value);
return;
}
@ -51,7 +51,7 @@ public async Task AddAsync(ListArchive listArchive, CancellationToken cancellati
_logger.LogWarning(
"Writing from non-plain text extension {Extension} for target {Target} not yet supported. Skipping list",
segment.Extension,
listArchive.TargetFileName);
listArchive.TargetFileName.Value);
return;
}
@ -99,7 +99,7 @@ public void Dispose()
_repo.CheckoutPaths("HEAD", _writtenFiles.Select(f => f.Name));
}
private FileInfo? GetTargetFile(string baseTargetFileName, int segmentNumber, ListFileExtension extension)
private FileInfo? GetTargetFile(ListFileName baseTargetFileName, int segmentNumber, ListFileExtension extension)
{
string targetExtension;
if (extension.IsPlainText)
@ -112,7 +112,7 @@ public void Dispose()
return default;
}
var targetFileName = baseTargetFileName +
var targetFileName = baseTargetFileName.Value +
(segmentNumber == 1 ? string.Empty : $"-{segmentNumber}") +
targetExtension;
return new FileInfo(Path.Combine(_options.RepositoryPath, targetFileName));