Generate the interface (ICRM) file based on the provided Core Resource Model (CRM) implementation. The ICRM should include all necessary class declarations, interface methods, and serialization/deserialization functions that match the CRM implementation.
In our architecture:
- CRM (Core Resource Model): Contains the actual implementation of functionality
- ICRM (Interface for Core Resource Model): Defines the interface and serialization methods
These need to stay synchronized, with the ICRM properly reflecting all methods decorated with @cc.transfer in the CRM.
I will provide a CRM implementation file. This file contains a class implementing specific functionality with methods that are decorated with @cc.transfer annotations specifying input_name and output_name parameters.
-
Class Declarations:
- Extract and declare any complex classes used as parameters or return types in the interface (similar to
GridAttributeandGridSchema) - Include proper type hints and docstrings
- Extract and declare any complex classes used as parameters or return types in the interface (similar to
-
Interface Class:
- Create an interface class that matches the CRM class
- IMPORTANT: Do NOT include an
__init__method in the interface class - Include all methods decorated with
@cc.transferin the CRM - Preserve the method signatures, docstrings, and decorator parameters
- Set direction attribute to '->' (component side direction) as a class variable
-
Serialization/Deserialization Functions:
- MANDATORY: Create serialization/deserialization functions for EVERY SINGLE
input_nameandoutput_namein the@cc.transferdecorators - CRITICAL: BOTH serialization AND deserialization functions MUST be implemented for ALL types
- PROHIBITED: Do NOT skip implementing any function with excuses like "No deserialization needed as it's only input" or "No serialization needed as it's only output"
- For each unique
input_nameandoutput_name:- Create a serialization function that converts Python objects to bytes
- Create a deserialization function that converts bytes back to Python objects
- CRUCIAL: The inputs/outputs of serialization/deserialization functions MUST EXACTLY match the corresponding method parameters:
- Serialization function parameters must exactly match the method inputs (don't combine them into tuples or other wrappers)
- Deserialization function output must exactly match the method output structure
- Follow the naming pattern:
serialize_Xanddeserialize_Xwhere X is the name from input_name/output_name - IMPORTANT: All serialization/deserialization functions MUST use PyArrow Tables as an intermediate format:
- For serialization:
- Define a PyArrow schema for the data structure
- Convert Python objects to PyArrow-compatible format (dict, list, arrays)
- Create a PyArrow Table from the data
- Use
cc.message.serialize_from_table(table)to convert to bytes
- For deserialization:
- Use
cc.message.deserialize_to_rows()orcc.message.deserialize_to_table()to convert bytes to PyArrow data - Extract data from PyArrow format and convert back to Python objects
- Use
- For serialization:
- MANDATORY: Create serialization/deserialization functions for EVERY SINGLE
-
Registration Code:
- CRITICAL: EVERY
register_wrappercall MUST include BOTH a serialization function AND a deserialization function - PROHIBITED: NEVER use
Noneor any other placeholder in place of a real function for either serializer or deserializer - WRONG EXAMPLE:
# THIS IS INCORRECT - DO NOT DO THIS: cc.message.register_wrapper('PeerGridInfos', serialize_peer_grid_infos, None) # No deserialization needed as it's only input
- CORRECT EXAMPLE:
# THIS IS CORRECT - ALWAYS DO THIS: cc.message.register_wrapper('PeerGridInfos', serialize_peer_grid_infos, deserialize_peer_grid_infos)
- Generate registration code using
cc.message.register_wrapperfor all serialization/deserialization pairs - IMPORTANT: ALL serialization/deserialization function pairs MUST be registered, even if you think they are only used in one direction
- CRITICAL: EVERY
-
Type Annotations:
- Use modern Python type annotations (Python 3.9+) without importing from the
typingmodule - Examples:
- Use
list[int]instead oftyping.List[int] - Use
dict[str, int]instead oftyping.Dict[str, int] - Use
tuple[int, str]instead oftyping.Tuple[int, str] - Use Union types with pipe notation
str | Noneinstead oftyping.Optional[str]ortyping.Union[str, None]
- Use
- Use modern Python type annotations (Python 3.9+) without importing from the
For a method in CRM like:
@cc.transfer(input_name='PeerGridInfos', output_name='GridAttributes')
def get_grid_infos(self, level: int, global_ids: list[int]) -> list[GridAttribute]:
# implementationThe serialization/deserialization functions must be:
# Input serialization - parameters match method input exactly
def serialize_peer_grid_infos(level: int, global_ids: list[int]) -> bytes:
# implementation
# Output deserialization - returns exactly what the method returns
def deserialize_grid_attributes(arrow_bytes: bytes) -> list[GridAttribute]:
# implementationAnd registered like this:
# BOTH functions must be provided:
cc.message.register_wrapper('PeerGridInfos', serialize_peer_grid_infos, deserialize_peer_grid_infos)
cc.message.register_wrapper('GridAttributes', serialize_grid_attributes, deserialize_grid_attributes)def serialize_grid_schema(schema: GridSchema) -> bytes:
# Define PyArrow schema
pa_schema = pa.schema([
pa.field('epsg', pa.int32()),
pa.field('bounds', pa.list_(pa.float64())),
pa.field('first_size', pa.float64()),
pa.field('subdivide_rules', pa.list_(pa.list_(pa.int32())))
])
# Convert to PyArrow-compatible format
data = {
'epsg': schema.epsg,
'bounds': schema.bounds,
'first_size': schema.first_size,
'subdivide_rules': schema.subdivide_rules
}
# Create PyArrow Table
table = pa.Table.from_pylist([data], schema=pa_schema)
# Serialize to bytes
return cc.message.serialize_from_table(table)- Match the exact method signatures, including parameter names and type hints
- Preserve docstrings from the original methods
- Handle any imports needed for the interface
- Consider dependencies between classes and methods
- The interface class should NOT have an implementation of methods, only declarations
- All serialization MUST use PyArrow Tables as the intermediate format
- DO NOT combine parameters into tuples or other containers when creating serialization functions
- You MUST implement BOTH serialization AND deserialization functions for ALL input_name and output_name values
- You MUST implement all functions even if they seem redundant or unused
- You MUST provide BOTH serialization AND deserialization functions in EVERY register_wrapper call
- NEVER use None, null, or any placeholder in the registration code - always provide real functions