Skip to content

Commit 3cd8d36

Browse files
committed
Fix temporary variables to be properly mangled
1 parent 308f8b4 commit 3cd8d36

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

src/Nirum/Targets/Python.hs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ renameMP Python { renames = table } = renameModulePath table
258258
thd3 :: (a, b, c) -> c
259259
thd3 (_, _, v) = v
260260

261+
mangleVar :: Code -> Int -> Code
262+
mangleVar expr randomNum = T.concat
263+
[ "__nirum_"
264+
, (`T.map` expr) $ \ c -> if ('A' <= c && c <= 'Z' ||
265+
'a' <= c && c <= 'z' || c == '_')
266+
then c else '_'
267+
, "__"
268+
, T.pack $ show randomNum
269+
, "__"
270+
]
271+
261272
-- | The set of Python reserved keywords.
262273
-- See also: https://docs.python.org/3/reference/lexical_analysis.html#keywords
263274
keywords :: S.Set T.Text
@@ -648,14 +659,23 @@ compileSerializer' mod' (OptionModifier typeExpr) pythonVar =
648659
compileSerializer' mod' (SetModifier typeExpr) pythonVar =
649660
compileSerializer' mod' (ListModifier typeExpr) pythonVar
650661
compileSerializer' mod' (ListModifier typeExpr) pythonVar =
651-
[qq|[($serializer) for __{pythonVar}__elem__ in ($pythonVar)]|]
662+
[qq|list(($serializer) for {mangleVar pythonVar 1} in ($pythonVar))|]
652663
where
653664
serializer :: Code
654-
serializer = compileSerializer' mod' typeExpr [qq|__{pythonVar}__elem__|]
665+
serializer = compileSerializer' mod' typeExpr $ mangleVar pythonVar 1
655666
compileSerializer' mod' (MapModifier kt vt) pythonVar =
656-
[qq|\{({compileSerializer' mod' kt $ T.concat ["__", pythonVar, "__k__"]}):
657-
({compileSerializer' mod' vt $ T.concat ["__", pythonVar, "__v__"]})
658-
for __{pythonVar}__k__, __{pythonVar}__v__ in ($pythonVar).items()\}|]
667+
[qq|list(
668+
\{
669+
'key': ({compileSerializer' mod' kt kVar}),
670+
'value': ({compileSerializer' mod' vt vVar}),
671+
\}
672+
for $kVar, $vVar in ($pythonVar).items()
673+
)|]
674+
where
675+
kVar :: Code
676+
kVar = mangleVar pythonVar 1
677+
vVar :: Code
678+
vVar = mangleVar pythonVar 2
659679
compileSerializer' mod' (TypeIdentifier typeId) pythonVar =
660680
case lookupType typeId mod' of
661681
Missing -> "None" -- must never happen

test/nirum_fixture/fixture/foo.nrm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,7 @@ service sample-service (
129129
sample-method (animal a, product b/bb, gender c, way d/dd,
130130
uuid e, binary f/ff, bigint g, text h/hh),
131131
);
132+
133+
record record-with-map (
134+
{text: text} text-to-text,
135+
);

test/python/primitive_test.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
from nirum.service import Service
66
from six import PY3
77

8-
from fixture.foo import (CultureAgnosticName, Dog,
8+
from fixture.foo import (Album, CultureAgnosticName, Dog,
99
EastAsianName, EvaChar,
1010
FloatUnbox, Gender, ImportedTypeUnbox, Irum,
1111
Line, MixedName, Mro, Music, NoMro, NullService,
12+
Person, People,
1213
Point1, Point2, Point3d, Pop, PingService, Product,
13-
RecordWithOptionalRecordField,
14+
RecordWithMap, RecordWithOptionalRecordField,
1415
ReservedKeywordEnum, ReservedKeywordUnion,
15-
Rnb, RpcError, Run, Status, Stop, Way, WesternName)
16+
Rnb, RpcError, Run, Song, Status, Stop, Way,
17+
WesternName)
1618
from fixture.foo.bar import PathUnbox, IntUnbox, Point
1719
from fixture.qux import Path, Name
1820

@@ -351,3 +353,43 @@ def test_nirum_tag_classes():
351353
Status.Tag.run: Run,
352354
Status.Tag.stop: Stop,
353355
}
356+
357+
358+
def test_list_serializer():
359+
album = Album(name=u'Album title', tracks=[Song(name=u'Song title')])
360+
assert album.__nirum_serialize__() == {
361+
'_type': 'album',
362+
'name': u'Album title',
363+
'tracks': [
364+
{'_type': 'song', 'name': u'Song title'},
365+
],
366+
}
367+
368+
369+
def test_set_serializer():
370+
people = People(people={
371+
Person(first_name=Name(u'First'), last_name=Name(u'Last')),
372+
})
373+
assert people.__nirum_serialize__() == {
374+
'_type': 'people',
375+
'people': [
376+
{
377+
'_type': 'person',
378+
'first_name': u'First',
379+
'last_name': u'Last',
380+
},
381+
]
382+
}
383+
384+
385+
def test_map_serializer():
386+
record = RecordWithMap(text_to_text={u'key': u'value'})
387+
assert record.__nirum_serialize__() == {
388+
'_type': 'record_with_map',
389+
'text_to_text': [
390+
{
391+
'key': u'key',
392+
'value': u'value',
393+
},
394+
],
395+
}

0 commit comments

Comments
 (0)