Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize tag field position in array_like struct encoding #736

Open
regnarg opened this issue Sep 16, 2024 · 0 comments
Open

Allow to customize tag field position in array_like struct encoding #736

regnarg opened this issue Sep 16, 2024 · 0 comments

Comments

@regnarg
Copy link

regnarg commented Sep 16, 2024

Description

When we create a tagged array_like struct, it is not possible to influence where the tag field is in the field order. This prevents using this feature to implement msgpack-rpc. In msgpack-rpc, the request is a 4-element array [type,msgid,method,params], where the type of params depends on the method. An intuitive way to implement this would be to create a single struct for each method, tagged by the 'method' field, and then use a Union of those to decode the request. However, this currently cannot be done as there is no way to put the tag on the third position.

In contrast to that, for JSON-RPC (where requests are dicts instead of arrays, this can be done):

    def _create_request_decoder(self):
        request_types = []
        for method_name, method_spec in self.methods.items():
            request_type = msgspec.defstruct(method_name, [
                ('jsonrpc', Literal["2.0"]),
                ('params', method_spec.params_type),
                ('id', str|int|None, None),
            ], tag=method_name, tag_field="method")
            request_types.append(request_type)
        return msgspec.json.Decoder(Union[*request_types])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant