{"action":"created","issue":{"url":"https://api.github.com/repos/ugorji/go/issues/141","repository_url":"https://api.github.com/repos/ugorji/go","labels_url":"https://api.github.com/repos/ugorji/go/issues/141/labels{/name}","comments_url":"https://api.github.com/repos/ugorji/go/issues/141/comments","events_url":"https://api.github.com/repos/ugorji/go/issues/141/events","html_url":"https://github.com/ugorji/go/issues/141","id":134028553,"number":141,"title":"Elements ignored when encoding struct embedding a Selfer","user":{"login":"2opremio","id":2362916,"avatar_url":"https://avatars.githubusercontent.com/u/2362916?v=3","gravatar_id":"","url":"https://api.github.com/users/2opremio","html_url":"https://github.com/2opremio","followers_url":"https://api.github.com/users/2opremio/followers","following_url":"https://api.github.com/users/2opremio/following{/other_user}","gists_url":"https://api.github.com/users/2opremio/gists{/gist_id}","starred_url":"https://api.github.com/users/2opremio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/2opremio/subscriptions","organizations_url":"https://api.github.com/users/2opremio/orgs","repos_url":"https://api.github.com/users/2opremio/repos","events_url":"https://api.github.com/users/2opremio/events{/privacy}","received_events_url":"https://api.github.com/users/2opremio/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ugorji/go/labels/WorkAsDesigned","name":"WorkAsDesigned","color":"009800"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":27,"created_at":"2016-02-16T16:34:24Z","updated_at":"2016-02-18T04:31:01Z","closed_at":"2016-02-17T05:12:37Z","body":"Here's a simplified repro:\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n        \"bytes\"\r\n        \"fmt\"\r\n\r\n        \"github.com/ugorji/go/codec\"\r\n)\r\n\r\n// We have defined Selfers for a type (either with codecgen or manually)\r\ntype Embedded struct {\r\n        Embedded1 string\r\n        Embedded2 string\r\n}\r\n\r\nfunc (e Embedded) CodecEncodeSelf(encoder *codec.Encoder) {\r\n        // phon4y output, in a real example this would encode the struct properly\r\n        encoder.Encode(\"['phony']\")\r\n}\r\n\r\nfunc (e *Embedded) CodecDecodeSelf(decoder *codec.Decoder) {\r\n}\r\n\r\n// We use embedded in a different structure\r\ntype Foo struct {\r\n        Bar string\r\n        Embedded\r\n}\r\n\r\nfunc main() {\r\n        foo := Foo{\r\n                Bar: \"bar\",\r\n                Embedded: Embedded{\r\n                        Embedded1: \"embedded1\",\r\n                        Embedded2: \"embedded2\",\r\n                },\r\n        }\r\n\r\n        // The Decoder only encodes the Embedded struct\r\n        // and ignores Bar!\r\n        buffer := &bytes.Buffer{}\r\n        codec.NewEncoder(buffer, &codec.JsonHandle{}).Encode(foo)\r\n\r\n         fmt.Printf(\"Output: %s\\n\", buffer.String())\r\n}\r\n```\r\n\r\nThe program above outputs:\r\n\r\n```\r\nOutput: \"['phony']\"\r\n```\r\n\r\ncompletely ignoring the `Bar` element.\r\n\r\nI would expect something in the lines of:\r\n\r\n```\r\nOutput: { Bar: \"Bar\", Embedded: \"phony\" }\r\n```\r\n\r\nOr, with a properly defined selfer:\r\n\r\n```\r\nOutput: { Bar: \"Bar\", Embedded1: \"embedded1\", Embedded2: \"embedded2\"  }\r\n```\r\n\r\nI guess that, since the embedded Selfer makes the full struct a Selfer too, the encoder is blindly applying `(e Embedded) CodecEncodeSelf()`, ignoring other fields. I think the Encoder must somehow distinguish between the case in which the struct embeds a Selfer and when the struct itself implements a Selfer directly.\r\n\r\nI haven't tried it yet it but, as a workaround, I guess one can define a Selfer for Foo.\r\n\r\nI haven't tested the decoding, but maybe that's broken too."},"comment":{"url":"https://api.github.com/repos/ugorji/go/issues/comments/185538049","html_url":"https://github.com/ugorji/go/issues/141#issuecomment-185538049","issue_url":"https://api.github.com/repos/ugorji/go/issues/141","id":185538049,"user":{"login":"ugorji","id":1222082,"avatar_url":"https://avatars.githubusercontent.com/u/1222082?v=3","gravatar_id":"","url":"https://api.github.com/users/ugorji","html_url":"https://github.com/ugorji","followers_url":"https://api.github.com/users/ugorji/followers","following_url":"https://api.github.com/users/ugorji/following{/other_user}","gists_url":"https://api.github.com/users/ugorji/gists{/gist_id}","starred_url":"https://api.github.com/users/ugorji/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ugorji/subscriptions","organizations_url":"https://api.github.com/users/ugorji/orgs","repos_url":"https://api.github.com/users/ugorji/repos","events_url":"https://api.github.com/users/ugorji/events{/privacy}","received_events_url":"https://api.github.com/users/ugorji/received_events","type":"User","site_admin":false},"created_at":"2016-02-18T04:31:01Z","updated_at":"2016-02-18T04:31:01Z","body":"You are suggesting ways to workaround it, but i'm not willing to look at workarounds because the fundamental premise is broken i.e. enclosing types inherit the embedded methods, and there's no workaround for that. Anything we do will cause discordance.\r\n\r\nForget the inconvenience of the ordering, etc. The main design principle doesn't give us room to play.\r\n"}}