{"action":"opened","number":194,"pull_request":{"url":"https://api.github.com/repos/tinylib/msgp/pulls/194","id":137488081,"html_url":"https://github.com/tinylib/msgp/pull/194","diff_url":"https://github.com/tinylib/msgp/pull/194.diff","patch_url":"https://github.com/tinylib/msgp/pull/194.patch","issue_url":"https://api.github.com/repos/tinylib/msgp/issues/194","number":194,"state":"open","locked":false,"title":"Implement polymorphism via msgp:intercept directive","user":{"login":"shabbyrobe","id":288426,"avatar_url":"https://avatars3.githubusercontent.com/u/288426?v=4","gravatar_id":"","url":"https://api.github.com/users/shabbyrobe","html_url":"https://github.com/shabbyrobe","followers_url":"https://api.github.com/users/shabbyrobe/followers","following_url":"https://api.github.com/users/shabbyrobe/following{/other_user}","gists_url":"https://api.github.com/users/shabbyrobe/gists{/gist_id}","starred_url":"https://api.github.com/users/shabbyrobe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shabbyrobe/subscriptions","organizations_url":"https://api.github.com/users/shabbyrobe/orgs","repos_url":"https://api.github.com/users/shabbyrobe/repos","events_url":"https://api.github.com/users/shabbyrobe/events{/privacy}","received_events_url":"https://api.github.com/users/shabbyrobe/received_events","type":"User","site_admin":false},"body":"Here's an attempt at #184. I have had about 4 or 5 goes at this now, this is the only way I've found that went in even remotely cleanly. I tried doing it with extensions, then found the warning against that in #35. I tried extending ShimMode at least twice, but that was a total mess. This is the first time I've tried something that didn't feel like I was committing an act of vandalism!\r\n\r\nUsing this, we can delegate to a function to provide a stand-in that can decode, encode, marshal and unmarshal instead of calling it on the type itself. Interface types can be supported, as well as typed primitives.\r\n\r\nThere's no interface type for the mapper so we can retain type safety. You can just implement as much or as little as the code generator requires, based on what you ask it to generate. \r\n\r\n```go\r\n//msgp:intercept MyIntf using:DefaultMyIntfMapper\r\n\r\ntype Thingy struct {\r\n    Foo MyIntf\r\n}\r\n\r\ntype MyIntf interface {}\r\n\r\nfunc DefaultMyIntfMapper() myIntfMapper { return &myIntfMapper{} }\r\n\r\ntype myIntfMapper struct {}\r\n\r\nfunc (p *myIntfMapper) DecodeMsg(dc *msgp.Reader) (t MyIntf, err error) {}\r\nfunc (p *myIntfMapper) UnmarshalMsg(bts []byte) (t MyIntf, o []byte, err error) {}\r\nfunc (p *myIntfMapper) EncodeMsg(t MyIntf, en *msgp.Writer) (err error) {}\r\nfunc (p *myIntfMapper) MarshalMsg(t MyIntf, b []byte) (o []byte, err error) {}\r\nfunc (p *myIntfMapper) Msgsize(t MyIntf) (s int) {}\r\n```\r\n\r\nThe use is demonstrated in the `_generated/intercept_defs.go` and `_generated/intercept_test.go` files. I know I haven't got a complete set of tests here, but I need to think of more scenarios. I just thought I'd open this for discussion before I go test-case crazy just in case this approach is not one that would be acceptable.\r\n\r\nI added a dummy element for interfaces that gets skipped by the printer but that was only to quiet the warning that gets raised: `intercept_defs.go: TestUsesIntfStructProvided: Foo: non-local identifier: TestIntfStructProvided`, if there's a better way to sort that out that involves fewer changes to the Elem hierarchy I'd be grateful for a tip, I didn't see one.\r\n\r\nI know it looks like a big patch, but the overwhelming majority of it is tests!","created_at":"2017-08-24T15:48:46Z","updated_at":"2017-08-24T15:48:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/tinylib/msgp/pulls/194/commits","review_comments_url":"https://api.github.com/repos/tinylib/msgp/pulls/194/comments","review_comment_url":"https://api.github.com/repos/tinylib/msgp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tinylib/msgp/issues/194/comments","statuses_url":"https://api.github.com/repos/tinylib/msgp/statuses/80d9823a21fb6811aac0c87698e02f9b8e7ea25a","head":{"label":"shabbyrobe:feature-polymorphism","ref":"feature-polymorphism","sha":"80d9823a21fb6811aac0c87698e02f9b8e7ea25a","user":{"login":"shabbyrobe","id":288426,"avatar_url":"https://avatars3.githubusercontent.com/u/288426?v=4","gravatar_id":"","url":"https://api.github.com/users/shabbyrobe","html_url":"https://github.com/shabbyrobe","followers_url":"https://api.github.com/users/shabbyrobe/followers","following_url":"https://api.github.com/users/shabbyrobe/following{/other_user}","gists_url":"https://api.github.com/users/shabbyrobe/gists{/gist_id}","starred_url":"https://api.github.com/users/shabbyrobe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shabbyrobe/subscriptions","organizations_url":"https://api.github.com/users/shabbyrobe/orgs","repos_url":"https://api.github.com/users/shabbyrobe/repos","events_url":"https://api.github.com/users/shabbyrobe/events{/privacy}","received_events_url":"https://api.github.com/users/shabbyrobe/received_events","type":"User","site_admin":false},"repo":{"id":92714881,"name":"msgp","full_name":"shabbyrobe/msgp","owner":{"login":"shabbyrobe","id":288426,"avatar_url":"https://avatars3.githubusercontent.com/u/288426?v=4","gravatar_id":"","url":"https://api.github.com/users/shabbyrobe","html_url":"https://github.com/shabbyrobe","followers_url":"https://api.github.com/users/shabbyrobe/followers","following_url":"https://api.github.com/users/shabbyrobe/following{/other_user}","gists_url":"https://api.github.com/users/shabbyrobe/gists{/gist_id}","starred_url":"https://api.github.com/users/shabbyrobe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shabbyrobe/subscriptions","organizations_url":"https://api.github.com/users/shabbyrobe/orgs","repos_url":"https://api.github.com/users/shabbyrobe/repos","events_url":"https://api.github.com/users/shabbyrobe/events{/privacy}","received_events_url":"https://api.github.com/users/shabbyrobe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shabbyrobe/msgp","description":"A Go code generator for MessagePack / msgpack.org[Go]","fork":true,"url":"https://api.github.com/repos/shabbyrobe/msgp","forks_url":"https://api.github.com/repos/shabbyrobe/msgp/forks","keys_url":"https://api.github.com/repos/shabbyrobe/msgp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shabbyrobe/msgp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shabbyrobe/msgp/teams","hooks_url":"https://api.github.com/repos/shabbyrobe/msgp/hooks","issue_events_url":"https://api.github.com/repos/shabbyrobe/msgp/issues/events{/number}","events_url":"https://api.github.com/repos/shabbyrobe/msgp/events","assignees_url":"https://api.github.com/repos/shabbyrobe/msgp/assignees{/user}","branches_url":"https://api.github.com/repos/shabbyrobe/msgp/branches{/branch}","tags_url":"https://api.github.com/repos/shabbyrobe/msgp/tags","blobs_url":"https://api.github.com/repos/shabbyrobe/msgp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shabbyrobe/msgp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shabbyrobe/msgp/git/refs{/sha}","trees_url":"https://api.github.com/repos/shabbyrobe/msgp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shabbyrobe/msgp/statuses/{sha}","languages_url":"https://api.github.com/repos/shabbyrobe/msgp/languages","stargazers_url":"https://api.github.com/repos/shabbyrobe/msgp/stargazers","contributors_url":"https://api.github.com/repos/shabbyrobe/msgp/contributors","subscribers_url":"https://api.github.com/repos/shabbyrobe/msgp/subscribers","subscription_url":"https://api.github.com/repos/shabbyrobe/msgp/subscription","commits_url":"https://api.github.com/repos/shabbyrobe/msgp/commits{/sha}","git_commits_url":"https://api.github.com/repos/shabbyrobe/msgp/git/commits{/sha}","comments_url":"https://api.github.com/repos/shabbyrobe/msgp/comments{/number}","issue_comment_url":"https://api.github.com/repos/shabbyrobe/msgp/issues/comments{/number}","contents_url":"https://api.github.com/repos/shabbyrobe/msgp/contents/{+path}","compare_url":"https://api.github.com/repos/shabbyrobe/msgp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shabbyrobe/msgp/merges","archive_url":"https://api.github.com/repos/shabbyrobe/msgp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shabbyrobe/msgp/downloads","issues_url":"https://api.github.com/repos/shabbyrobe/msgp/issues{/number}","pulls_url":"https://api.github.com/repos/shabbyrobe/msgp/pulls{/number}","milestones_url":"https://api.github.com/repos/shabbyrobe/msgp/milestones{/number}","notifications_url":"https://api.github.com/repos/shabbyrobe/msgp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shabbyrobe/msgp/labels{/name}","releases_url":"https://api.github.com/repos/shabbyrobe/msgp/releases{/id}","deployments_url":"https://api.github.com/repos/shabbyrobe/msgp/deployments","created_at":"2017-05-29T07:02:37Z","updated_at":"2017-05-29T07:02:39Z","pushed_at":"2017-08-24T15:39:52Z","git_url":"git://github.com/shabbyrobe/msgp.git","ssh_url":"git@github.com:shabbyrobe/msgp.git","clone_url":"https://github.com/shabbyrobe/msgp.git","svn_url":"https://github.com/shabbyrobe/msgp","homepage":"","size":8919,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tinylib:master","ref":"master","sha":"bfa68fe30b555b40d68871606ee725537f8023a1","user":{"login":"tinylib","id":10499993,"avatar_url":"https://avatars2.githubusercontent.com/u/10499993?v=4","gravatar_id":"","url":"https://api.github.com/users/tinylib","html_url":"https://github.com/tinylib","followers_url":"https://api.github.com/users/tinylib/followers","following_url":"https://api.github.com/users/tinylib/following{/other_user}","gists_url":"https://api.github.com/users/tinylib/gists{/gist_id}","starred_url":"https://api.github.com/users/tinylib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tinylib/subscriptions","organizations_url":"https://api.github.com/users/tinylib/orgs","repos_url":"https://api.github.com/users/tinylib/repos","events_url":"https://api.github.com/users/tinylib/events{/privacy}","received_events_url":"https://api.github.com/users/tinylib/received_events","type":"Organization","site_admin":false},"repo":{"id":23944963,"name":"msgp","full_name":"tinylib/msgp","owner":{"login":"tinylib","id":10499993,"avatar_url":"https://avatars2.githubusercontent.com/u/10499993?v=4","gravatar_id":"","url":"https://api.github.com/users/tinylib","html_url":"https://github.com/tinylib","followers_url":"https://api.github.com/users/tinylib/followers","following_url":"https://api.github.com/users/tinylib/following{/other_user}","gists_url":"https://api.github.com/users/tinylib/gists{/gist_id}","starred_url":"https://api.github.com/users/tinylib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tinylib/subscriptions","organizations_url":"https://api.github.com/users/tinylib/orgs","repos_url":"https://api.github.com/users/tinylib/repos","events_url":"https://api.github.com/users/tinylib/events{/privacy}","received_events_url":"https://api.github.com/users/tinylib/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tinylib/msgp","description":"A Go code generator for MessagePack / msgpack.org[Go]","fork":false,"url":"https://api.github.com/repos/tinylib/msgp","forks_url":"https://api.github.com/repos/tinylib/msgp/forks","keys_url":"https://api.github.com/repos/tinylib/msgp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tinylib/msgp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tinylib/msgp/teams","hooks_url":"https://api.github.com/repos/tinylib/msgp/hooks","issue_events_url":"https://api.github.com/repos/tinylib/msgp/issues/events{/number}","events_url":"https://api.github.com/repos/tinylib/msgp/events","assignees_url":"https://api.github.com/repos/tinylib/msgp/assignees{/user}","branches_url":"https://api.github.com/repos/tinylib/msgp/branches{/branch}","tags_url":"https://api.github.com/repos/tinylib/msgp/tags","blobs_url":"https://api.github.com/repos/tinylib/msgp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tinylib/msgp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tinylib/msgp/git/refs{/sha}","trees_url":"https://api.github.com/repos/tinylib/msgp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tinylib/msgp/statuses/{sha}","languages_url":"https://api.github.com/repos/tinylib/msgp/languages","stargazers_url":"https://api.github.com/repos/tinylib/msgp/stargazers","contributors_url":"https://api.github.com/repos/tinylib/msgp/contributors","subscribers_url":"https://api.github.com/repos/tinylib/msgp/subscribers","subscription_url":"https://api.github.com/repos/tinylib/msgp/subscription","commits_url":"https://api.github.com/repos/tinylib/msgp/commits{/sha}","git_commits_url":"https://api.github.com/repos/tinylib/msgp/git/commits{/sha}","comments_url":"https://api.github.com/repos/tinylib/msgp/comments{/number}","issue_comment_url":"https://api.github.com/repos/tinylib/msgp/issues/comments{/number}","contents_url":"https://api.github.com/repos/tinylib/msgp/contents/{+path}","compare_url":"https://api.github.com/repos/tinylib/msgp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tinylib/msgp/merges","archive_url":"https://api.github.com/repos/tinylib/msgp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tinylib/msgp/downloads","issues_url":"https://api.github.com/repos/tinylib/msgp/issues{/number}","pulls_url":"https://api.github.com/repos/tinylib/msgp/pulls{/number}","milestones_url":"https://api.github.com/repos/tinylib/msgp/milestones{/number}","notifications_url":"https://api.github.com/repos/tinylib/msgp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tinylib/msgp/labels{/name}","releases_url":"https://api.github.com/repos/tinylib/msgp/releases{/id}","deployments_url":"https://api.github.com/repos/tinylib/msgp/deployments","created_at":"2014-09-12T03:09:53Z","updated_at":"2017-08-24T08:37:31Z","pushed_at":"2017-08-24T15:39:57Z","git_url":"git://github.com/tinylib/msgp.git","ssh_url":"git@github.com:tinylib/msgp.git","clone_url":"https://github.com/tinylib/msgp.git","svn_url":"https://github.com/tinylib/msgp","homepage":"","size":9877,"stargazers_count":671,"watchers_count":671,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":63,"mirror_url":null,"open_issues_count":36,"forks":63,"open_issues":36,"watchers":671,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tinylib/msgp/pulls/194"},"html":{"href":"https://github.com/tinylib/msgp/pull/194"},"issue":{"href":"https://api.github.com/repos/tinylib/msgp/issues/194"},"comments":{"href":"https://api.github.com/repos/tinylib/msgp/issues/194/comments"},"review_comments":{"href":"https://api.github.com/repos/tinylib/msgp/pulls/194/comments"},"review_comment":{"href":"https://api.github.com/repos/tinylib/msgp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tinylib/msgp/pulls/194/commits"},"statuses":{"href":"https://api.github.com/repos/tinylib/msgp/statuses/80d9823a21fb6811aac0c87698e02f9b8e7ea25a"}},"merged":false,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"maintainer_can_modify":true,"commits":2,"additions":641,"deletions":30,"changed_files":13}}
{"actor":{"display_login":"shabbyrobe"}}