Skip to content

Commit

Permalink
codec parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Dec 18, 2024
1 parent cacb4e7 commit be59e08
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 150 deletions.
53 changes: 46 additions & 7 deletions src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#nullable enable

#pragma warning disable 612,618

using System.Linq;

using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -52,7 +58,7 @@ public TTo Convert<TCtx>(TCtx? context, AbstractBaboonConversions conversions, T
{
if (TypeId() != bgr.BaboonTypeIdentifier())
{
throw new ArgumentException($"Provided instance is {bgr.BaboonTypeIdentifier()} but must be {TypeId()}");
throw new ArgumentException($"Produced instance is {bgr.BaboonTypeIdentifier()} but must be {TypeId()}");
}
}

Expand All @@ -65,7 +71,7 @@ public TTo Convert<TCtx>(TCtx? context, AbstractBaboonConversions conversions, T
{
if (from is not TFrom fr)
{
throw new Exception($"Can't use IBaboonGeneratedConversion interface when from is not of type {typeof(TFrom).FullName}");
throw new Exception($"Can't use IBaboonGeneratedConversion interface when 'from' is not of type {typeof(TFrom).FullName}");
}
var res = Convert(context, conversions, fr);

Expand Down Expand Up @@ -97,23 +103,56 @@ public interface IBaboonCodecData {
public String BaboonTypeIdentifier();
}

public class BaboonCodecContext {
public BaboonCodecContext(bool useIndexes)
{
UseIndexes = useIndexes;
}

public Boolean UseIndexes { get; }

public static BaboonCodecContext Indexed { get; } = new BaboonCodecContext(true);
public static BaboonCodecContext Compact { get; } = new BaboonCodecContext(false);
public static BaboonCodecContext Default { get; } = Compact;

}

public interface IBaboonCodec<T> : IBaboonCodecData {}

public interface IBaboonValueCodec<T, TWire> : IBaboonCodec<T>
{
TWire Encode(T instance);
T Decode(TWire wire);
// json codec should always ignore context
TWire Encode(BaboonCodecContext ctx, T instance);
T Decode(BaboonCodecContext ctx, TWire wire);
}

public interface IBaboonJsonCodec<T> : IBaboonValueCodec<T, JToken> {}

public interface IBaboonStreamCodec<T, in TOut, in TIn> : IBaboonCodec<T>
{
void Encode(TOut writer, T instance);
T Decode(TIn wire);
void Encode(BaboonCodecContext ctx, TOut writer, T instance);
T Decode(BaboonCodecContext ctx, TIn wire);
}

public interface IBaboonBinCodec<T> : IBaboonStreamCodec<T, BinaryWriter, BinaryReader> {}
public interface IBaboonBinCodec<T> : IBaboonStreamCodec<T, BinaryWriter, BinaryReader>
{
void EncodeMessage(BaboonCodecContext ctx, BinaryWriter writer, T instance)
{
var header = 0b0000000;
if (ctx.UseIndexes)
{
header |= 0b0000001;
}
Encode(ctx, writer, instance);
}

T DecodeMessage(BinaryReader wire)
{
var header = wire.ReadByte();
var ctx = new BaboonCodecContext((header & 0b0000001) != 0);
return Decode(ctx, wire);
}
}

public interface IBaboonTypeCodecs {
public String Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ object CSBaboonTranslator {
// Baboon codec types
val iBaboonCodecData: CSType =
CSType(baboonRtPkg, "IBaboonCodecData", fq = false)
val baboonCodecContext: CSType =
CSType(baboonRtPkg, "BaboonCodecContext", fq = false)
val iBaboonCodec: CSType =
CSType(baboonRtPkg, "IBaboonCodec", fq = false)
val iBaboonValueCodec: CSType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ object CSCodecTestsTranslator {
typeTranslator: CSTypeTranslator,
logger: BLogger,
enquiries: BaboonEnquiries,
compilerOptions: CompilerOptions
) extends CSCodecTestsTranslator {
compilerOptions: CompilerOptions)
extends CSCodecTestsTranslator {
override def translate(definition: DomainMember.User,
csRef: CSValue.CSType,
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): Option[TextTree[CSValue]] = {
): Option[TextTree[CSValue]] = {

val codecTestName = definition.id.owner match {
case Owner.Toplevel => srcRef.name
Expand All @@ -47,17 +47,19 @@ object CSCodecTestsTranslator {
case d if enquiries.isRecursiveTypedef(d, domain) => None
case d if d.defn.isInstanceOf[Typedef.NonDataTypedef] => None
case _ =>
val init = fieldsInitialization(definition, srcRef, domain, evo)
val testClass =
q"""[$nunitTestFixture]
|public class $testClassName
|{
| #nullable disable
| ${testFields(definition, srcRef, domain, evo)}
| ${testFields(definition, srcRef, domain, evo).shift(2).trim}
| private $baboonCodecContext context = $baboonCodecContext.Default;
|
| [$nunitOneTimeSetUp]
| public void Setup()
| {
| ${fieldsInitialization(definition, srcRef, domain, evo)}
| ${init.shift(4).trim}
| }
|
| ${tests(definition, srcRef, domain, evo)}
Expand Down Expand Up @@ -89,11 +91,11 @@ object CSCodecTestsTranslator {
}

private def fieldsInitialization(
definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain,
evolution: BaboonEvolution
): TextTree[CSValue] = {
definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain,
evolution: BaboonEvolution
): TextTree[CSValue] = {
definition.defn match {
case adt: Typedef.Adt =>
adt
Expand All @@ -120,33 +122,33 @@ object CSCodecTestsTranslator {
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
): TextTree[CSValue] = {
codecs
.map {
case jsonCodec: CSNSJsonCodecGenerator =>
q"""[Test]
|public void jsonCodecTest()
|{
| ${jsonCodecAssertions(
jsonCodec,
definition,
srcRef,
domain,
evo
).shift(4).trim}
jsonCodec,
definition,
srcRef,
domain,
evo
).shift(4).trim}
|}
|""".stripMargin
case uebaCodec: CSUEBACodecGenerator =>
q"""[Test]
|public void uebaCodecTest()
|{
| ${uebaCodecAssertions(
uebaCodec,
definition,
srcRef,
domain,
evo
).shift(4).trim}
uebaCodec,
definition,
srcRef,
domain,
evo
).shift(4).trim}
|}
|""".stripMargin
case unknown =>
Expand All @@ -166,7 +168,7 @@ object CSCodecTestsTranslator {
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
): TextTree[CSValue] = {
definition.defn match {
case adt: Typedef.Adt =>
adt
Expand All @@ -178,22 +180,20 @@ object CSCodecTestsTranslator {
val fieldName = member.name.name.toLowerCase
val serialized = s"${fieldName}_json"
val decoded = s"${fieldName}_decoded"
q"""var $serialized = $codecName.Instance.Encode($fieldName);
|var $decoded = $codecName.Instance.Decode($serialized);
q"""var $serialized = $codecName.Instance.Encode(this.context, $fieldName);
|var $decoded = $codecName.Instance.Decode(this.context, $serialized);
|Assert.AreEqual($fieldName, $decoded);
|""".stripMargin
}
.toList
.join("\n")
.shift(6)
.trim
case _ =>
val codecName = codec.codecName(srcRef)
val fieldName = srcRef.name.toLowerCase
val serialized = s"${fieldName}_json"
val decoded = s"${fieldName}_decoded"
q"""var $serialized = $codecName.Instance.Encode($fieldName);
|var $decoded = $codecName.Instance.Decode($serialized);
q"""var $serialized = $codecName.Instance.Encode(this.context, $fieldName);
|var $decoded = $codecName.Instance.Decode(this.context, $serialized);
|Assert.AreEqual($fieldName, $decoded);
|""".stripMargin
}
Expand All @@ -204,7 +204,7 @@ object CSCodecTestsTranslator {
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
): TextTree[CSValue] = {
definition.defn match {
case adt: Typedef.Adt =>
adt
Expand All @@ -222,21 +222,19 @@ object CSCodecTestsTranslator {
|{
| using ($binaryWriter binaryWriter = new $binaryWriter(writeMemoryStream))
| {
| $codecName.Instance.Encode(binaryWriter, $fieldName);
| $codecName.Instance.Encode(this.context, binaryWriter, $fieldName);
| }
| writeMemoryStream.Flush();
| var $serialized = writeMemoryStream.GetBuffer();
| var $readStream = new MemoryStream($serialized);
| var $binaryReader = new BinaryReader($readStream);
| var $decoded = $codecName.Instance.Decode($binaryReader);
| var $decoded = $codecName.Instance.Decode(this.context, $binaryReader);
| Assert.AreEqual($fieldName, $decoded);
|}
|""".stripMargin
}
.toList
.join("\n")
.shift(4)
.trim
case _ =>
val codecName = codec.codecName(srcRef)
val fieldName = srcRef.name.toLowerCase
Expand All @@ -246,17 +244,17 @@ object CSCodecTestsTranslator {
|{
| using ($binaryWriter binaryWriter = new $binaryWriter(writeMemoryStream))
| {
| $codecName.Instance.Encode(binaryWriter, $fieldName);
| $codecName.Instance.Encode(this.context, binaryWriter, $fieldName);
| }
| writeMemoryStream.Flush();
|
| var $serialized = writeMemoryStream.GetBuffer();
| var readMemoryStream = new MemoryStream($serialized);
| var binaryReader = new BinaryReader(readMemoryStream);
| var $decoded = $codecName.Instance.Decode(binaryReader);
| var $decoded = $codecName.Instance.Decode(this.context, binaryReader);
| Assert.AreEqual($fieldName, $decoded);
|}
|""".stripMargin.shift(2).trim
|""".stripMargin
}
}
}
Expand Down
Loading

0 comments on commit be59e08

Please sign in to comment.