parent
ef7aa64c5f
commit
9a26348d26
@ -0,0 +1,21 @@
|
||||
using System.IO.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Recyclarr.TrashLib.Json;
|
||||
|
||||
public class GuideJsonLoader : IBulkJsonLoader
|
||||
{
|
||||
private readonly IBulkJsonLoader _loader;
|
||||
|
||||
public GuideJsonLoader(Func<JsonSerializerSettings, IBulkJsonLoader> jsonLoaderFactory)
|
||||
{
|
||||
_loader = jsonLoaderFactory(GlobalJsonSerializerSettings.Guide);
|
||||
}
|
||||
|
||||
public ICollection<T> LoadAllFilesAtPaths<T>(
|
||||
IEnumerable<IDirectoryInfo> jsonPaths,
|
||||
Func<IObservable<LoadedJsonObject<T>>, IObservable<T>>? extra = null)
|
||||
{
|
||||
return _loader.LoadAllFilesAtPaths(jsonPaths, extra);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using System.IO.Abstractions;
|
||||
|
||||
namespace Recyclarr.TrashLib.Json;
|
||||
|
||||
public interface IBulkJsonLoader
|
||||
{
|
||||
ICollection<T> LoadAllFilesAtPaths<T>(
|
||||
IEnumerable<IDirectoryInfo> jsonPaths,
|
||||
Func<IObservable<LoadedJsonObject<T>>, IObservable<T>>? extra = null);
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using Autofac;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Recyclarr.TrashLib.Json;
|
||||
|
||||
public class JsonAutofacModule : Module
|
||||
{
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
base.Load(builder);
|
||||
builder.Register<Func<JsonSerializerSettings, IBulkJsonLoader>>(c =>
|
||||
{
|
||||
return settings => new BulkJsonLoader(c.Resolve<ILogger>(), settings);
|
||||
});
|
||||
|
||||
// Decorators for BulkJsonLoader. We do not use RegisterDecorator() here for these reasons:
|
||||
// - We consume the BulkJsonLoader as a delegate factory, not by instance
|
||||
// - We do not want all implementations of BulkJsonLoader to be decorated, only a specific implementation.
|
||||
builder.RegisterType<GuideJsonLoader>();
|
||||
builder.RegisterType<ServiceJsonLoader>();
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using System.IO.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Recyclarr.TrashLib.Json;
|
||||
|
||||
public class ServiceJsonLoader : IBulkJsonLoader
|
||||
{
|
||||
private readonly IBulkJsonLoader _loader;
|
||||
|
||||
public ServiceJsonLoader(Func<JsonSerializerSettings, IBulkJsonLoader> jsonLoaderFactory)
|
||||
{
|
||||
_loader = jsonLoaderFactory(GlobalJsonSerializerSettings.Services);
|
||||
}
|
||||
|
||||
public ICollection<T> LoadAllFilesAtPaths<T>(
|
||||
IEnumerable<IDirectoryInfo> jsonPaths,
|
||||
Func<IObservable<LoadedJsonObject<T>>, IObservable<T>>? extra = null)
|
||||
{
|
||||
return _loader.LoadAllFilesAtPaths(jsonPaths, extra);
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
using Recyclarr.TrashLib.Json;
|
||||
using Recyclarr.TrashLib.Models;
|
||||
using Recyclarr.TrashLib.TestLibrary;
|
||||
|
||||
namespace Recyclarr.TrashLib.Tests.Json;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class BulkJsonLoaderIntegrationTest : TrashLibIntegrationFixture
|
||||
{
|
||||
[SuppressMessage("ReSharper", "NotAccessedPositionalProperty.Local")]
|
||||
private sealed record TestJsonObject(string TrashId, int TrashScore, string Name);
|
||||
|
||||
[Test]
|
||||
public void Guide_deserialize_works()
|
||||
{
|
||||
var sut = Resolve<GuideJsonLoader>();
|
||||
|
||||
const string jsonData =
|
||||
"""
|
||||
{
|
||||
"trash_id": "90cedc1fea7ea5d11298bebd3d1d3223",
|
||||
"trash_score": "-10000",
|
||||
"name": "TheName"
|
||||
}
|
||||
""";
|
||||
|
||||
Fs.AddFile(Fs.CurrentDirectory().File("file.json"), new MockFileData(jsonData));
|
||||
|
||||
var result = sut.LoadAllFilesAtPaths<TestJsonObject>(new[] {Fs.CurrentDirectory()});
|
||||
|
||||
result.Should().BeEquivalentTo(new[]
|
||||
{
|
||||
new TestJsonObject("90cedc1fea7ea5d11298bebd3d1d3223", -10000, "TheName")
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Service_deserialize_works()
|
||||
{
|
||||
var sut = Resolve<ServiceJsonLoader>();
|
||||
|
||||
const string jsonData =
|
||||
"""
|
||||
{
|
||||
"id": 22,
|
||||
"name": "FUNi",
|
||||
"includeCustomFormatWhenRenaming": true
|
||||
}
|
||||
""";
|
||||
|
||||
Fs.AddFile(Fs.CurrentDirectory().File("file.json"), new MockFileData(jsonData));
|
||||
|
||||
var result = sut.LoadAllFilesAtPaths<CustomFormatData>(new[] {Fs.CurrentDirectory()});
|
||||
|
||||
result.Should().BeEquivalentTo(new[]
|
||||
{
|
||||
new CustomFormatData
|
||||
{
|
||||
Id = 22,
|
||||
Name = "FUNi",
|
||||
IncludeCustomFormatWhenRenaming = true
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
using Recyclarr.TrashLib.Json;
|
||||
|
||||
namespace Recyclarr.TrashLib.Tests.Json;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class BulkJsonLoaderTest
|
||||
{
|
||||
[SuppressMessage("ReSharper", "NotAccessedPositionalProperty.Local")]
|
||||
private sealed record TestJsonObject(string TrashId, int TrashScore, string Name);
|
||||
|
||||
[Test, AutoMockData]
|
||||
public void Deserialize_works(
|
||||
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
|
||||
BulkJsonLoader sut)
|
||||
{
|
||||
var jsonData =
|
||||
"""
|
||||
{
|
||||
"trash_id": "90cedc1fea7ea5d11298bebd3d1d3223",
|
||||
"trash_score": "-10000",
|
||||
"name": "TheName"
|
||||
}
|
||||
""";
|
||||
|
||||
fs.AddFile(fs.CurrentDirectory().File("file.json"), new MockFileData(jsonData));
|
||||
|
||||
var result = sut.LoadAllFilesAtPaths<TestJsonObject>(new[] {fs.CurrentDirectory()});
|
||||
|
||||
result.Should().BeEquivalentTo(new[]
|
||||
{
|
||||
new TestJsonObject("90cedc1fea7ea5d11298bebd3d1d3223", -10000, "TheName")
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue