diff --git a/src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs b/src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs index 73b123b..0501305 100644 --- a/src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs +++ b/src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs @@ -1,12 +1,8 @@ -#nullable enable - -#pragma warning disable 612,618 - using System.Collections.Generic; using System.IO; -using System.Linq; using Newtonsoft.Json.Linq; using System; +using System.Diagnostics; namespace Baboon.Runtime.Shared { public interface IBaboonGenerated { @@ -41,7 +37,29 @@ public interface IBaboonGeneratedConversion : IConversion public abstract class AbstractConversion : IBaboonGeneratedConversion { - public abstract TTo Convert(TCtx? context, AbstractBaboonConversions conversions, TFrom from); + public TTo Convert(TCtx? context, AbstractBaboonConversions conversions, TFrom from) { + if (from is IBaboonGenerated bgf) + { + if (TypeId() != bgf.BaboonTypeIdentifier()) + { + throw new ArgumentException($"Provided instance is {bgf.BaboonTypeIdentifier()} but must be {TypeId()}"); + } + } + + var result = DoConvert(context, conversions, from); + + if (result is IBaboonGenerated bgr) + { + if (TypeId() != bgr.BaboonTypeIdentifier()) + { + throw new ArgumentException($"Provided instance is {bgr.BaboonTypeIdentifier()} but must be {TypeId()}"); + } + } + + return result; + } + + protected abstract TTo DoConvert(TCtx? context, AbstractBaboonConversions conversions, TFrom from); IBaboonGenerated IBaboonGeneratedConversion.Convert(TCtx? context, AbstractBaboonConversions conversions, IBaboonGenerated from) where TCtx : default { diff --git a/src/main/resources/baboon-runtime/cs/BaboonTestRuntimeShared.cs b/src/main/resources/baboon-runtime/cs/BaboonTestRuntimeShared.cs index d0bf295..8edf156 100644 --- a/src/main/resources/baboon-runtime/cs/BaboonTestRuntimeShared.cs +++ b/src/main/resources/baboon-runtime/cs/BaboonTestRuntimeShared.cs @@ -1,11 +1,6 @@ -#nullable enable - -#pragma warning disable 612,618 - using System.Text; using Baboon.Time; using System.Collections.Generic; -using System.Linq; using System.Collections.Immutable; using System; diff --git a/src/main/resources/baboon-runtime/cs/BaboonTime.cs b/src/main/resources/baboon-runtime/cs/BaboonTime.cs index aafe634..065545b 100644 --- a/src/main/resources/baboon-runtime/cs/BaboonTime.cs +++ b/src/main/resources/baboon-runtime/cs/BaboonTime.cs @@ -1,10 +1,5 @@ -#nullable enable - -#pragma warning disable 612,618 - using System.Globalization; using Newtonsoft.Json; -using System.Linq; using System; namespace Baboon.Time { diff --git a/src/main/scala/io/septimalmind/baboon/translator/csharp/IndividualConversionHandler.scala b/src/main/scala/io/septimalmind/baboon/translator/csharp/IndividualConversionHandler.scala index ef5a95a..3f520a3 100644 --- a/src/main/scala/io/septimalmind/baboon/translator/csharp/IndividualConversionHandler.scala +++ b/src/main/scala/io/septimalmind/baboon/translator/csharp/IndividualConversionHandler.scala @@ -93,7 +93,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator, val cdefn = q"""public abstract class ${convname} : $abstractConversion<${tin}, ${tout}> |{ - | public abstract override ${tout} Convert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default; + | protected abstract override ${tout} DoConvert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default; | | $fullMeta |}""".stripMargin @@ -121,7 +121,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator, val cdefn = q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}> |{ - | public override ${tout} Convert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default { + | protected override ${tout} DoConvert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default { | if ($csEnum.TryParse(from.ToString(), out ${tout} parsed)) | { | return parsed; @@ -157,7 +157,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator, val cdefn = q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}> |{ - | public override ${tout} Convert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default { + | protected override ${tout} DoConvert(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default { | ${branches.join("\nelse\n").shift(8).trim} | } | @@ -329,7 +329,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator, val cdefn = q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}> |{ - | public override ${tout} Convert(C? context, $abstractBaboonConversions conversions, ${tin} _from) where C: default { + | protected override ${tout} DoConvert(C? context, $abstractBaboonConversions conversions, ${tin} _from) where C: default { | ${initExprs.join(";\n").shift(8).trim} | return new ${tout}( | ${consExprs.join(",\n").shift(12).trim}