deltachat / deltachat-core-rust

Delta Chat Rust Core library, used by Android/iOS/desktop apps, bindings and bots 📧
https://delta.chat/en/contribute
Other
669 stars 85 forks source link

Inconsistent tag names for union types in JSON-RPC #4601

Closed link2xt closed 1 year ago

link2xt commented 1 year ago

I have started to write a generator for Python bindings using OpenRPC specification as a source and hit the problem of parsing oneOf types.

Some oneOf types like SystemMessageType are simple enumerations, I convert them to this:

class SystemMessageType(Enum):
    UNKNOWN = "Unknown"
    GROUP_NAME_CHANGED = "GroupNameChanged"
    GROUP_IMAGE_CHANGED = "GroupImageChanged"
    MEMBER_ADDED_TO_GROUP = "MemberAddedToGroup"
    MEMBER_REMOVED_FROM_GROUP = "MemberRemovedFromGroup"
    AUTOCRYPT_SETUP_MESSAGE = "AutocryptSetupMessage"
    SECUREJOIN_MESSAGE = "SecurejoinMessage"
    LOCATION_STREAMING_ENABLED = "LocationStreamingEnabled"
    LOCATION_ONLY = "LocationOnly"
    CHAT_PROTECTION_ENABLED = "ChatProtectionEnabled"
    CHAT_PROTECTION_DISABLED = "ChatProtectionDisabled"
    WEBXDC_STATUS_UPDATE = "WebxdcStatusUpdate"
    EPHEMERAL_TIMER_CHANGED = "EphemeralTimerChanged"
    MULTI_DEVICE_SYNC = "MultiDeviceSync"
    WEBXDC_INFO_MESSAGE = "WebxdcInfoMessage"

class Viewtype(Enum):
    UNKNOWN = "Unknown"
    TEXT = "Text"
    IMAGE = "Image"
    GIF = "Gif"
    STICKER = "Sticker"
    AUDIO = "Audio"
    VOICE = "Voice"
    VIDEO = "Video"
    FILE = "File"
    VIDEOCHAT_INVITATION = "VideochatInvitation"
    WEBXDC = "Webxdc"

But other enums are more complex and use various tags. MessageLoadResult has a tag "variant", MessageQuote uses tag "kind" and Account uses tag "type". I can detect all of them in a general way by looking for a single string field common to all variands, but would be nice to converge to something even at the cost of breaking the API. From a quick search it seems that the most common convention is using kind as a tag.

cc @Simon-Laux

link2xt commented 1 year ago

MuteDuration is even worse, it has this type:

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

So it is either a sting "NotMuted", string "Forever" or an object {"Until": 1234}. I would suggest that we add a tag to it as well.