{"action":"created","issue":{"url":"https://api.github.com/repos/ably/ably-go/issues/9","labels_url":"https://api.github.com/repos/ably/ably-go/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/ably/ably-go/issues/9/comments","events_url":"https://api.github.com/repos/ably/ably-go/issues/9/events","html_url":"https://github.com/ably/ably-go/issues/9","id":67554340,"number":9,"title":"Go JSON / Binary / String support","user":{"login":"mattheworiordan","id":43789,"avatar_url":"https://avatars.githubusercontent.com/u/43789?v=3","gravatar_id":"","url":"https://api.github.com/users/mattheworiordan","html_url":"https://github.com/mattheworiordan","followers_url":"https://api.github.com/users/mattheworiordan/followers","following_url":"https://api.github.com/users/mattheworiordan/following{/other_user}","gists_url":"https://api.github.com/users/mattheworiordan/gists{/gist_id}","starred_url":"https://api.github.com/users/mattheworiordan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattheworiordan/subscriptions","organizations_url":"https://api.github.com/users/mattheworiordan/orgs","repos_url":"https://api.github.com/users/mattheworiordan/repos","events_url":"https://api.github.com/users/mattheworiordan/events{/privacy}","received_events_url":"https://api.github.com/users/mattheworiordan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-10T09:42:00Z","updated_at":"2015-04-10T16:45:51Z","closed_at":null,"body":"As discussed with @kouno, we need to be aware that ALL of our client libraries natively support three payload data types namely:\r\n\r\n* String UTF-8\r\n* Byte arrays (binary)\r\n* JSON\r\n\r\nTypically in dynamically typed languages, when you subscribe to messages, you will be delivered the data type automatically because when the message is received, it is decoded automatically, see https://github.com/ably/ably-ruby/blob/master/SPEC.md#ablymodelsmessageencoders.  \r\n\r\nIn more strongly typed languages such as Java, we still encode & decode automatically and return the correct data type, see https://github.com/ably/ably-java/blob/db50628fceb0af37c63c33693ab77dbb35d3c245/src/io/ably/types/BaseMessage.java#L97-L135.\r\n\r\nHowever, in Go, JSON is not a common data type and instead the norm is to Marshal JSON strings into typed Structs.  The problem for us is that whilst we return a JSON data type in Ruby / Javascript / Java, for us to do this in Go we'd need to return a generic `Interface{}` object, see http://golang.org/src/encoding/json/decode.go?s=2340:2388#L57.  We could do this, but my understanding is that this is largely frowned upon the Go community and instead JSON should be marshalled to a Struct.  If we did return a generic interface, it's quite probable the developer would only then need to marshal it into another object so we're just adding unnecessary processing.\r\n\r\nI don't have a solution, but am raising this issue so that we can discuss.\r\n\r\n@paddybyers any thoughts?"},"comment":{"url":"https://api.github.com/repos/ably/ably-go/issues/comments/91615793","html_url":"https://github.com/ably/ably-go/issues/9#issuecomment-91615793","issue_url":"https://api.github.com/repos/ably/ably-go/issues/9","id":91615793,"user":{"login":"rjeczalik","id":1162017,"avatar_url":"https://avatars.githubusercontent.com/u/1162017?v=3","gravatar_id":"","url":"https://api.github.com/users/rjeczalik","html_url":"https://github.com/rjeczalik","followers_url":"https://api.github.com/users/rjeczalik/followers","following_url":"https://api.github.com/users/rjeczalik/following{/other_user}","gists_url":"https://api.github.com/users/rjeczalik/gists{/gist_id}","starred_url":"https://api.github.com/users/rjeczalik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjeczalik/subscriptions","organizations_url":"https://api.github.com/users/rjeczalik/orgs","repos_url":"https://api.github.com/users/rjeczalik/repos","events_url":"https://api.github.com/users/rjeczalik/events{/privacy}","received_events_url":"https://api.github.com/users/rjeczalik/received_events","type":"User","site_admin":false},"created_at":"2015-04-10T16:45:51Z","updated_at":"2015-04-10T16:45:51Z","body":"> We could do this, but my understanding is that this is largely frowned upon the Go community and instead JSON should be marshalled to a Struct.\r\n\r\nJava's `JSONArray` ~is Go`s `[]interface{}`, the same `JSONObject` -> `map[string]interface{}`. In order to just have the functionality of automatic decoding `interface{}` is fine.\r\n\r\nEven if either:\r\n\r\n- user provided a TypeMap (`map[string]reflect.Type`) for each channel to instruct the Channel how to unmarshal the payload for each message before delivering it to the listener\r\n- we built the type map information automatically by inspecting user-provided listener with reflection\r\n- we wanted to defer unmarshmaling of the payload to the user by making Channel configurable, so the user could then use raw []byte and unmarshal it by hand into a struct\r\n\r\nthere'd be an limitation that user would have to ensure the payload type is the same for each message with the same name for given channel. In other words if user is able to map `protocol.Message` programatically to his/her own struct, then we're able to do it automatically. I don't see how that mapping would look like.\r\n\r\nSo we're left with the default behaviour of unmarshalling into `interface{}` - that is to unmarshal payload to generic interface{} (either an array - `[]interface{}`, or an object - `map[string]interface{}`)."}}