Skip to content

Commit

Permalink
use IProxyMapper in root converter
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Jun 14, 2024
1 parent 808232c commit 4099052
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@
using Speckle.Converters.Common.Objects;
using Speckle.Core.Models;
using Speckle.InterfaceGenerator;
using Speckle.Revit.Interfaces;

namespace Speckle.Converters.Common;

[GenerateAutoInterface]
public class RootToSpeckleConverter : IRootToSpeckleConverter
{
private readonly IFactory<IToSpeckleTopLevelConverter> _toSpeckle;
private readonly IProxyMapper _proxyMapper;

public RootToSpeckleConverter(IFactory<IToSpeckleTopLevelConverter> toSpeckle)
public RootToSpeckleConverter(IFactory<IToSpeckleTopLevelConverter> toSpeckle, IProxyMapper proxyMapper)
{
_toSpeckle = toSpeckle;
_proxyMapper = proxyMapper;
}

public Base Convert(Type type, object target)
public Base Convert(object target)
{
Type revitType = target.GetType();
var wrapper = _proxyMapper.WrapIfExists(revitType, target);
if (wrapper == null)
{
throw new NotSupportedException($"No wrapper found for Revit type: {revitType.Name}");
}
var (wrappedType, wrappedObject) = wrapper.Value;
try
{
var objectConverter = _toSpeckle.ResolveInstance(type.Name); //poc: would be nice to have supertypes resolve
var objectConverter = _toSpeckle.ResolveInstance(wrappedType.Name); //poc: would be nice to have supertypes resolve

if (objectConverter == null)
{
throw new NotSupportedException($"No conversion found for {type.Name}");
throw new NotSupportedException($"No conversion found for {wrappedType.Name}");
}
var convertedObject = objectConverter.Convert(target);
var convertedObject = objectConverter.Convert(wrappedObject);

return convertedObject;
}
Expand Down

0 comments on commit 4099052

Please sign in to comment.