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

Added newtype support for primitive type enum #76

Merged
merged 16 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
27 changes: 27 additions & 0 deletions projects/jdwp/codegen/new_type_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""New type Generator."""
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
from projects.jdwp.defs.schema import PrimitiveType
import typing


def get_python_type(jdwp_type: PrimitiveType) -> str:
"""Map JDWP type to Python type."""
mapping = {
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
PrimitiveType.STRING: "str",
PrimitiveType.INT: "int",
PrimitiveType.BYTE: "int",
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():
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
for jdwp_type in PrimitiveType:
type_alias_definition = get_type_alias_definition(jdwp_type)
print(type_alias_definition)
2 changes: 2 additions & 0 deletions projects/jdwp/defs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class PrimitiveType(Enum):

STRING = "string"
BOOLEAN = "boolean"
INT = "int"
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
BYTE = "int"
DICT = "dict"
REFERENCE_TYPE_ID = "referenceTypeID"
CLASS_LOADER = "classLoader"
Expand Down
3 changes: 3 additions & 0 deletions projects/jdwp/tests/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
python_test(
name = "tests",
srcs = glob(["*.py"]),
deps = [
"//projects/jdwp:lib",
],
)
17 changes: 17 additions & 0 deletions projects/jdwp/tests/test_primitve_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
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()
19 changes: 19 additions & 0 deletions projects/jdwp/tests/test_type_alias_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
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_type_alias_definitions(self):
for jdwp_type in PrimitiveType:
Daquiver1 marked this conversation as resolved.
Show resolved Hide resolved
with self.subTest(jdwp_type=jdwp_type):
expected_start = f"{jdwp_type.name.capitalize()}Type = typing.NewType"
definition = get_type_alias_definition(jdwp_type)
self.assertTrue(
definition.startswith(expected_start),
f"Definition for {jdwp_type} is incorrect",
)


if __name__ == "__main__":
unittest.main()