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

TypeMap based on multiple properties #61

Open
Kingdutch opened this issue Jul 9, 2024 · 1 comment
Open

TypeMap based on multiple properties #61

Kingdutch opened this issue Jul 9, 2024 · 1 comment

Comments

@Kingdutch
Copy link

TypeMap currently only supports a single property but I'd like to be able to use multiple (2) properties to decide a class to deserialize to (and map a class to 2 properties on serialization). I can't implement this as custom typemap because the interface also provides only a single property at a time.

Detailed description

This would be an enhancement to the library.

It would allow serializing structures such as the following to classes in a V1/V2 namespace:

[
{
  "type": "paragraph",
  "version': 1,
  "nodes": [...]
},
{
  "type": "hr",
  "version": 1,
},
{
  "type": "paragraph",
  "version': 2,
  "children": [...]
},
]

Context

I'm trying to bring the Lexical Editor editor state into PHP for storage and inspection. I'd love to use Serde to serialize/deserialize this. The lexical state provides a "type" and "version" field for each node because newer editor versions may contain additional info (e.g. to be up to date with changes in the HTML spec).

There are similar JSON based formats that may have a type and version property. For example the CloudEvents Specification has a "specversion" and event "type" field.

Possible implementation

I'm not entirely sure. I think turning the string in TypeMap (in new attribute) into string[] and allowing those from custom typemaps (although an array lookup table could possibly be made within an attribute). This would require the place where the typemap is called to be updated to deal with N property lookups.

@Crell
Copy link
Owner

Crell commented Jul 10, 2024

Oh geez. I don't know that can be done in TypeMap directly without a BC break. With union types it might be possible, at an API level, but I'm not sure if it's going to cause other issues. This is something I'd have to investigate in more detail, and I'm not sure when I'll have the opportunity to do so. (Though sponsorship to help me find the time is welcome.)

Looks like I didn't follow Garfield's Law: One is a special case of many. :-/

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

2 participants