Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions test/tontester/src/tl/generator/generators/tlobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _write_to_dict(tlobject: ParsedTLObject, builder: SourceBuilder, ctx: Import
for arg in tlobject.args:
builder.writeln(",")
if arg.type in BASE_TYPES:
builder.write(f"'{arg.name}': ")
builder.write(f"'{arg.unmangled_name}': ")
if arg.type in BASE64_ENCODED_TYPES:
ctx.base64_needed = True
if arg.is_vector:
Expand All @@ -164,11 +164,11 @@ def _write_to_dict(tlobject: ParsedTLObject, builder: SourceBuilder, ctx: Import
builder.write("self.{}", arg.name)
else:
if arg.is_vector:
builder.write(f"'{arg.name}': ")
builder.write(f"'{arg.unmangled_name}': ")
builder.write(f"[x.to_dict() for x in self.{arg.name}]")
else:
builder.write(
f"**({{'{arg.name}': self.{arg.name}.to_dict()}} "
f"**({{'{arg.unmangled_name}': self.{arg.name}.to_dict()}} "
+ f"if self.{arg.name} is not None else {{}})"
) # do not write object in json if it's None

Expand Down Expand Up @@ -231,11 +231,11 @@ def _write_from_dict(
if not arg.is_vector:
builder.writeln(
f"_{arg.name} = "
+ f"{deserializer}(d.get('{arg.name}', {arg.default_value_for_from_dict()}))"
+ f"{deserializer}(d.get('{arg.unmangled_name}', {arg.default_value_for_from_dict()}))"
)
else:
builder.writeln(
f"_{arg.name} = tl.deserialize_list(d.get('{arg.name}', []), {deserializer})"
f"_{arg.name} = tl.deserialize_list(d.get('{arg.unmangled_name}', []), {deserializer})"
)

builder.writeln(
Expand Down
1 change: 1 addition & 0 deletions test/tontester/src/tl/generator/parsers/tlobject/tlarg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, name: str, arg_type: str, generic_definition: bool):
"bytes": "bytes_",
}

self.unmangled_name: str = name
self.name: str = MANGLING.get(name, name)

# Default values
Expand Down
91 changes: 91 additions & 0 deletions test/tontester/tests/tl/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from generated.test_schema import (
AllPrimitives,
MangledFieldsBaseTypes,
MangledFieldsObjects,
MangledFieldsVectorBaseTypes,
MangledFieldsVectorObjects,
NestedObject,
ObjectQueryRequest,
OptionalFields,
Expand Down Expand Up @@ -504,3 +508,90 @@ def test_object_request_defaults():
req = ObjectQueryRequest.from_dict(d)

assert req.obj is None


def test_mangled_fields_all_cases():
test_cases = [
(
MangledFieldsBaseTypes(
from_=42,
list_="test string",
self_=b"test bytes",
bytes_=b"\x01" * 16,
),
{
"@type": "mangledFieldsBaseTypes",
"from": 42,
"list": "test string",
"self": base64.b64encode(b"test bytes").decode(),
"bytes": base64.b64encode(b"\x01" * 16).decode(),
},
),
(
MangledFieldsVectorBaseTypes(
from_=[1, 2, 3],
list_=["a", "b", "c"],
self_=[b"x", b"y"],
bytes_=[b"\x01" * 32, b"\x02" * 32],
),
{
"@type": "mangledFieldsVectorBaseTypes",
"from": [1, 2, 3],
"list": ["a", "b", "c"],
"self": [
base64.b64encode(b"x").decode(),
base64.b64encode(b"y").decode(),
],
"bytes": [
base64.b64encode(b"\x01" * 32).decode(),
base64.b64encode(b"\x02" * 32).decode(),
],
},
),
(
MangledFieldsObjects(
from_=SimpleObject(id=1, name="obj1"),
list_=VariantA(value=100),
self_=SimpleObject(id=2, name="obj2"),
bytes_=VariantB(text="test"),
),
{
"@type": "mangledFieldsObjects",
"from": {"@type": "simpleObject", "id": 1, "name": "obj1"},
"list": {"@type": "variantA", "value": 100},
"self": {"@type": "simpleObject", "id": 2, "name": "obj2"},
"bytes": {"@type": "variantB", "text": "test"},
},
),
(
MangledFieldsVectorObjects(
from_=[SimpleObject(id=1, name="a"), SimpleObject(id=2, name="b")],
list_=[VariantA(value=10), VariantB(text="x")],
self_=[SimpleObject(id=3, name="c")],
bytes_=[VariantC(flag=True), VariantA(value=20)],
),
{
"@type": "mangledFieldsVectorObjects",
"from": [
{"@type": "simpleObject", "id": 1, "name": "a"},
{"@type": "simpleObject", "id": 2, "name": "b"},
],
"list": [
{"@type": "variantA", "value": 10},
{"@type": "variantB", "text": "x"},
],
"self": [
{"@type": "simpleObject", "id": 3, "name": "c"},
],
"bytes": [
{"@type": "variantC", "flag": True},
{"@type": "variantA", "value": 20},
],
},
),
]

for obj, expected_dict in test_cases:
assert obj.to_dict() == expected_dict
roundtrip_dict = type(obj).from_dict(expected_dict).to_dict()
assert roundtrip_dict == expected_dict
28 changes: 28 additions & 0 deletions test/tontester/tests/tl/test_schema.tl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ optionalFields

trueField has_feature:true other:int = TrueField;

mangledFieldsBaseTypes
from:int
list:string
self:bytes
bytes:int128
= MangledFieldsBaseTypes;

mangledFieldsVectorBaseTypes
from:vector<int>
list:vector<string>
self:vector<bytes>
bytes:vector<int256>
= MangledFieldsVectorBaseTypes;

mangledFieldsObjects
from:SimpleObject
list:TestVariant
self:SimpleObject
bytes:TestVariant
= MangledFieldsObjects;

mangledFieldsVectorObjects
from:vector<SimpleObject>
list:vector<TestVariant>
self:vector<SimpleObject>
bytes:vector<TestVariant>
= MangledFieldsVectorObjects;

---functions---

query id:int data:string = AllPrimitives;
Expand Down
4 changes: 2 additions & 2 deletions test/tontester/tests/tontester/test_tl.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_to_dict(pubk: Pub_unenc, block: TonNode_blockIdExt):
"@type": "adnl.packetContents",
"rand1": "AQE=",
"flags": 1,
"from_": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
"from": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
"messages": [],
"seqno": 0,
"confirm_seqno": 0,
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_from_dict(pubk: Pub_unenc):
"@type": "adnl.packetContents",
"rand1": "AQE=",
"flags": 1,
"from_": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
"from": {"@type": "pub.unenc", "data": "EhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhI="},
"rand2": "AQE=",
}
)
Expand Down
Loading