-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added newtype support for primitive type enum (#76)
* Added newtype support for primitive type enum * Make newgen generator automatic * Changed type of jdwp_type and changed default type to int * Wrote test to validate primitive type mapping * Added test for type alias definition * Factored out function defining type alias. * Used absolute name format for newtype * Changed testing framework to unittest * Added dependency for jdwp project * Added int and string type * Changed the expected start * Updated test and removed int and byte from primitive types. * Removed int and boolean type from new type generator * doc: add copyright
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
from projects.jdwp.defs.schema import PrimitiveType | ||
import typing | ||
|
||
|
||
def get_python_type(jdwp_type: PrimitiveType) -> str: | ||
"""Map JDWP type to Python type.""" | ||
mapping = { | ||
PrimitiveType.STRING: "str", | ||
PrimitiveType.BOOLEAN: "bool", | ||
} | ||
return mapping.get(jdwp_type, "int") | ||
|
||
|
||
def get_type_alias_definition(jdwp_type: PrimitiveType) -> str: | ||
"""Return the type alias definition for a given JDWP type.""" | ||
python_type = get_python_type(jdwp_type) | ||
new_type_name = f"{jdwp_type.name.capitalize()}Type" | ||
return f"{new_type_name} = typing.NewType('{new_type_name}', {python_type})" | ||
|
||
|
||
def generate_new_types(): | ||
for jdwp_type in PrimitiveType: | ||
type_alias_definition = get_type_alias_definition(jdwp_type) | ||
print(type_alias_definition) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
import unittest | ||
from projects.jdwp.codegen.new_type_generator import get_python_type | ||
from projects.jdwp.defs.schema import PrimitiveType | ||
|
||
|
||
class TestEnumMemberMapping(unittest.TestCase): | ||
def test_enum_member_mappings(self): | ||
for jdwp_type in PrimitiveType: | ||
with self.subTest(jdwp_type=jdwp_type): | ||
result = get_python_type(jdwp_type) | ||
self.assertIsInstance( | ||
result, str, f"Mapping for {jdwp_type} is missing or not a string" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
import unittest | ||
from projects.jdwp.defs.schema import PrimitiveType | ||
from projects.jdwp.codegen.new_type_generator import get_type_alias_definition | ||
|
||
|
||
class TestTypeAliasDefinition(unittest.TestCase): | ||
def test_specific_type_alias_definitions(self): | ||
expected_string_type_definition = ( | ||
"StringType = typing.NewType('StringType', str)" | ||
) | ||
self.assertEqual( | ||
get_type_alias_definition(PrimitiveType.STRING), | ||
expected_string_type_definition, | ||
) | ||
|
||
expected_boolean_type_definition = ( | ||
"BooleanType = typing.NewType('BooleanType', bool)" | ||
) | ||
self.assertEqual( | ||
get_type_alias_definition(PrimitiveType.BOOLEAN), | ||
expected_boolean_type_definition, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |