Skip to content

Commit

Permalink
[jdwp] generate projects.jdwp.runtime.structs
Browse files Browse the repository at this point in the history
  • Loading branch information
michalgr committed Dec 6, 2023
1 parent 560c892 commit 721f170
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
9 changes: 9 additions & 0 deletions projects/jdwp/codegen/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ python_binary(
visibility=["//projects/jdwp/runtime/..."],
)

python_binary(
name="generate-dataclasses",
main_module="projects.jdwp.codegen.dataclass_generator",
deps=[
":codegen",
],
visibility=["//projects/jdwp/runtime/..."],
)

python_library(
name="codegen",
srcs=glob(["**/*.py"]),
Expand Down
53 changes: 47 additions & 6 deletions projects/jdwp/codegen/dataclass_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

import enum
from textwrap import dedent
from projects.jdwp.codegen.types import python_type_for
import typing

from projects.jdwp.defs.schema import (
Array,
ArrayLength,
Command,
CommandSet,
Field,
Struct,
TaggedUnion,
Expand Down Expand Up @@ -59,14 +62,13 @@ def __generate_dataclass(self, struct: Struct) -> str:
class_def = f"@dataclasses.dataclass(frozen=True)\nclass {name}:\n{fields_def}"
return dedent(class_def)

def generate(self):
return [
self.__generate_dataclass(nested)
for _, _, nested in reversed(list(nested_structs(self.__root)))
] + [self.__generate_dataclass(self.__root)]
def generate(self) -> typing.Generator[str, None, None]:
for _, _, nested in reversed(list(nested_structs(self.__root))):
yield self.__generate_dataclass(nested)
yield self.__generate_dataclass(self.__root)


def format_enum_name(enum_value):
def format_enum_name(enum_value: enum.Enum) -> str:
words = enum_value.name.split("_")
formatted_name = "".join(word.capitalize() for word in words)
return f"{formatted_name}Type"
Expand Down Expand Up @@ -110,3 +112,42 @@ def compute_struct_names(root: Struct, name: str) -> typing.Mapping[Struct, str]
case_struct
] = f"{names[parent]}{sanitized_field_name}Case{case_name}"
return names


def generate_for_command(command: Command) -> typing.Generator[str, None, None]:
if command.out:
yield from StructGenerator(command.out, f"{command.name}Out").generate()
if command.reply:
yield from StructGenerator(command.reply, f"{command.name}Reply").generate()


def generate_for_command_set(
command_set: CommandSet,
) -> typing.Generator[str, None, None]:
for command in command_set.commands:
yield from generate_for_command(command)


def generate_for_all_command_sets() -> typing.Generator[str, None, None]:
# TODO: refactor this once PR90 is merged
from projects.jdwp.defs.command_sets.virtual_machine import VirtualMachine
from projects.jdwp.defs.command_sets.reference_type import ReferenceType
from projects.jdwp.defs.command_sets.event_request import EventRequest

yield from generate_for_command_set(VirtualMachine)
yield from generate_for_command_set(ReferenceType)
yield from generate_for_command_set(EventRequest)


def main():
print("import dataclasses")
print("import typing")
print("from projects.jdwp.runtime.type_aliases import *")

for struct_definition in generate_for_all_command_sets():
print()
print(struct_definition)


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions projects/jdwp/runtime/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ genrule(
cmd = "$(exe //projects/jdwp/codegen:generate-new-types) > $OUT",
)

genrule(
name = "structs",
out = "structs.py",
cmd = "$(exe //projects/jdwp/codegen:generate-dataclasses) > $OUT",
)

python_library(
name = "runtime",
srcs = [
":structs",
":type-aliases",
"async_streams.py",
"jdwpstruct.py",
Expand Down
8 changes: 8 additions & 0 deletions projects/jdwp/tests/runtime/structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

import unittest


class GeneratedStructsTest(unittest.TestCase):
def test_structs_can_be_imported(self):
from projects.jdwp.runtime.structs import IDSizesReply
6 changes: 3 additions & 3 deletions projects/jdwp/tests/test_dataclass_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_simple_struct(self):
" id: int"
]

self.assertEqual(result, expected)
self.assertSequenceEqual(list(result), expected)

def test_nested_struct(self):
inner_struct = Struct(
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_nested_struct(self):
" nested: OuterStructNested",
]

self.assertEqual(result, expected)
self.assertSequenceEqual(list(result), expected)

def test_struct_in_array(self):
# Define a structure
Expand Down Expand Up @@ -101,4 +101,4 @@ def test_struct_in_array(self):
" arrayOfElements: typing.List[ArrayStructArrayOfElementsElement]",
]

self.assertEqual(result, expected)
self.assertSequenceEqual(list(result), expected)

0 comments on commit 721f170

Please sign in to comment.