From 6acf87ef0dc965d3b6f9aeeb0fb2cf2a61746e25 Mon Sep 17 00:00:00 2001 From: Benito Palacios Sanchez Date: Sat, 19 Aug 2023 08:01:02 +0200 Subject: [PATCH] :sparkles: Add overload to use TransformWith without generics --- src/.editorconfig | 2 +- src/Yarhl.UnitTests/PluginManagerTests.cs | 24 ++++++++++- src/Yarhl/FileSystem/Node.cs | 52 ++++++++++++++++------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/.editorconfig b/src/.editorconfig index c020adb9..474b50b0 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -10,7 +10,7 @@ indent_size = 4 indent_style = space tab_width = 8 trim_trailing_whitespace = true -end_of_line = lf +end_of_line = unset # Hard to enforce on Windows, Git will take care insert_final_newline = true ## .NET style rules diff --git a/src/Yarhl.UnitTests/PluginManagerTests.cs b/src/Yarhl.UnitTests/PluginManagerTests.cs index 7fb07b86..1a5c9e99 100644 --- a/src/Yarhl.UnitTests/PluginManagerTests.cs +++ b/src/Yarhl.UnitTests/PluginManagerTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2019 SceneGate +// Copyright (c) 2019 SceneGate // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -198,6 +198,15 @@ public void GetConvertersWithMetadataReturnsListWithMetadata() Assert.That(conv.Convert(new PluginFormat()), Is.EqualTo(0)); } + [Test] + [Ignore("To be fixed")] + public void GetConvertersWithParametersReturnsMetadata() + { + var formats = PluginManager.Instance.GetConverters() + .Select(f => f.Metadata.Type); + Assert.That(formats, Does.Contain(typeof(PluginConverterParametrized))); + } + [Export(typeof(IExistsInterface))] public class ExistsClass : IExistsInterface { @@ -229,5 +238,18 @@ public int Convert(PluginFormat source) return PluginFormat.Value; } } + + [PartNotDiscoverable] // TODO: remove attribute + public class PluginConverterParametrized : IConverter + { + public PluginConverterParametrized(bool ignoreMe) + { + } + + public int Convert(PluginFormat source) + { + return PluginFormat.Value; + } + } } } diff --git a/src/Yarhl/FileSystem/Node.cs b/src/Yarhl/FileSystem/Node.cs index 31cdb0c3..a1dd1ff3 100644 --- a/src/Yarhl/FileSystem/Node.cs +++ b/src/Yarhl/FileSystem/Node.cs @@ -218,23 +218,9 @@ public Node TransformWith() if (Disposed) throw new ObjectDisposedException(nameof(Node)); - if (Format is null) { - throw new InvalidOperationException( - "Cannot transform a node without format"); - } - - ConvertFormat.ValidateConverterType(typeof(TConv), Format.GetType()); - // This is a valid use of ConvertFormat.With as we don't care about - // the returned type. As the method was deprecated we replicate - // the same logic here. - dynamic converter = new TConv(); - dynamic input = Format; // passing an object variable doesn't work - object result = converter.Convert(input); - - CastAndChangeFormat(result); - - return this; + // the returned type. There isn't anything to do type safety. + return TransformWith(typeof(TConv)); } /// @@ -290,6 +276,40 @@ public Node TransformWith(Type converterType, params object?[] args) return this; } + /// + /// Transform the node format to another format using a converter. + /// + /// + /// This API may behave slower and produce less obvious exceptions. + /// It's recommended to use instead + /// . + /// + /// Convert to use. + /// This node. + public Node TransformWith(IConverter converter) + { + if (Disposed) + throw new ObjectDisposedException(nameof(Node)); + + if (converter == null) + throw new ArgumentNullException(nameof(converter)); + + if (Format is null) { + throw new InvalidOperationException( + "Cannot transform a node without format"); + } + + ConvertFormat.ValidateConverterType(converter.GetType(), Format.GetType()); + + dynamic converterDyn = converter; + dynamic source = Format; + IFormat newFormat = converterDyn.Convert(source); + + ChangeFormat(newFormat); + + return this; + } + /// /// Transform the node format to another format using a converter. ///