Skip to content

Commit

Permalink
✨ Adds FileInfoToString converter
Browse files Browse the repository at this point in the history
  • Loading branch information
akritikos committed Feb 16, 2021
1 parent 19f3ce6 commit c2f5583
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace Kritikos.Configuration.Persistence.Converters
{
using System.IO;

using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

/// <summary>
/// Converts from <seealso cref="FileInfo"/> to and from string.
/// </summary>
public class FileInfoToStringConverter : ValueConverter<FileInfo, string>
{
/// <summary>
/// Initializes a new instance of the <seealso cref="FileInfoToStringConverter"/> class.
/// </summary>
/// <param name="separator">Character used as directory separator in the persistence layer.</param>
/// <param name="basePath"><seealso cref="DirectoryInfo"/> used as path base when handling relative paths.</param>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public FileInfoToStringConverter(
char separator,
FileSystemInfo? basePath = null,
ConverterMappingHints? mappingHints = null)
: base(
v => FromFileInfo(basePath, v, separator),
v => FromPath(basePath, v, separator),
mappingHints)
{
}

private static FileInfo FromPath(FileSystemInfo? basePath, string filePath, char separator)
{
var path = (basePath == null
? filePath
: Path.Combine(basePath.FullName, filePath))
.Replace(separator, Path.DirectorySeparatorChar);

return new FileInfo(path);
}

private static string FromFileInfo(FileSystemInfo? basePath, FileSystemInfo file, char separator)
{
var path = file.FullName;
path = basePath != null
? path
.Replace(basePath?.FullName ?? string.Empty, string.Empty)[1..]
: path;

path = separator != '\\' && Path.DirectorySeparatorChar == '\\' && Path.GetPathRoot(path).Length - 1 > 0
? path[(Path.GetPathRoot(path).Length - 1)..]
: path;
path = path.Replace(Path.DirectorySeparatorChar, separator);

return path;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Kritikos.Configuration.PersistenceTests.ConverterTests
{
using System.IO;

using FluentAssertions;

using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

using Xunit;

public class FileInfoToStringConverter
{
private const string WindowsBase = @"C:\Windows\System32";
private const string WindowsRelative = @"drivers\etc\hosts";

private const string WindowsPath = @"C:\Windows\System32\drivers\etc\hosts";

private const string LinuxBase = @"/srv/http";
private const string LinuxRelative = @"root/index.html";
private const string LinuxPath = @"/srv/http/root/index.html";

private static readonly ConverterMappingHints MappingHints = new(unicode: true);

[Fact]
public void Relative_path_windows()
{
var converter =
new Persistence.Converters.FileInfoToStringConverter('\\', new DirectoryInfo(WindowsBase), MappingHints);

var file = converter.ConvertFromProvider(WindowsRelative) as FileInfo;
var foo = converter.ConvertToProvider(file) as string;

file.FullName.Should().Be(WindowsPath);
foo.Should().Be(WindowsRelative);
}

[Fact]
public void Absolute_path_windows()
{
var converter =
new Persistence.Converters.FileInfoToStringConverter('\\', mappingHints: MappingHints);

var file = converter.ConvertFromProvider(WindowsPath) as FileInfo;
var foo = converter.ConvertToProvider(file) as string;

file.FullName.Should().Be(WindowsPath);
foo.Should().Be(WindowsPath);
}

[Fact]
public void Relative_path_linux()
{
var converter =
new Persistence.Converters.FileInfoToStringConverter('/', new DirectoryInfo(LinuxBase),
mappingHints: MappingHints);

var file = converter.ConvertFromProvider(LinuxRelative) as FileInfo;
var foo = converter.ConvertToProvider(file) as string;

foo.Should().Be(LinuxRelative);
}

[Fact]
public void Absolute_path_linux()
{
var converter =
new Persistence.Converters.FileInfoToStringConverter('/', mappingHints: MappingHints);

var file = converter.ConvertFromProvider(LinuxPath) as FileInfo;
var foo = converter.ConvertToProvider(file) as string;

foo.Should().Be(LinuxPath);
}
}
}

0 comments on commit c2f5583

Please sign in to comment.