How to convert data to custom class instance while unpacking #62
|
Hi, I'm a fan of the declarative nature of caterpillar and I'm evaluating if it would be a good fit to replace the existing binary parsing system in a project. One thing I'm trying to accomplish is annotating fields in a struct with a custom non-struct type even though a normal Here's an example that hopefully illustrates what I mean: What I would like to happen is: when parsing If I try to run this as-is, I get the error: I've read through the documentation but I haven't been able to come up with anything for this situation.
For Actions I'm not sure if something like this can work: The troublesome part for all this seems to be the fact that |
Replies: 1 comment 1 reply
|
Hi @qasikfwn , I think what you are looking for is the from caterpillar.py import Transformer, f, uint64
class Custom64bitHash:
h: int
def __init__(self, h: int) -> None:
# do some other stuff with `h`
# ...
self.h = h
class _64bitHash(Transformer[Custom64bitHash, int, Custom64bitHash, int]):
def __init__(self):
# parses a uint64 first
super().__init__(uint64)
def encode(self, obj: Custom64bitHash, context) -> int:
return obj.h
def decode(self, parsed: int, context) -> Custom64bitHash:
return Custom64bitHash(parsed)
Custom64bitHash_t = f[Custom64bitHash, _64bitHash()]Here, Also, I did look into the documentation again and this could the only component that is still missing from the reference/tutorial -> will be marked as an open issue. |
Hi @qasikfwn ,
I think what you are looking for is the
Transformerclass:Here,
uint64is parsed first and you can de…