Skip to content

Commit

Permalink
conversion safety
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Dec 17, 2024
1 parent df08f04 commit 320f131
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
30 changes: 24 additions & 6 deletions src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -41,7 +37,29 @@ public interface IBaboonGeneratedConversion : IConversion

public abstract class AbstractConversion<TFrom, TTo> : IBaboonGeneratedConversion
{
public abstract TTo Convert<TCtx>(TCtx? context, AbstractBaboonConversions conversions, TFrom from);
public TTo Convert<TCtx>(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>(TCtx? context, AbstractBaboonConversions conversions, TFrom from);

IBaboonGenerated IBaboonGeneratedConversion.Convert<TCtx>(TCtx? context, AbstractBaboonConversions conversions, IBaboonGenerated from) where TCtx : default
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
5 changes: 0 additions & 5 deletions src/main/resources/baboon-runtime/cs/BaboonTime.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
val cdefn =
q"""public abstract class ${convname} : $abstractConversion<${tin}, ${tout}>
|{
| public abstract override ${tout} Convert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default;
| protected abstract override ${tout} DoConvert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default;
|
| $fullMeta
|}""".stripMargin
Expand Down Expand Up @@ -121,7 +121,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
val cdefn =
q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}>
|{
| public override ${tout} Convert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default {
| protected override ${tout} DoConvert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default {
| if ($csEnum.TryParse(from.ToString(), out ${tout} parsed))
| {
| return parsed;
Expand Down Expand Up @@ -157,7 +157,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
val cdefn =
q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}>
|{
| public override ${tout} Convert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default {
| protected override ${tout} DoConvert<C>(C? context, $abstractBaboonConversions conversions, ${tin} from) where C: default {
| ${branches.join("\nelse\n").shift(8).trim}
| }
|
Expand Down Expand Up @@ -329,7 +329,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
val cdefn =
q"""public sealed class ${convname} : $abstractConversion<${tin}, ${tout}>
|{
| public override ${tout} Convert<C>(C? context, $abstractBaboonConversions conversions, ${tin} _from) where C: default {
| protected override ${tout} DoConvert<C>(C? context, $abstractBaboonConversions conversions, ${tin} _from) where C: default {
| ${initExprs.join(";\n").shift(8).trim}
| return new ${tout}(
| ${consExprs.join(",\n").shift(12).trim}
Expand Down

0 comments on commit 320f131

Please sign in to comment.