Skip to content

Commit

Permalink
Use type KeyValuePair ctor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caparow committed Jul 24, 2024
1 parent db366df commit 210b548
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,11 @@ class CSNSJsonCodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
q"""$BaboonTools.ReadValue($ref, t => ${mkDecoder(args.head, domain, q"t")})"""

case TypeId.Builtins.map =>
val keyRef = args.head
val valueRef = args.last
val keyDec = decodeKey(args.head, domain, q"kv.Name")
val valueDec = mkDecoder(args.last, domain, q"kv.Value")
q"""$ref!.Value<$nsJObject>()!.Properties().Select(kv => $csKeyValuePair.Create($keyDec, $valueDec)).ToImmutableDictionary()"""
q"""$ref!.Value<$nsJObject>()!.Properties().Select(kv => new $csKeyValuePair<${trans.asCsRef(keyRef, domain)}, ${trans.asCsRef(valueRef, domain)}>($keyDec, $valueDec)).ToImmutableDictionary()"""

case TypeId.Builtins.lst =>
q"""$ref!.Value<$nsJArray>()!.Select(e => ${mkDecoder(args.head, domain, q"e")}).ToImmutableList()"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import izumi.fundamentals.platform.strings.TextTree
import izumi.fundamentals.platform.strings.TextTree.*

class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
extends CSCodecTranslator {
extends CSCodecTranslator {
override def translate(defn: DomainMember.User,
csRef: CSValue.CSType,
srcRef: CSValue.CSType,
Expand All @@ -25,22 +25,24 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
}

// plumbing reference leaks
val insulatedEnc = q"""
|#pragma warning disable CS0162
|if (this == instance.Value)
|{
| ${enc.shift(4).trim}
| return;
|}
|#pragma warning disable CS0162
|
|instance.Value.Encode(writer, value);""".stripMargin
val insulatedDec = q"""if (this == instance.Value)
|{
| ${dec.shift(4).trim}
|}
|
|return instance.Value.Decode(wire);""".stripMargin
val insulatedEnc =
q"""
|#pragma warning disable CS0162
|if (this == instance.Value)
|{
| ${enc.shift(4).trim}
| return;
|}
|#pragma warning disable CS0162
|
|instance.Value.Encode(writer, value);""".stripMargin
val insulatedDec =
q"""if (this == instance.Value)
|{
| ${dec.shift(4).trim}
|}
|
|return instance.Value.Decode(wire);""".stripMargin

genCodec(
defn,
Expand All @@ -60,7 +62,7 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
enc: TextTree[CSValue],
dec: TextTree[CSValue],
addExtensions: Boolean,
): TextTree[CSValue] = {
): TextTree[CSValue] = {
val iName = q"$iBaboonBinCodec<$name>"

val baseMethods = List(
Expand Down Expand Up @@ -133,7 +135,7 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
private def genAdtBodies(name: CSValue.CSType,
domain: Domain,
a: Typedef.Adt,
) = {
) = {
val branches = a.members.zipWithIndex.toList.map {
case (m, idx) =>
val branchNs = q"${trans.adtNsName(a.id)}"
Expand All @@ -142,12 +144,14 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
val cName = codecName(trans.toCsTypeRefNoDeref(m, domain))
val castedName = branchName.toLowerCase

(q"""if (value is $fqBranch $castedName)
(
q"""if (value is $fqBranch $castedName)
|{
| writer.Write((byte)${idx.toString});
| $cName.Instance.Encode(writer, $castedName);
| return;
|}""".stripMargin, q"""if (asByte == ${idx.toString})
|}""".stripMargin,
q"""if (asByte == ${idx.toString})
|{
| return $cName.Instance.Decode(wire);
|}""".stripMargin)
Expand All @@ -168,11 +172,13 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
private def genEnumBodies(name: CSValue.CSType, e: Typedef.Enum) = {
val branches = e.members.zipWithIndex.toList.map {
case (m, idx) =>
(q"""if (value == $name.${m.name})
(
q"""if (value == $name.${m.name})
|{
| writer.Write((byte)${idx.toString});
| return;
|}""".stripMargin, q"""if (asByte == ${idx.toString})
|}""".stripMargin,
q"""if (asByte == ${idx.toString})
|{
| return $name.${m.name};
|}""".stripMargin)
Expand Down Expand Up @@ -201,9 +207,11 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
}

val fenc =
q"""${branches
.map(_._1)
.join(";\n")};""".stripMargin
q"""${
branches
.map(_._1)
.join(";\n")
};""".stripMargin

val fdec =
q"""return new $name(
Expand Down Expand Up @@ -248,7 +256,7 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
q"wire.ReadString()"
case TypeId.Builtins.uid =>
q"$csGuid.Parse(wire.ReadString())"
case TypeId.Builtins.tsu | TypeId.Builtins.tso=>
case TypeId.Builtins.tsu | TypeId.Builtins.tso =>
q"$csDateTimeFormats.FromString(wire.ReadString())"
case o =>
throw new RuntimeException(s"BUG: Unexpected type: $o")
Expand All @@ -266,20 +274,18 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
q"""(wire.ReadByte() == 0 ? null : ${mkDecoder(c.args.head, domain)})""".stripMargin

case TypeId.Builtins.map =>
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => $csKeyValuePair.Create(${mkDecoder(
c.args.head,
domain
)}, ${mkDecoder(c.args.last, domain)})).ToImmutableDictionary()"""
val keyRef = c.args.head
val valueRef = c.args.last
val keyDecoder = mkDecoder(keyRef, domain)
val valueDecoder = mkDecoder(valueRef, domain)
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => new $csKeyValuePair<${trans.asCsRef(keyRef, domain)}, ${trans.asCsRef(valueRef, domain)}>($keyDecoder, $valueDecoder)).ToImmutableDictionary()"""

case TypeId.Builtins.lst =>
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => ${mkDecoder(
c.args.head,
domain
)}).ToImmutableList()"""
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => ${mkDecoder(c.args.head, domain)}).ToImmutableList()"""

case TypeId.Builtins.set =>
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => ${mkDecoder(
c.args.head,
domain
)}).ToImmutableHashSet()"""
q"""$csEnumerable.Range(0, wire.ReadInt32()).Select(idx => ${mkDecoder(c.args.head, domain)}).ToImmutableHashSet()"""

case o =>
throw new RuntimeException(s"BUG: Unexpected type: $o")
}
Expand Down Expand Up @@ -341,18 +347,22 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)
|} else
|{
| writer.Write((byte)1);
| ${mkEncoder(c.args.head, domain, trans.deNull(c.args.head, ref))
.shift(4)
.trim};
| ${
mkEncoder(c.args.head, domain, trans.deNull(c.args.head, ref))
.shift(4)
.trim
};
|}""".stripMargin
case TypeId.Builtins.map =>
q"""writer.Write($ref.Count());
|foreach (var kv in $ref)
|{
| ${mkEncoder(c.args.head, domain, q"kv.Key").shift(4).trim};
| ${mkEncoder(c.args.last, domain, q"kv.Value")
.shift(4)
.trim};
| ${
mkEncoder(c.args.last, domain, q"kv.Value")
.shift(4)
.trim
};
|}""".stripMargin
case TypeId.Builtins.lst =>
q"""writer.Write($ref.Count());
Expand All @@ -378,10 +388,11 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator, tools: CSDefnTools)

override def codecMeta(defn: DomainMember.User,
name: CSValue.CSType): CSCodecTranslator.CodecMeta = {
val member = q"""public IBaboonBinCodec<$name> Codec_UEBA()
|{
| return ${codecName(name)}.Instance;
|}""".stripMargin
val member =
q"""public IBaboonBinCodec<$name> Codec_UEBA()
|{
| return ${codecName(name)}.Instance;
|}""".stripMargin
CodecMeta(member)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
)
case c: TypeRef.Constructor
if c.id == TypeId.Builtins.map =>
val keyRef = c.args.head
val valueRef = c.args.last
Right(
Seq(
q"(from e in $fieldRef select ${csKeyValuePair}.Create(${transfer(
q"(from e in $fieldRef select new $csKeyValuePair<${trans.asCsRef(keyRef, domain)}, ${trans.asCsRef(valueRef, domain)}>(${transfer(
c.args.head,
q"e.Key"
)}, ${transfer(c.args.last, q"e.Value")})).ToImmutableDictionary(v => v.Key, v => v.Value)"
Expand Down Expand Up @@ -379,7 +381,7 @@ class IndividualConversionHandler(trans: CSTypeTranslator,
val vt = trans.asCsRef(newCollArgs.last, domain)
Right(
Seq(
q"(from e in $fieldRef select $csKeyValuePair.Create(($kt)e.Key, ($vt)e.Value)).ToImmutableDictionary(v => v.Key, v => v.Value)"
q"(from e in $fieldRef select new $csKeyValuePair<$kt, $vt>(($kt)e.Key, ($vt)e.Value)).ToImmutableDictionary(v => v.Key, v => v.Value)"
)
)
case _ =>
Expand Down

0 comments on commit 210b548

Please sign in to comment.