Closed ahopkins closed 1 year ago
To work around this:
multiple = Shape([Point(*data) for data in decode(multiple_points)])
But, this seems like something that I should not have to do.
I think there's some confusion here - array_like=True
serializes structs like tuples. By your definition Shape
would expect a length-1 array as input that contains an array of length-2 arrays (for the points) - the same as if it was of type tuple[list[Point]]
. This works as expected:
from msgspec import Struct
from msgspec.json import decode
class Point(Struct, array_like=True):
lng: float
lat: float
class Shape(Struct, array_like=True):
points: list[Point]
# indenting to make the nested structure clearer:
msg = b"""
[
[
[10,20],
[30,40],
[50,60]
]
]
"""
result = decode(msg, type=Shape)
print(result)
#> Shape(
#> points=[
#> Point(lng=10.0, lat=20.0),
#> Point(lng=30.0, lat=40.0),
#> Point(lng=50.0, lat=60.0)
#> ]
#> )
To decode messages of the structure you have above, you'd decode them into a list[Point]
, no need to define a top-level object.
from msgspec import Struct
from msgspec.json import decode
class Point(Struct, array_like=True):
lng: float
lat: float
msg = b"""
[
[10,20],
[30,40],
[50,60]
]
"""
result = decode(msg, type=list[Point])
print(result)
#> [
#> Point(lng=10.0, lat=20.0),
#> Point(lng=30.0, lat=40.0),
#> Point(lng=50.0, lat=60.0)
#> ]
Ahh, I see. Thanks so much for the quick response. Sorry about that. Thanks for the awesome project. It's been a lot of fun playing with and I look forward to using it more.
No worries, and thanks for the kind words! Please let me know if you run into any further questions or issues.
@jcrist :laughing: Just realized you posted on this PR: https://github.com/sanic-org/sanic-ext/pull/197
Thanks. I feel like the ability of not only handling the modeling, but also the deserialization calls for a deeper integration, but I'm still working out in my head how to accomplish that.
Description
It seems that nested
array_like=True
is not working as expected.Here's the scenario:
The output I receive:
As you can see, the
Point
is fine, but the list of points is not.