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)
- Hot to get field alias name from Struct? HOT 3
- Struct `__post_init__` is not called when converting to Struct with `from_attributes=True` HOT 2
- can msgspec provide a faster enum implementation? HOT 1
- Coerce a `None` value to the default HOT 4
- ForwardRef is not supported
- License discrepancy HOT 1
- Support freezegun fake types
- Let json.schema take an enc_hook for handling default values of custom types
- Support encoding any Enum value type
- Json Encoder: dataclass classes are encoded to empty dict
- Tighten typing of msgspec.json.schema
- Support numeric constraints for Decimal values
- Support decoding empty strings as unset for all field types
- Does msgspec have a concept of schema mode?
- Add an option to disable constraint checks
- Ability to add an example to a Struct as output to the JSON schema
- Unable to encode `lxml.etree._ElementUnicodeResult`
- fallback to dict for unknown type in tagged union? HOT 3
- Multiple types to same Struct HOT 1
- Encode field name
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.