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

api!(deltachat-jsonrpc): use kind as a tag for all union types #4602

Merged
merged 1 commit into from
Aug 4, 2023

Conversation

link2xt
Copy link
Collaborator

@link2xt link2xt commented Aug 3, 2023

Closes #4601

@link2xt link2xt requested a review from Simon-Laux August 3, 2023 19:38
@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch 2 times, most recently from bff0aef to 188d21f Compare August 3, 2023 19:55
@link2xt link2xt removed the request for review from Simon-Laux August 3, 2023 20:08
@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch 4 times, most recently from 90331c7 to a883c2b Compare August 3, 2023 20:46
@link2xt
Copy link
Collaborator Author

link2xt commented Aug 3, 2023

I have tried to change MuteDuration declaration to this:

#[derive(Clone, Serialize, Deserialize, TypeDef, schemars::JsonSchema)]
#[serde(tag = "kind")]
pub enum MuteDuration {
    NotMuted, 
    Forever,
    Until(i64),
}

It results in this OpenRPC specification:

[{'properties': {'kind': {'enum': ['NotMuted'], 'type': 'string'}},
  'required': ['kind'],
  'type': 'object'},
 {'properties': {'kind': {'enum': ['Forever'], 'type': 'string'}},
  'required': ['kind'],
  'type': 'object'},
 {'format': 'int64',
  'properties': {'kind': {'enum': ['Until'], 'type': 'string'}},
  'required': ['kind'],
  'type': ['object', 'integer']}]

'type': ['object', 'integer'] looks weird.

Generated TypeScript type is this:

export type MuteDuration=({"kind":"NotMuted";}|{"kind":"Forever";}|({"kind":"Until";}&I64));

({"kind":"Until";}&I64) seems like an empty type? This question confirms:
https://stackoverflow.com/questions/65908355/why-is-it-allowed-to-intersect-primitives-and-objects-in-typescript

I am going to change the type to have no anonymous fields so this does not happen, but also reported a bug at dbeckwith/rust-typescript-type-def#24

@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch 2 times, most recently from 2febab8 to 454d097 Compare August 3, 2023 23:40
@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch from 454d097 to e12044e Compare August 3, 2023 23:46
@link2xt link2xt marked this pull request as draft August 4, 2023 01:26
@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch 2 times, most recently from 31e838a to 88d0087 Compare August 4, 2023 03:05
@link2xt
Copy link
Collaborator Author

link2xt commented Aug 4, 2023

There is a scripts/generate_openrpc_bindings.py which calls deltachat-rpc-server --openrpc and generates some Python code.

There are some weird types remaining, all involving maps, for example:

  • {'type': 'object', 'additionalProperties': {'type': 'string'}} for BTreeMap<&'static str, String>, the return type of get_system_info()
  • {'type': 'object', 'additionalProperties': {'type': ['string', 'null']}} for HashMap<String, Option<String>>, the return type of batch_get_config()
  • {'type': 'object', 'additionalProperties': {'type': 'string'}} for HashMap<u32, String>, the argument of set_stock_strings
  • {'type': 'object', 'additionalProperties': {'$ref': '#/components/schemas/MessageLoadResult'}} for HashMap<u32, MessageLoadResult>, the return type of get_messages

In Reactions there is a field reactions_by_contact, it has this type:

{'additionalProperties': {'items': {'type': 'string'}, 'type': 'array'},
 'description': "Map from a contact to it's reaction to message.",
 'type': 'object'

The problem is that the key of the map is nowhere to be found, it's a bug in yerpc or its dependencies probably. Worst case we have to replace these types with a list of tuples.

Once this is fixed the parser is ready and the remaining part is generating the code to serialize/deserialize arguments and results.

I opened an upstream issue at GREsau/schemars#235

@link2xt link2xt changed the title api!(deltachat-jsonrpc): use kind as a tag for all union types JSON-RPC Python bindings generator Aug 4, 2023
@link2xt link2xt force-pushed the link2xt/unify-jsonrpc-tags branch 2 times, most recently from 7650d19 to e12044e Compare August 4, 2023 14:36
@link2xt link2xt changed the title JSON-RPC Python bindings generator api!(deltachat-jsonrpc): use kind as a tag for all union types Aug 4, 2023
@link2xt link2xt marked this pull request as ready for review August 4, 2023 14:38
@link2xt link2xt merged commit e12044e into master Aug 4, 2023
@link2xt link2xt deleted the link2xt/unify-jsonrpc-tags branch August 4, 2023 14:39
@link2xt
Copy link
Collaborator Author

link2xt commented Aug 4, 2023

I have merged this change as no other type changes are needed.
JSON-RPC bindings generator is moved to #4603

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

Successfully merging this pull request may close these issues.

Inconsistent tag names for union types in JSON-RPC
2 participants