{"action":"created","comment":{"url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/comments/30358389","id":30358389,"diff_hunk":"@@ -0,0 +1,233 @@\n+package dynamodb\n+\n+import (\n+\t\"bytes\"\n+\t\"encoding/json\"\n+\t\"errors\"\n+\t\"fmt\"\n+\t\"reflect\"\n+\t\"runtime\"\n+\t\"strconv\"\n+)\n+\n+// Marshal accepts a map or struct converts it to the map[string]*AttributeValue\n+// type used to interact with the DynamoDB Item APIs.\n+//\n+// If in is a struct, we first JSON encode/decode it to get the data as a map.\n+// This can/should be optimized later.\n+func Marshal(in interface{}) (item map[string]*AttributeValue, err error) {\n+\tdefer func() {\n+\t\tif r := recover(); r != nil {\n+\t\t\tif e, ok := r.(runtime.Error); ok {\n+\t\t\t\terr = e\n+\t\t\t} else if s, ok := r.(string); ok {\n+\t\t\t\terr = errors.New(s)\n+\t\t\t} else {\n+\t\t\t\terr = r.(error)\n+\t\t\t}\n+\t\t\titem = nil\n+\t\t}\n+\t}()\n+\n+\tv := reflect.ValueOf(in)\n+\tswitch v.Kind() {\n+\tcase reflect.Struct:\n+\t\titem = marshalStruct(in)\n+\tcase reflect.Map:\n+\t\tif v.Type().Key().Kind() != reflect.String {\n+\t\t\treturn nil, errors.New(\"item must be a map[string]interface{} or struct (or a non-nil pointer to one), got \" + v.Type().String())\n+\t\t}\n+\t\titem = marshalMap(in)\n+\tcase reflect.Ptr:\n+\t\tif v.IsNil() {\n+\t\t\treturn nil, errors.New(\"item must not be nil\")\n+\t\t}\n+\t\treturn Marshal(v.Elem().Interface())\n+\tdefault:\n+\t\treturn nil, errors.New(\"item must be a map[string]interface{} or struct (or a non-nil pointer to one), got \" + v.Type().String())\n+\t}\n+\treturn item, nil\n+}\n+\n+// Unmarshal accepts the map[string]*AttributeValue type returned by the\n+// DynamoDB Item APIs and converts it to a map or struct.\n+//\n+// If v points to a struct, we first convert it to a basic map, then JSON\n+// encode/decode it to convert to a struct. This can/should be optimized later.\n+func Unmarshal(item map[string]*AttributeValue, v interface{}) (err error) {\n+\tdefer func() {\n+\t\tif r := recover(); r != nil {\n+\t\t\tif e, ok := r.(runtime.Error); ok {\n+\t\t\t\terr = e\n+\t\t\t} else if s, ok := r.(string); ok {\n+\t\t\t\terr = errors.New(s)\n+\t\t\t} else {\n+\t\t\t\terr = r.(error)\n+\t\t\t}\n+\t\t\titem = nil\n+\t\t}\n+\t}()\n+\n+\tm := make(map[string]interface{})\n+\tfor k, v := range item {\n+\t\tm[k] = unmarshal(v)\n+\t}\n+\n+\trv := reflect.ValueOf(v)\n+\tif rv.Kind() != reflect.Ptr || rv.IsNil() {\n+\t\treturn errors.New(\"v must be a non-nil pointer to a map[string]interface{} or struct, got \" + rv.Type().String())\n+\t}\n+\n+\tswitch rv.Elem().Kind() {","path":"service/dynamodb/marshal.go","position":81,"original_position":81,"commit_id":"ae28a1d093c8e1129f1c2cb6bf1b4e2b98215b9e","original_commit_id":"ae28a1d093c8e1129f1c2cb6bf1b4e2b98215b9e","user":{"login":"cbroglie","id":2257371,"avatar_url":"https://avatars.githubusercontent.com/u/2257371?v=3","gravatar_id":"","url":"https://api.github.com/users/cbroglie","html_url":"https://github.com/cbroglie","followers_url":"https://api.github.com/users/cbroglie/followers","following_url":"https://api.github.com/users/cbroglie/following{/other_user}","gists_url":"https://api.github.com/users/cbroglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cbroglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cbroglie/subscriptions","organizations_url":"https://api.github.com/users/cbroglie/orgs","repos_url":"https://api.github.com/users/cbroglie/repos","events_url":"https://api.github.com/users/cbroglie/events{/privacy}","received_events_url":"https://api.github.com/users/cbroglie/received_events","type":"User","site_admin":false},"body":"That's true. The json encoding has always made me feel a little icky, but I haven't spent time to remove it because it just works.\r\n\r\nAlso, I haven't benchmarked it, but one nice thing about using json to convert is that it caches the reflection results. Though I guess in practice this isn't likely to be a hot spot either way, the data types are usually pretty small.","created_at":"2015-05-14T20:19:15Z","updated_at":"2015-05-14T20:19:15Z","html_url":"https://github.com/awslabs/aws-sdk-go/pull/231#discussion_r30358389","pull_request_url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231","_links":{"self":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/comments/30358389"},"html":{"href":"https://github.com/awslabs/aws-sdk-go/pull/231#discussion_r30358389"},"pull_request":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231"}}},"pull_request":{"url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231","id":35248093,"html_url":"https://github.com/awslabs/aws-sdk-go/pull/231","diff_url":"https://github.com/awslabs/aws-sdk-go/pull/231.diff","patch_url":"https://github.com/awslabs/aws-sdk-go/pull/231.patch","issue_url":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/231","number":231,"state":"open","locked":false,"title":"Support marshaling structs to/from DynamoDB","user":{"login":"cbroglie","id":2257371,"avatar_url":"https://avatars.githubusercontent.com/u/2257371?v=3","gravatar_id":"","url":"https://api.github.com/users/cbroglie","html_url":"https://github.com/cbroglie","followers_url":"https://api.github.com/users/cbroglie/followers","following_url":"https://api.github.com/users/cbroglie/following{/other_user}","gists_url":"https://api.github.com/users/cbroglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cbroglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cbroglie/subscriptions","organizations_url":"https://api.github.com/users/cbroglie/orgs","repos_url":"https://api.github.com/users/cbroglie/repos","events_url":"https://api.github.com/users/cbroglie/events{/privacy}","received_events_url":"https://api.github.com/users/cbroglie/received_events","type":"User","site_admin":false},"body":"Fixes #72 ","created_at":"2015-05-12T17:02:29Z","updated_at":"2015-05-14T20:19:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a7a9ca9758f3d0e48ad7be5194fb0a8001892554","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231/commits","review_comments_url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231/comments","review_comment_url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/comments{/number}","comments_url":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/231/comments","statuses_url":"https://api.github.com/repos/awslabs/aws-sdk-go/statuses/ae28a1d093c8e1129f1c2cb6bf1b4e2b98215b9e","head":{"label":"cbroglie:dynamodb-marshaler","ref":"dynamodb-marshaler","sha":"ae28a1d093c8e1129f1c2cb6bf1b4e2b98215b9e","user":{"login":"cbroglie","id":2257371,"avatar_url":"https://avatars.githubusercontent.com/u/2257371?v=3","gravatar_id":"","url":"https://api.github.com/users/cbroglie","html_url":"https://github.com/cbroglie","followers_url":"https://api.github.com/users/cbroglie/followers","following_url":"https://api.github.com/users/cbroglie/following{/other_user}","gists_url":"https://api.github.com/users/cbroglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cbroglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cbroglie/subscriptions","organizations_url":"https://api.github.com/users/cbroglie/orgs","repos_url":"https://api.github.com/users/cbroglie/repos","events_url":"https://api.github.com/users/cbroglie/events{/privacy}","received_events_url":"https://api.github.com/users/cbroglie/received_events","type":"User","site_admin":false},"repo":{"id":30456222,"name":"aws-sdk-go","full_name":"cbroglie/aws-sdk-go","owner":{"login":"cbroglie","id":2257371,"avatar_url":"https://avatars.githubusercontent.com/u/2257371?v=3","gravatar_id":"","url":"https://api.github.com/users/cbroglie","html_url":"https://github.com/cbroglie","followers_url":"https://api.github.com/users/cbroglie/followers","following_url":"https://api.github.com/users/cbroglie/following{/other_user}","gists_url":"https://api.github.com/users/cbroglie/gists{/gist_id}","starred_url":"https://api.github.com/users/cbroglie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cbroglie/subscriptions","organizations_url":"https://api.github.com/users/cbroglie/orgs","repos_url":"https://api.github.com/users/cbroglie/repos","events_url":"https://api.github.com/users/cbroglie/events{/privacy}","received_events_url":"https://api.github.com/users/cbroglie/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cbroglie/aws-sdk-go","description":"An incredibly experimental, automatically generated set of AWS clients in Go.","fork":true,"url":"https://api.github.com/repos/cbroglie/aws-sdk-go","forks_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/forks","keys_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/teams","hooks_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/hooks","issue_events_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/issues/events{/number}","events_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/events","assignees_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/assignees{/user}","branches_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/branches{/branch}","tags_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/tags","blobs_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/statuses/{sha}","languages_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/languages","stargazers_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/stargazers","contributors_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/contributors","subscribers_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/subscribers","subscription_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/subscription","commits_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/contents/{+path}","compare_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/merges","archive_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/downloads","issues_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/issues{/number}","pulls_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/pulls{/number}","milestones_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/milestones{/number}","notifications_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/labels{/name}","releases_url":"https://api.github.com/repos/cbroglie/aws-sdk-go/releases{/id}","created_at":"2015-02-07T13:15:06Z","updated_at":"2015-02-07T13:15:07Z","pushed_at":"2015-05-12T17:01:47Z","git_url":"git://github.com/cbroglie/aws-sdk-go.git","ssh_url":"git@github.com:cbroglie/aws-sdk-go.git","clone_url":"https://github.com/cbroglie/aws-sdk-go.git","svn_url":"https://github.com/cbroglie/aws-sdk-go","homepage":null,"size":6143,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"awslabs:master","ref":"master","sha":"24d1eef417392c6e30f730b268719a320eec3075","user":{"login":"awslabs","id":3299148,"avatar_url":"https://avatars.githubusercontent.com/u/3299148?v=3","gravatar_id":"","url":"https://api.github.com/users/awslabs","html_url":"https://github.com/awslabs","followers_url":"https://api.github.com/users/awslabs/followers","following_url":"https://api.github.com/users/awslabs/following{/other_user}","gists_url":"https://api.github.com/users/awslabs/gists{/gist_id}","starred_url":"https://api.github.com/users/awslabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awslabs/subscriptions","organizations_url":"https://api.github.com/users/awslabs/orgs","repos_url":"https://api.github.com/users/awslabs/repos","events_url":"https://api.github.com/users/awslabs/events{/privacy}","received_events_url":"https://api.github.com/users/awslabs/received_events","type":"Organization","site_admin":false},"repo":{"id":27576132,"name":"aws-sdk-go","full_name":"awslabs/aws-sdk-go","owner":{"login":"awslabs","id":3299148,"avatar_url":"https://avatars.githubusercontent.com/u/3299148?v=3","gravatar_id":"","url":"https://api.github.com/users/awslabs","html_url":"https://github.com/awslabs","followers_url":"https://api.github.com/users/awslabs/followers","following_url":"https://api.github.com/users/awslabs/following{/other_user}","gists_url":"https://api.github.com/users/awslabs/gists{/gist_id}","starred_url":"https://api.github.com/users/awslabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awslabs/subscriptions","organizations_url":"https://api.github.com/users/awslabs/orgs","repos_url":"https://api.github.com/users/awslabs/repos","events_url":"https://api.github.com/users/awslabs/events{/privacy}","received_events_url":"https://api.github.com/users/awslabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/awslabs/aws-sdk-go","description":"AWS SDK for the Go programming language.","fork":false,"url":"https://api.github.com/repos/awslabs/aws-sdk-go","forks_url":"https://api.github.com/repos/awslabs/aws-sdk-go/forks","keys_url":"https://api.github.com/repos/awslabs/aws-sdk-go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/awslabs/aws-sdk-go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/awslabs/aws-sdk-go/teams","hooks_url":"https://api.github.com/repos/awslabs/aws-sdk-go/hooks","issue_events_url":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/events{/number}","events_url":"https://api.github.com/repos/awslabs/aws-sdk-go/events","assignees_url":"https://api.github.com/repos/awslabs/aws-sdk-go/assignees{/user}","branches_url":"https://api.github.com/repos/awslabs/aws-sdk-go/branches{/branch}","tags_url":"https://api.github.com/repos/awslabs/aws-sdk-go/tags","blobs_url":"https://api.github.com/repos/awslabs/aws-sdk-go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/awslabs/aws-sdk-go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/awslabs/aws-sdk-go/git/refs{/sha}","trees_url":"https://api.github.com/repos/awslabs/aws-sdk-go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/awslabs/aws-sdk-go/statuses/{sha}","languages_url":"https://api.github.com/repos/awslabs/aws-sdk-go/languages","stargazers_url":"https://api.github.com/repos/awslabs/aws-sdk-go/stargazers","contributors_url":"https://api.github.com/repos/awslabs/aws-sdk-go/contributors","subscribers_url":"https://api.github.com/repos/awslabs/aws-sdk-go/subscribers","subscription_url":"https://api.github.com/repos/awslabs/aws-sdk-go/subscription","commits_url":"https://api.github.com/repos/awslabs/aws-sdk-go/commits{/sha}","git_commits_url":"https://api.github.com/repos/awslabs/aws-sdk-go/git/commits{/sha}","comments_url":"https://api.github.com/repos/awslabs/aws-sdk-go/comments{/number}","issue_comment_url":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/comments{/number}","contents_url":"https://api.github.com/repos/awslabs/aws-sdk-go/contents/{+path}","compare_url":"https://api.github.com/repos/awslabs/aws-sdk-go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/awslabs/aws-sdk-go/merges","archive_url":"https://api.github.com/repos/awslabs/aws-sdk-go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/awslabs/aws-sdk-go/downloads","issues_url":"https://api.github.com/repos/awslabs/aws-sdk-go/issues{/number}","pulls_url":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls{/number}","milestones_url":"https://api.github.com/repos/awslabs/aws-sdk-go/milestones{/number}","notifications_url":"https://api.github.com/repos/awslabs/aws-sdk-go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/awslabs/aws-sdk-go/labels{/name}","releases_url":"https://api.github.com/repos/awslabs/aws-sdk-go/releases{/id}","created_at":"2014-12-05T05:29:41Z","updated_at":"2015-05-13T07:12:16Z","pushed_at":"2015-05-14T18:07:32Z","git_url":"git://github.com/awslabs/aws-sdk-go.git","ssh_url":"git@github.com:awslabs/aws-sdk-go.git","clone_url":"https://github.com/awslabs/aws-sdk-go.git","svn_url":"https://github.com/awslabs/aws-sdk-go","homepage":"","size":10237,"stargazers_count":1531,"watchers_count":1531,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":131,"mirror_url":null,"open_issues_count":31,"forks":131,"open_issues":31,"watchers":1531,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231"},"html":{"href":"https://github.com/awslabs/aws-sdk-go/pull/231"},"issue":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/231"},"comments":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/issues/231/comments"},"review_comments":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231/comments"},"review_comment":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/pulls/231/commits"},"statuses":{"href":"https://api.github.com/repos/awslabs/aws-sdk-go/statuses/ae28a1d093c8e1129f1c2cb6bf1b4e2b98215b9e"}}}}