Skip to content

Commit

Permalink
[jdwp] make jdwp schema immutable and hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
michalgr committed Dec 5, 2023
1 parent df7db48 commit babe5a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
28 changes: 14 additions & 14 deletions projects/jdwp/defs/command_sets/event_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@
"Modifier cases",
TaggedUnion(
__SetCommand_out_modKind,
{
ModifierKind.COUNT: CountModifier,
ModifierKind.CONDITIONAL: ConditionalModifier,
ModifierKind.THREAD_ONLY: ThreadOnlyModifier,
ModifierKind.CLASS_ONLY: ClassOnlyModifier,
ModifierKind.CLASS_MATCH: ClassMatchModifier,
ModifierKind.CLASS_EXCLUDE: ClassExcludeModifier,
ModifierKind.STEP: StepModifier,
ModifierKind.LOCATION_ONLY: LocationOnlyModifier,
ModifierKind.EXCEPTION_ONLY: ExceptionOnlyModifier,
ModifierKind.FIELD_ONLY: FieldOnlyModifier,
ModifierKind.INSTANCE_ONLY: InstanceOnlyModifier,
ModifierKind.SOURCE_NAME_MATCH: SourceNameMatchModifier,
},
[
(ModifierKind.COUNT, CountModifier),
(ModifierKind.CONDITIONAL, ConditionalModifier),
(ModifierKind.THREAD_ONLY, ThreadOnlyModifier),
(ModifierKind.CLASS_ONLY, ClassOnlyModifier),
(ModifierKind.CLASS_MATCH, ClassMatchModifier),
(ModifierKind.CLASS_EXCLUDE, ClassExcludeModifier),
(ModifierKind.STEP, StepModifier),
(ModifierKind.LOCATION_ONLY, LocationOnlyModifier),
(ModifierKind.EXCEPTION_ONLY, ExceptionOnlyModifier),
(ModifierKind.FIELD_ONLY, FieldOnlyModifier),
(ModifierKind.INSTANCE_ONLY, InstanceOnlyModifier),
(ModifierKind.SOURCE_NAME_MATCH, SourceNameMatchModifier),
],
),
"Modifier cases.",
)
Expand Down
16 changes: 14 additions & 2 deletions projects/jdwp/defs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, Generic, TypeVar, Type as TypeAlias, Union
from typing import Optional, Generic, Tuple, TypeVar, Type as TypeAlias, Union
from collections.abc import Mapping
from enum import Enum
from collections.abc import Set, Sequence
Expand Down Expand Up @@ -85,7 +85,10 @@ class TaggedUnion(Generic[EnumT]):
"""Tagged Union class type"""

tag: Field[UnionTag[EnumT]]
cases: Mapping[EnumT, Struct]
cases: Sequence[Tuple[EnumT, Struct]]

def __post_init__(self):
object.__setattr__(self, "cases", tuple(self.cases))


@dataclass(frozen=True)
Expand Down Expand Up @@ -114,6 +117,9 @@ class Struct:

fields: Sequence[Field[Type]]

def __post_init__(self):
object.__setattr__(self, "fields", tuple(self.fields))


@dataclass(frozen=True)
class Command:
Expand All @@ -125,6 +131,9 @@ class Command:
reply: Optional[Struct]
error: Set[ErrorType]

def __post_init__(self):
object.__setattr__(self, "error", frozenset(self.error))


@dataclass(frozen=True)
class CommandSet:
Expand All @@ -133,3 +142,6 @@ class CommandSet:
name: str
id: int
commands: Sequence[Command]

def __post_init__(self):
object.__setattr__(self, "commands", tuple(self.commands))
6 changes: 5 additions & 1 deletion projects/jdwp/tests/defs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class SchemaTests(unittest.TestCase):
def test_schema_can_be_imported(self):
from projects.jdwp.defs.schema import OpaqueType

def test_command_sets_can_be_imported(self):
def test_command_sets_are_hashable(self):
from projects.jdwp.defs.command_sets import (
event_request,
reference_type,
virtual_machine,
)

hash(event_request.EventRequest)
hash(reference_type.ReferenceType)
hash(event_request.EventRequest)

0 comments on commit babe5a8

Please sign in to comment.