Skip to content

Commit

Permalink
Added newtype support for primitive type enum (#76)
Browse files Browse the repository at this point in the history
* 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
Daquiver1 authored Nov 21, 2023
1 parent 9c0f4e4 commit 16396ad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions projects/jdwp/codegen/new_type_generator.py
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)
19 changes: 19 additions & 0 deletions projects/jdwp/tests/test_primitve_type.py
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()
28 changes: 28 additions & 0 deletions projects/jdwp/tests/test_type_alias_definition.py
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()

0 comments on commit 16396ad

Please sign in to comment.