parent
016bcb6624
commit
82cbfb3741
@ -1,18 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using Recyclarr.TrashLib.Models;
|
||||
|
||||
namespace Recyclarr.Cli.Pipelines.CustomFormat.Guide;
|
||||
|
||||
public class CustomFormatParser : ICustomFormatParser
|
||||
{
|
||||
public CustomFormatData ParseCustomFormatData(string guideData, string fileName)
|
||||
{
|
||||
var cf = JsonConvert.DeserializeObject<CustomFormatData>(guideData);
|
||||
if (cf is null)
|
||||
{
|
||||
throw new JsonSerializationException($"Unable to parse JSON at file {fileName}");
|
||||
}
|
||||
|
||||
return cf with {FileName = fileName};
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
using Recyclarr.TrashLib.Models;
|
||||
|
||||
namespace Recyclarr.Cli.Pipelines.CustomFormat.Guide;
|
||||
|
||||
public interface ICustomFormatParser
|
||||
{
|
||||
CustomFormatData ParseCustomFormatData(string guideData, string fileName);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using System.IO.Abstractions;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Recyclarr.Common;
|
||||
|
||||
namespace Recyclarr.TrashLib.Json;
|
||||
|
||||
public record LoadedJsonObject<T>(IFileInfo File, T Obj);
|
||||
|
||||
public class BulkJsonLoader
|
||||
{
|
||||
private readonly ILogger _log;
|
||||
|
||||
public BulkJsonLoader(ILogger log)
|
||||
{
|
||||
_log = log;
|
||||
}
|
||||
|
||||
public ICollection<T> LoadAllFilesAtPaths<T>(
|
||||
IEnumerable<IDirectoryInfo> jsonPaths,
|
||||
Func<IObservable<LoadedJsonObject<T>>, IObservable<T>>? extra = null)
|
||||
{
|
||||
var jsonFiles = JsonUtils.GetJsonFilesInDirectories(jsonPaths, _log);
|
||||
var observable = jsonFiles.ToObservable()
|
||||
.Select(x => Observable.Defer(() => LoadJsonFromFile<T>(x)))
|
||||
.Merge(8);
|
||||
|
||||
var convertedObservable = extra?.Invoke(observable) ?? observable.Select(x => x.Obj);
|
||||
|
||||
return convertedObservable.ToEnumerable().ToList();
|
||||
}
|
||||
|
||||
private static T ParseJson<T>(string guideData, string fileName)
|
||||
{
|
||||
var obj = JsonConvert.DeserializeObject<T>(guideData, GlobalJsonSerializerSettings.Guide);
|
||||
if (obj is null)
|
||||
{
|
||||
throw new JsonSerializationException($"Unable to parse JSON at file {fileName}");
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
private IObservable<LoadedJsonObject<T>> LoadJsonFromFile<T>(IFileInfo file)
|
||||
{
|
||||
return Observable.Using(file.OpenText, x => x.ReadToEndAsync().ToObservable())
|
||||
.Select(x => new LoadedJsonObject<T>(file, ParseJson<T>(x, file.Name)))
|
||||
.Catch((JsonException e) =>
|
||||
{
|
||||
_log.Warning("Failed to parse JSON file: {File} ({Reason})", file.Name, e.Message);
|
||||
return Observable.Empty<LoadedJsonObject<T>>();
|
||||
});
|
||||
}
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Recyclarr.Cli.Pipelines.CustomFormat.Guide;
|
||||
using Recyclarr.Common.Extensions;
|
||||
using Recyclarr.TrashLib.Json;
|
||||
using Recyclarr.TrashLib.Models;
|
||||
|
||||
namespace Recyclarr.Cli.Tests.Pipelines.CustomFormat.Guide;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class CustomFormatParserTest
|
||||
{
|
||||
[Test, AutoMockData]
|
||||
public void Deserialize_works(CustomFormatParser sut)
|
||||
{
|
||||
const string jsonData =
|
||||
"""
|
||||
{
|
||||
"trash_id": "90cedc1fea7ea5d11298bebd3d1d3223",
|
||||
"trash_scores": {
|
||||
"default": -10000,
|
||||
},
|
||||
"name": "EVO (no WEBDL)",
|
||||
"includeCustomFormatWhenRenaming": false,
|
||||
"specifications": [
|
||||
{
|
||||
"name": "EVO",
|
||||
"implementation": "ReleaseTitleSpecification",
|
||||
"negate": false,
|
||||
"required": true,
|
||||
"fields": [{
|
||||
"value": "\\bEVO(TGX)?\\b"
|
||||
}]
|
||||
},
|
||||
{
|
||||
"name": "WEBDL",
|
||||
"implementation": "SourceSpecification",
|
||||
"negate": true,
|
||||
"required": true,
|
||||
"fields": {
|
||||
"value": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "WEBRIP",
|
||||
"implementation": "SourceSpecification",
|
||||
"negate": true,
|
||||
"required": true,
|
||||
"fields": {
|
||||
"value": 8
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
var result = sut.ParseCustomFormatData(jsonData, "file.json");
|
||||
|
||||
result.Should().BeEquivalentTo(new CustomFormatData
|
||||
{
|
||||
FileName = "file.json",
|
||||
TrashId = "90cedc1fea7ea5d11298bebd3d1d3223",
|
||||
TrashScores = {["default"] = -10000},
|
||||
Name = "EVO (no WEBDL)",
|
||||
IncludeCustomFormatWhenRenaming = false,
|
||||
Specifications = new[]
|
||||
{
|
||||
new CustomFormatSpecificationData
|
||||
{
|
||||
Name = "EVO",
|
||||
Implementation = "ReleaseTitleSpecification",
|
||||
Negate = false,
|
||||
Required = true,
|
||||
Fields = new[]
|
||||
{
|
||||
new CustomFormatFieldData
|
||||
{
|
||||
Value = "\\bEVO(TGX)?\\b"
|
||||
}
|
||||
}
|
||||
},
|
||||
new CustomFormatSpecificationData
|
||||
{
|
||||
Name = "WEBDL",
|
||||
Implementation = "SourceSpecification",
|
||||
Negate = true,
|
||||
Required = true,
|
||||
Fields = new[]
|
||||
{
|
||||
new CustomFormatFieldData
|
||||
{
|
||||
Value = 7
|
||||
}
|
||||
}
|
||||
},
|
||||
new CustomFormatSpecificationData
|
||||
{
|
||||
Name = "WEBRIP",
|
||||
Implementation = "SourceSpecification",
|
||||
Negate = true,
|
||||
Required = true,
|
||||
Fields = new[]
|
||||
{
|
||||
new CustomFormatFieldData
|
||||
{
|
||||
Value = 8
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test, AutoMockData]
|
||||
public void Serialize_skips_trash_properties(CustomFormatParser sut)
|
||||
{
|
||||
var cf = new CustomFormatData
|
||||
{
|
||||
FileName = "file.json",
|
||||
TrashId = "90cedc1fea7ea5d11298bebd3d1d3223",
|
||||
TrashScores = {["default"] = -10000},
|
||||
Name = "EVO (no WEBDL)",
|
||||
IncludeCustomFormatWhenRenaming = false
|
||||
};
|
||||
|
||||
var json = JObject.FromObject(cf, JsonSerializer.Create(GlobalJsonSerializerSettings.Services));
|
||||
|
||||
json.Children<JProperty>().Should().NotContain(x => x.Name.ContainsIgnoreCase("trash"));
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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