Skip to content

Commit

Permalink
Add Result objects for Rhino
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Dec 18, 2024
1 parent f924a59 commit 01c5f8e
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,21 @@ CancellationToken cancellationToken

// 2: convert
var result = _converter.Convert(obj);
if (!result.IsSuccess)
{
conversionResults.Add(new(Status.ERROR, obj, result.Message.NotNull()));
convertActivity?.SetStatus(SdkActivityStatusCode.Error);
continue;
}

// 3: bake
var conversionIds = new List<string>();
if (result is GeometryBase geometryBase)
if (result.Host is GeometryBase geometryBase)
{
var guid = BakeObject(geometryBase, obj, null, atts);
conversionIds.Add(guid.ToString());
}
else if (result is List<GeometryBase> geometryBases) // one to many raw encoding case
else if (result.Host is List<GeometryBase> geometryBases) // one to many raw encoding case
{
// NOTE: I'm unhappy about this case (dim). It's needed as the raw encoder approach can hypothetically return
// multiple "geometry bases" - but this is not a fallback conversion.
Expand All @@ -167,7 +173,7 @@ CancellationToken cancellationToken
conversionIds.Add(guid.ToString());
}
}
else if (result is IEnumerable<(object, Base)> fallbackConversionResult) // one to many fallback conversion
else if (result.Host is IEnumerable<(object, Base)> fallbackConversionResult) // one to many fallback conversion
{
var guids = BakeObjectsAsFallbackGroup(fallbackConversionResult, obj, atts, baseLayerName);
conversionIds.AddRange(guids.Select(id => id.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Speckle.Converters.Common;
using Speckle.Converters.Rhino;
using Speckle.Sdk;
using Speckle.Sdk.Common;
using Speckle.Sdk.Logging;
using Speckle.Sdk.Models;
using Speckle.Sdk.Models.Collections;
Expand Down Expand Up @@ -159,8 +160,17 @@ string projectId
}
else
{
converted = _rootToSpeckleConverter.Convert(rhinoObject);
converted.applicationId = applicationId;
var result = _rootToSpeckleConverter.Convert(rhinoObject);
if (result.IsSuccess)
{
converted = result.Base.NotNull();
converted.applicationId = applicationId;
}
else
{
_logger.LogSendConversionError(sourceType, result.Message.NotNull());
return new(Status.ERROR, applicationId, sourceType);
}
}

// add to host
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Sdk.Common;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;
Expand All @@ -21,7 +22,7 @@ ITypedConverter<TIn, TOut> geometryBaseConverter
_geometryBaseConverter = geometryBaseConverter;
}

public object Convert(Base target)
public HostResult Convert(Base target)
{
var castedBase = (TIn)target;
var result = _geometryBaseConverter.Convert(castedBase);
Expand All @@ -37,7 +38,7 @@ public object Convert(Base target)
if (result is RG.GeometryBase geometryBase && units is not null)
{
geometryBase.Transform(GetScaleTransform(units));
return geometryBase;
return HostResult.Success(geometryBase);
}

if (result is List<RG.GeometryBase> geometryBases && units is not null)
Expand All @@ -48,10 +49,10 @@ public object Convert(Base target)
gb.Transform(t);
}

return geometryBases;
return HostResult.Success(geometryBases);
}

return result;
return HostResult.Success(result);
}

private RG.Transform GetScaleTransform(string from)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Sdk.Common;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;
Expand Down Expand Up @@ -35,7 +36,7 @@ IConverterSettingsStore<RhinoConversionSettings> settingsStore
_settingsStore = settingsStore;
}

public object Convert(Base target) => Convert((DisplayableObject)target);
public HostResult Convert(Base target) => HostResult.Success(Convert((DisplayableObject)target));

public List<RG.GeometryBase> Convert(DisplayableObject target)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Rhino.DocObjects;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Converters.Rhino.ToSpeckle.Encoding;
using Speckle.Converters.Rhino.ToSpeckle.Meshing;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Rhino.ToSpeckle.TopLevel;

Expand All @@ -22,7 +22,7 @@ IConverterSettingsStore<RhinoConversionSettings> settingsStore
_settingsStore = settingsStore;
}

public Base Convert(object target)
public BaseResult Convert(object target)
{
var brepObject = (BrepObject)target;
var brepEncoding = RawEncodingCreator.Encode(brepObject.Geometry, _settingsStore.Current.Document);
Expand All @@ -37,6 +37,6 @@ public Base Convert(object target)
units = _settingsStore.Current.SpeckleUnits
};

return bx;
return BaseResult.Success(bx);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Rhino.DocObjects;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Converters.Rhino.ToSpeckle.Encoding;
using Speckle.Converters.Rhino.ToSpeckle.Meshing;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Rhino.ToSpeckle.TopLevel;

Expand All @@ -22,7 +22,7 @@ IConverterSettingsStore<RhinoConversionSettings> settingsStore
_settingsStore = settingsStore;
}

public Base Convert(object target)
public BaseResult Convert(object target)
{
var extrusionObject = (ExtrusionObject)target;
var extrusionEncoding = RawEncodingCreator.Encode(extrusionObject.Geometry, _settingsStore.Current.Document);
Expand All @@ -37,6 +37,6 @@ public Base Convert(object target)
units = _settingsStore.Current.SpeckleUnits
};

return bx;
return BaseResult.Success(bx);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Sdk.Models;
using RhinoObject = Rhino.DocObjects.RhinoObject;

