Comments (3)
There's no native way to spell this cleanly right now (I'm not even sure what am API for that might look like), but this pattern can be supported in a few different ways through extensions. Do you also need to reserialize the data in the same format? Or is this for decoding only?
from msgspec.
Hope it's alright to chime in here, I saw the discussion on Mastodon. It sort of reminds me of relationship attributes in SQLModel/SQLAlchemy, and somehow the type annotation would need to carry a coupling between the encoded types and the more ergonomic decoded struct.
A pseudo-Python API example (black magic omitted) might involve explicitly defining the encoded representation, then the more ergonomic, decoded struct "reshapes" that data on decode. And it would shape it back again on encode, I guess? Key uniqueness wouldn't be preserved in the decoded structs, and probably only re-validated at the edge? In the following example, underscore-suffixed classes represent encoded structures, and the non-suffixed classes represent decoded structures. Also, I've omitted @define
or struct subclassing in this pseudo-API.
# Need black magic here...
MailboxSize = int
MailboxName = str
ServerName = str
class Mailbox_:
size: MailboxSize
class Server_:
server: ServerName
mailboxes: dict[MailboxName, Mailbox_]
class Mailbox:
name: MailboxName
size: MailboxSize
class Server:
name: ServerName # renamed this attribute from the OP example
mailboxes: list[Mailbox]
This is the most explicit API representation I could imagine, and does entail duplication, but such an API would give lots of expressivity in "transformations" of structs, not just in flattening mappings into lists of instances with keys as instance attributes. Though a more focused implementation might hack Annotated
types or use a magic Field
type that does linking to encoded representations, with less duplication but less expressivity, maybe?
from msgspec.
There's no native way to spell this cleanly right now (I'm not even sure what am API for that might look like), but this pattern can be supported in a few different ways through extensions. Do you also need to reserialize the data in the same format? Or is this for decoding only?
I do not, but you also don't have to add any functionality just for me. :) I can achieve that using an extra transformation step as outlined by Blake β I was just surprised that I couldn't find a straight-forward solution to an ostensibly common problem in any serialization package so I thought I'd ask around if I'm missing something. :)
Thanks for humoring me!
from msgspec.
Related Issues (20)
- Typed encoding HOT 1
- Validation on serialization HOT 5
- Make msgspec more aware of large data and other serialization protocols
- Recommendation for Efficiently Decoding BSON from PyMongo with msgspec
- leading underscores and `"camel"` rename strategy HOT 2
- Constraint check for typing.Optional field HOT 1
- Automatically support `functools.cached_property` without requiring setting `dict=True` HOT 2
- UnboundLocalError for `new_scope` HOT 3
- Sign release tags when making future releases? HOT 4
- How can I do data normalization on a frozen instance in `__post_init__`? HOT 1
- Cannot convert with `from_attributes` when using a rename convention HOT 3
- Cannot set `gc=False` on Generic structs? HOT 5
- Support types.MappingProxyType HOT 3
- Add either `init_omit_defaults` or `omit_none` HOT 5
- Consider making `DecodeError` and `ValidationError` inherit from `ValueError` HOT 1
- Docs page on testing
- json schema generation - differences between pydantic and msgspec HOT 3
- Allow conversion to collection from generator HOT 2
- Porting guide for users coming from `orjson`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgspec.