ably-go icon indicating copy to clipboard operation
ably-go copied to clipboard

Json messages are not decoded transparently

Open Morganamilo opened this issue 3 years ago • 2 comments

When a struct is sent over a channel it is turned to json. Then when it is received on the other end it is meant to be transparently turned back to that struct. Instead, a json string is received.

However this works correctly when using the history API. This means the messages are being correctly sent but not received.

The history code also converts to a generic map instead of the struct. Both should be changed to return the struct.

Clients may rely on this bad behaviour so this change will need to be for v2.

┆Issue is synchronized with this Jira Task by Unito

Morganamilo avatar Feb 18 '22 11:02 Morganamilo

The history code also converts to a generic map instead of the struct. Both should be changed to return the struct.

I think this will need some form of struct registration, because the SDK won't be able to infer which in-memory struct to decode into without being told about it.

It may be worth considering a Message.Decode(dst interface{}) method which decodes msg.Data into the provided object:

channel.Subscribe(ctx, func(msg *ably.Message) {
	var data MyStruct
	if err := msg.Decode(&data); err != nil {
		return
	}
	// do something with data
})

Longer term we'd like to have first class support for schemas (e.g. register a protobuf definition for a channel, and the SDK decodes messages based on that definition).

lmars avatar Feb 18 '22 17:02 lmars

It may be worth considering a Message.Decode(dst interface{}) method

This is a wider question than just for this library and should be discussed as such. @QuintinWillison

paddybyers avatar Feb 19 '22 08:02 paddybyers