Expand All @@ -19,7 +20,7 @@ protected RhinoObjectToSpeckleTopLevelConverter(ITypedConverter<TInRaw, TOutRaw>
// POC: IIndex would fix this as I would just request the type from `RhinoObject.Geometry` directly.
protected abstract TInRaw GetTypedGeometry(TTopLevelIn input);

public virtual Base Convert(object target)
public BaseResult Convert(object target)
{
var typedTarget = (TTopLevelIn)target;
var typedGeometry = GetTypedGeometry(typedTarget);
Expand All @@ -33,6 +34,6 @@ public virtual Base Convert(object target)
result["name"] = typedTarget.Attributes.Name;
}

return result;
return BaseResult.Success( result);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Rhino.DocObjects;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Converters.Rhino.ToSpeckle.Encoding;
using Speckle.Converters.Rhino.ToSpeckle.Meshing;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Rhino.ToSpeckle.TopLevel;

Expand All @@ -22,7 +22,7 @@ IConverterSettingsStore<RhinoConversionSettings> settingsStore
_settingsStore = settingsStore;
}

public Base Convert(object target)
public BaseResult Convert(object target)
{
var subDObject = (SubDObject)target;
var subdEncoding = RawEncodingCreator.Encode(subDObject.Geometry, _settingsStore.Current.Document);
Expand All @@ -37,6 +37,6 @@ public Base Convert(object target)
units = _settingsStore.Current.SpeckleUnits
};

return bx;
return BaseResult.Success(bx);
}
}
12 changes: 12 additions & 0 deletions Sdk/Speckle.Connectors.Common/Conversion/ReportResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public ReceiveConversionResult(
ResultType = resultType;
Error = FormatError(exception);
}

public ReceiveConversionResult(
Status status,
Base source,
string exception
)
{
Status = status;
SourceId = source.id.NotNull();
SourceType = source.speckle_type; // Note: we'll parse it nicely in FE
Error = new ErrorWrapper() { Message = exception, StackTrace = "" };
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ string objectType

logger.Log(logLevel, ex, "Conversion of object {objectType} was not successful", objectType);
}

public static void LogSendConversionError<T>(
this ILogger<IRootObjectBuilder<T>> logger,
string objectType,
string message
)
{
logger.Log(LogLevel.Information, "Conversion of object {objectType} was not successful:" + message, objectType);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
using Microsoft.Extensions.Logging;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Registration;
using Speckle.Sdk;
using Speckle.Sdk.Common;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Common.Extensions;

public static class ToHostTopLevelConverterExtension
{
public static object ConvertAndLog(this IToHostTopLevelConverter converter, Base target, ILogger logger)
public static HostResult ConvertAndLog(this ConverterResult<IToHostTopLevelConverter> converter, Base target, ILogger logger)
{
try
{
if (converter.IsSuccess)
{
return converter.Converter.NotNull().Convert(target);
}

logger.Log(
LogLevel.Information,
"Conversion of object {target} using {converter} was not successful",
target.GetType(),
converter.GetType()
);
return HostResult.NoConverter(converter.Message);
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
//SpeckleExceptions are expected, if a converter throws anything else, its considered an error that we should investigate and fix
LogLevel logLevel = ex switch
{
SpeckleException => LogLevel.Information, //If it's too noisy, we could demote to LogLevel.Debug
_ => LogLevel.Error
};

logger.Log(
logLevel,
ex,
"Conversion of object {target} using {converter} was not successful",
target.GetType(),
converter.GetType()
);

return HostResult.NoConversion($"Conversion of object {target} using {converter} was not successful: " + ex.Message);
}
}
public static HostResult ConvertAndLog(this IToHostTopLevelConverter converter, Base target, ILogger logger)
{
try
{
Expand Down
3 changes: 2 additions & 1 deletion Sdk/Speckle.Converters.Common/IRootToHostConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Speckle.Converters.Common.Registration;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Common;

public interface IRootToHostConverter
{
object Convert(Base target);
HostResult Convert(Base target);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Speckle.Converters.Common.Registration;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Common.Objects;

public interface IToHostTopLevelConverter
{
object Convert(Base target);
HostResult Convert(Base target);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Speckle.Sdk.Models;
using Speckle.Converters.Common.Registration;

namespace Speckle.Converters.Common.Objects;

public interface IToSpeckleTopLevelConverter
{
Base Convert(object target);
BaseResult Convert(object target);
}
11 changes: 6 additions & 5 deletions Sdk/Speckle.Converters.Common/Registration/ConverterManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Speckle.Sdk.Common.Exceptions;

namespace Speckle.Converters.Common.Registration;


public class ConverterManager<T>(ConcurrentDictionary<string, Type> converterTypes, IServiceProvider serviceProvider)
: IConverterManager<T>
{
public string Name => typeof(T).Name;

public T ResolveConverter(Type type, bool recursive = true)
public ConverterResult<T> ResolveConverter(Type type, bool recursive = true)
{
var currentType = type;
while (true)
Expand All @@ -23,16 +23,17 @@ public T ResolveConverter(Type type, bool recursive = true)

if (currentType == null)
{
throw new ConversionNotSupportedException($"No conversion found for {type.Name} or any of its base types");
return new ConverterResult<T>(ConversionStatus.NoConverter, Message: $"No conversion found for {type.Name} or any of its base types");
}
}
else if (converter is null)
{
throw new ConversionNotSupportedException($"No conversion found for {type.Name}");
return new ConverterResult<T>(ConversionStatus.NoConverter, Message: $"No conversion found for {type.Name}");
}
else
{
return converter;

return new ConverterResult<T>(ConversionStatus.Success, converter);
}
}
}
Expand Down
Loading

0 comments on commit 01c5f8e

Please sign in to comment.