Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __nirum_annotations__ on record type #320

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ To be released.

### Python target

- Python 3.7 support. [[#298], [#300] by Alan D.J. Synn]
- Python 3.7 support. [[#298], [#300] by Alan Synn]
- Add annotation metadata `__nirum_annotations__` on record type. [[#273] by Alan Synn]

[#273]: https://github.com/nirum-lang/nirum/issues/273
[#298]: https://github.com/nirum-lang/nirum/issues/298

### Et cetera
Expand Down
18 changes: 18 additions & 0 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,11 @@ class #{className}(enum.Enum):
|]
compileTypeDeclaration src d@TypeDeclaration { typename = Name tnFacial tnBehind
, type' = RecordType fields'
, typeAnnotations = AnnotationSet { AI.annotations = annoMap }
} = do
let className = toClassName tnFacial
insertStandardImport "typing"
insertThirdPartyImportsA [("nirum.datastructures", [("map_type", "Map")])]
abc <- collectionsAbc
pyVer <- getPythonVersion
fieldCodes <- toFieldCodes src
Expand All @@ -867,6 +869,22 @@ class #{className}(object):
%{ endforall }
)
__nirum_type__ = 'record'
__nirum_annotations__ = map_type({
%{ forall (annotationName, annotationArgument) <- M.toList annoMap }
'#{toAttributeName annotationName}': map_type({
%{ forall (annotationArgumentName, annotationArgumentValue) <- M.toList annotationArgument }
'#{toAttributeName annotationArgumentName}':
%{ case annotationArgumentValue}
%{ of AI.Text t }
'''#{t}'''
%{ of AI.Integer i }
#{i}
%{ endcase }
,
%{ endforall }
}),
%{ endforall }
})

%{ case pyVer }
%{ of Python2 }
Expand Down
12 changes: 11 additions & 1 deletion test/python/annotation_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fixture.datetime import DatetimeService
from fixture.foo import PingService, RpcError
from fixture.foo import PingService, RpcError, AnnotationTestRecord
from nirum.datastructures import Map


Expand All @@ -21,3 +21,13 @@ def test_annotation_int():
'num_constraints': Map({'max': 12, 'min': 1}),
})
assert DatetimeService.__nirum_method_annotations__['delta_month'] == exp


def test_record_annotation():
exp = Map({
'helloworld': Map({
'foo': 1,
'bar': 'foobar',
}),
})
assert AnnotationTestRecord.__nirum_annotations__ == exp