Skip to content

Commit

Permalink
Merge pull request #188 from admire93/enum-mro
Browse files Browse the repository at this point in the history
Rename mro not to overlap with Python keyword
  • Loading branch information
dahlia authored Oct 26, 2017
2 parents b6cbe68 + f459705 commit 8ac04b0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ compileTypeDeclaration _ d@TypeDeclaration { typename = typename'
"\n"
[ T.concat [ compileDocsComment " " m
, "\n "
, toAttributeName' memberName
, toEnumMemberName memberName
, " = '"
, I.toSnakeCaseText bn
, "'"
Expand All @@ -700,6 +700,16 @@ $memberNames
){ ret className }:
return cls(value.replace('-', '_')) # FIXME: validate input
|]
where
memberKeywords :: [T.Text]
memberKeywords = ["mro"]
toEnumMemberName :: Name -> T.Text
toEnumMemberName name' = if attributeName `elem` memberKeywords
then attributeName `T.snoc` '_'
else attributeName
where
attributeName :: T.Text
attributeName = toAttributeName' name'
compileTypeDeclaration src d@TypeDeclaration { typename = typename'
, type' = RecordType fields
} = do
Expand Down
3 changes: 3 additions & 0 deletions test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ enum eva-char = soryu-asuka-langley
| katsuragi-misato
| nagisa-kaworu
;
enum duplicate-keyword = mro
| no-mro
;

record point1 (
# Record docs.
Expand Down
16 changes: 14 additions & 2 deletions test/python/primitive_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from nirum.service import Service
from six import PY3

from fixture.foo import (CultureAgnosticName, Dog, EastAsianName,
EvaChar, FloatUnbox, Gender, ImportedTypeUnbox, Irum,
from fixture.foo import (CultureAgnosticName, Dog, DuplicateKeyword,
EastAsianName, EvaChar,
FloatUnbox, Gender, ImportedTypeUnbox, Irum,
Line, MixedName, NullService,
Point1, Point2, Point3d, Pop, PingService, Product,
Rnb, Run, Stop, Way, WesternName)
Expand Down Expand Up @@ -265,3 +266,14 @@ def test_service():
PingService().ping(nonce=u'nonce')
with raises(TypeError):
PingService().ping(wrongkwd=u'a')


def test_enum_duplicate_member_name():
assert hasattr(DuplicateKeyword, 'mro_')
assert hasattr(DuplicateKeyword, 'no_mro')
assert (DuplicateKeyword.__nirum_deserialize__(u'mro') ==
DuplicateKeyword.mro_)
assert (DuplicateKeyword.__nirum_deserialize__(u'no-mro') ==
DuplicateKeyword.no_mro)
assert DuplicateKeyword.mro_.__nirum_serialize__() == 'mro'
assert DuplicateKeyword.no_mro.__nirum_serialize__() == 'no_mro'

0 comments on commit 8ac04b0

Please sign in to comment.