Skip to content

Commit

Permalink
✨ Add overload to use TransformWith without generics
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Aug 19, 2023
1 parent bf668f4 commit 6acf87e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion src/Yarhl.UnitTests/PluginManagerTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -229,5 +238,18 @@ public int Convert(PluginFormat source)
return PluginFormat.Value;
}
}

[PartNotDiscoverable] // TODO: remove attribute
public class PluginConverterParametrized : IConverter<PluginFormat, int>
{
public PluginConverterParametrized(bool ignoreMe)
{
}

public int Convert(PluginFormat source)
{
return PluginFormat.Value;
}
}
}
}
52 changes: 36 additions & 16 deletions src/Yarhl/FileSystem/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,9 @@ public Node TransformWith<TConv>()
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));
}

/// <summary>
Expand Down Expand Up @@ -290,6 +276,40 @@ public Node TransformWith(Type converterType, params object?[] args)
return this;
}

/// <summary>
/// Transform the node format to another format using a converter.
/// </summary>
/// <remarks>
/// This API may behave slower and produce less obvious exceptions.
/// It's recommended to use instead
/// <see cref="TransformWith{TSrc, TDst}(IConverter{TSrc, TDst})"/>.
/// </remarks>
/// <param name="converter">Convert to use.</param>
/// <returns>This node.</returns>
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;
}

/// <summary>
/// Transform the node format to another format using a converter.
/// </summary>
Expand Down

0 comments on commit 6acf87e

Please sign in to comment.