kalgory-com / kalgory-py

Python client library for kalgory
Mozilla Public License 2.0
0 stars 0 forks source link

Parse the payload between user-defined type and bytes #6

Closed ZhaoTzuHsien closed 1 day ago

ZhaoTzuHsien commented 1 week ago

WIT definition

You can always refer to wit/block.wit for latest definition.

package kalgory:component@0.1.0;

world block {
    export execute: func(payload: list<u8>) -> result<list<u8>>;
}

The type of payload in WIT

I changed the type of Payload to list<u8> in WIT in order to support different encoding methods in the future, such as Protocol Buffers or Cap'n Proto. In Python's perspective, list<u8> is equivalent to bytes.

Payload format

Currently, we decide to use JSON as the format of Payload for its simplicity. It models as an JSON array with each of its items being a valid JSON value for user-defined parameters. Here is an example of a payload:

[
  "NVDA",
  20,
  {
    "o": 127.38,
    "h": 128.85,
    "l": 125.68,
    "c": 125.83
  }
]

When receiving the above payload, we know there are 3 parameters for user-defined handle function:

class Block(BaseBlock):
    def handle(stock: str, amount: int, ohlc: NamedTuple) -> bytes:
        pass

Return format

Since Python supports tuple return, we collect each item in the returned tuple and combine them into a JSON array.

For example, if the user define the following Python code:

class Block(BaseBlock):
    def handle(stock: str, amount: int, ohlc: NamedTuple) -> (str, int):
        return "NVDA", 100

The library should parse the data into the following JSON value and convert it to bytes:

[
  "NVDA",
  100
]

Be aware that if the user raise an exception in the handle function, the execute function should also raise the exception in order to notify the host that there is an error in the block.

ZhaoTzuHsien commented 1 week ago

@ChuWeiChang

Please describe how you want to implement this feature.

ChuWeiChang commented 1 week ago

block.execute serves as a "little host", which parse the input data -> feed into "handle" and get the return data -> turn the data into json -> output the json i am currently thinking if some checks are needed

ZhaoTzuHsien commented 1 week ago

Can you describe more about what types of checks are needed? If they are not related to the parsing scope, feel free to open a new issue.

ChuWeiChang commented 1 week ago

I am thinking about checking if return values' type consistent with the annotation, since Cpython interpreter dont check it.

ex:


def add(x: int, y: int) -> int:
    return "ss"
b = add(2,3)
print(b)

this program runs somehow But i am not sure if it's turned to wasm, same thing could happen or not I will make some tests to see if it's handled automatically, and add the checking codes if neccessary

ZhaoTzuHsien commented 1 week ago

Be aware that CPythom doesn't validate the annotation of parameters either. It's good to enforce type-checking when data passes the boundary between JSON and Python types.