{"action":"created","comment":{"url":"https://api.github.com/repos/juju/environschema/pulls/comments/38199065","id":38199065,"diff_hunk":"@@ -0,0 +1,215 @@\n+// Copyright 2015 Canonical Ltd.\n+// Licensed under the LGPLv3, see LICENCE file for details.\n+\n+package environschema\n+\n+import (\n+\t\"bytes\"\n+\t\"fmt\"\n+\t\"go/doc\"\n+\t\"io\"\n+\t\"reflect\"\n+\t\"sort\"\n+\t\"strings\"\n+\t\"unicode\"\n+\n+\t\"gopkg.in/yaml.v2\"\n+)\n+\n+// SampleYAML writes YAML output to w, indented by indent spaces\n+// that holds the attributes in attrs with descriptions found\n+// in the given fields. An entry for any attribute in fields not\n+// in attrs will be generated but commented out.\n+func SampleYAML(w io.Writer, indent int, attrs map[string]interface{}, fields Fields) error {\n+\tindentStr := strings.Repeat(\" \", indent)\n+\torderedFields := make(fieldsByGroup, 0, len(fields))\n+\tfor name, f := range fields {\n+\t\torderedFields = append(orderedFields, attrWithName{\n+\t\t\tname: name,\n+\t\t\tAttr: f,\n+\t\t})\n+\t}\n+\tsort.Sort(orderedFields)\n+\tfor i, f := range orderedFields {\n+\t\tif i > 0 {\n+\t\t\tfmt.Fprintf(w, \"\\n\")\n+\t\t}\n+\t\twriteSampleDescription(w, f.Attr, indentStr+\"# \")\n+\t\tval, ok := attrs[f.name]\n+\t\tif ok {\n+\t\t\tfmt.Fprintf(w, \"%s:\", f.name)\n+\t\t\tindentVal(w, val, indentStr)\n+\t\t} else {\n+\t\t\tif f.Example != nil {\n+\t\t\t\tval = f.Example\n+\t\t\t} else {\n+\t\t\t\tval = sampleValue(f.Type)\n+\t\t\t}\n+\t\t\tfmt.Fprintf(w, \"# %s:\", f.name)\n+\t\t\tindentVal(w, val, indentStr+\"# \")\n+\t\t}\n+\t}\n+\treturn nil\n+}\n+\n+const textWidth = 80\n+\n+func writeSampleDescription(w io.Writer, f Attr, indent string) {\n+\tpreviousText := false\n+\n+\t// section marks the start of a new section of the comment;\n+\t// sections are separated with empty lines.\n+\tsection := func() {\n+\t\tif previousText {\n+\t\t\tfmt.Fprintf(w, \"%s\\n\", strings.TrimRightFunc(indent, unicode.IsSpace))\n+\t\t}\n+\t\tpreviousText = true\n+\t}\n+\n+\tdescr := strings.TrimSpace(f.Description)\n+\tif descr != \"\" {\n+\t\tsection()\n+\t\tif !strings.HasSuffix(descr, \".\") {\n+\t\t\tdescr += \".\"\n+\t\t}\n+\t\tdoc.ToText(w, descr, indent, \"    \", textWidth-len(indent))\n+\t}\n+\tvars := make([]string, 0, len(f.EnvVars)+1)\n+\tif f.EnvVar != \"\" {\n+\t\tvars = append(vars, \"$\"+f.EnvVar)\n+\t}\n+\tfor _, v := range f.EnvVars {\n+\t\tvars = append(vars, \"$\"+v)\n+\t}\n+\tif len(vars) > 0 {\n+\t\tsection()\n+\t\tfmt.Fprintf(w, \"%sDefault value taken from %s.\\n\", indent, wordyList(vars))\n+\t}\n+\tattrText := \"\"\n+\tswitch {\n+\tcase f.Secret && f.Immutable:\n+\t\tattrText = \"immutable and considered secret\"\n+\tcase f.Secret:\n+\t\tattrText = \"considered secret\"\n+\tcase f.Immutable:\n+\t\tattrText = \"immutable\"\n+\t}\n+\tif attrText != \"\" {\n+\t\tsection()\n+\t\tfmt.Fprintf(w, \"%sThis attribute is %s.\\n\", indent, attrText)\n+\t}\n+\tsection()\n+}\n+\n+func emptyLine(w io.Writer, indent string) {\n+\tfmt.Fprintf(w, \"%s\\n\", strings.TrimRightFunc(indent, unicode.IsSpace))\n+}\n+\n+func wordyList(words []string) string {\n+\tif len(words) == 0 {\n+\t\treturn \"\"\n+\t}\n+\tif len(words) == 1 {\n+\t\treturn words[0]\n+\t}\n+\treturn strings.Join(words[0:len(words)-1], \", \") + \" or \" + words[len(words)-1]\n+}\n+\n+var groupPriority = map[Group]int{\n+\tProviderGroup: 3,\n+\tAccountGroup:  2,\n+\tEnvironGroup:  1,\n+}\n+\n+type attrWithName struct {\n+\tname string\n+\tAttr\n+}\n+\n+type fieldsByGroup []attrWithName\n+\n+func (f fieldsByGroup) Len() int {\n+\treturn len(f)\n+}\n+\n+func (f fieldsByGroup) Swap(i, j int) {\n+\tf[i], f[j] = f[j], f[i]\n+}\n+\n+func (f fieldsByGroup) Less(i0, i1 int) bool {\n+\tf0, f1 := &f[i0], &f[i1]\n+\tpri0, pri1 := groupPriority[f0.Group], groupPriority[f1.Group]\n+\tif pri0 != pri1 {\n+\t\treturn pri0 > pri1\n+\t}\n+\treturn f0.name < f1.name\n+}\n+\n+var (\n+\tspace = []byte(\" \")\n+\tnl    = []byte(\"\\n\")\n+)\n+\n+// indentVal writes the given YAML-formatted value x to w and prefixing\n+// the second and subsequent lines with the given ident.\n+func indentVal(w io.Writer, x interface{}, indentStr string) {\n+\tdata, err := yaml.Marshal(x)\n+\tif err != nil {\n+\t\tpanic(fmt.Errorf(\"cannot marshal YAML\", err))\n+\t}\n+\tif len(data) == 0 {\n+\t\tpanic(\"YAML cannot marshal to empty string\")\n+\t}\n+\tindent := []byte(indentStr + \"  \")\n+\tif canUseSameLine(x) {\n+\t\tw.Write(space)\n+\t} else {\n+\t\tw.Write(nl)","path":"sample.go","position":167,"original_position":167,"commit_id":"a9820aebfdf419910b97e847503b46c140405f31","original_commit_id":"a9820aebfdf419910b97e847503b46c140405f31","user":{"login":"mhilton","id":1136018,"avatar_url":"https://avatars.githubusercontent.com/u/1136018?v=3","gravatar_id":"","url":"https://api.github.com/users/mhilton","html_url":"https://github.com/mhilton","followers_url":"https://api.github.com/users/mhilton/followers","following_url":"https://api.github.com/users/mhilton/following{/other_user}","gists_url":"https://api.github.com/users/mhilton/gists{/gist_id}","starred_url":"https://api.github.com/users/mhilton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhilton/subscriptions","organizations_url":"https://api.github.com/users/mhilton/orgs","repos_url":"https://api.github.com/users/mhilton/repos","events_url":"https://api.github.com/users/mhilton/events{/privacy}","received_events_url":"https://api.github.com/users/mhilton/received_events","type":"User","site_admin":false},"body":"previously you've used fmt.Fprintf(w, \"\\n\") to do the same thing. I'm not following why this one should be different.","created_at":"2015-08-28T13:30:16Z","updated_at":"2015-08-28T13:30:16Z","html_url":"https://github.com/juju/environschema/pull/6#discussion_r38199065","pull_request_url":"https://api.github.com/repos/juju/environschema/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/juju/environschema/pulls/comments/38199065"},"html":{"href":"https://github.com/juju/environschema/pull/6#discussion_r38199065"},"pull_request":{"href":"https://api.github.com/repos/juju/environschema/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/juju/environschema/pulls/6","id":43625446,"html_url":"https://github.com/juju/environschema/pull/6","diff_url":"https://github.com/juju/environschema/pull/6.diff","patch_url":"https://github.com/juju/environschema/pull/6.patch","issue_url":"https://api.github.com/repos/juju/environschema/issues/6","number":6,"state":"open","locked":false,"title":"add SampleYAML function","user":{"login":"rogpeppe","id":66491,"avatar_url":"https://avatars.githubusercontent.com/u/66491?v=3","gravatar_id":"","url":"https://api.github.com/users/rogpeppe","html_url":"https://github.com/rogpeppe","followers_url":"https://api.github.com/users/rogpeppe/followers","following_url":"https://api.github.com/users/rogpeppe/following{/other_user}","gists_url":"https://api.github.com/users/rogpeppe/gists{/gist_id}","starred_url":"https://api.github.com/users/rogpeppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogpeppe/subscriptions","organizations_url":"https://api.github.com/users/rogpeppe/orgs","repos_url":"https://api.github.com/users/rogpeppe/repos","events_url":"https://api.github.com/users/rogpeppe/events{/privacy}","received_events_url":"https://api.github.com/users/rogpeppe/received_events","type":"User","site_admin":false},"body":"This will enable us to generate environment provider boilerplate\r\nYAML files.","created_at":"2015-08-28T13:03:23Z","updated_at":"2015-08-28T13:30:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"32f15fa67495002a16050f538722331edfa30a3b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/juju/environschema/pulls/6/commits","review_comments_url":"https://api.github.com/repos/juju/environschema/pulls/6/comments","review_comment_url":"https://api.github.com/repos/juju/environschema/pulls/comments{/number}","comments_url":"https://api.github.com/repos/juju/environschema/issues/6/comments","statuses_url":"https://api.github.com/repos/juju/environschema/statuses/a9820aebfdf419910b97e847503b46c140405f31","head":{"label":"rogpeppe:007-sample-yaml","ref":"007-sample-yaml","sha":"a9820aebfdf419910b97e847503b46c140405f31","user":{"login":"rogpeppe","id":66491,"avatar_url":"https://avatars.githubusercontent.com/u/66491?v=3","gravatar_id":"","url":"https://api.github.com/users/rogpeppe","html_url":"https://github.com/rogpeppe","followers_url":"https://api.github.com/users/rogpeppe/followers","following_url":"https://api.github.com/users/rogpeppe/following{/other_user}","gists_url":"https://api.github.com/users/rogpeppe/gists{/gist_id}","starred_url":"https://api.github.com/users/rogpeppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogpeppe/subscriptions","organizations_url":"https://api.github.com/users/rogpeppe/orgs","repos_url":"https://api.github.com/users/rogpeppe/repos","events_url":"https://api.github.com/users/rogpeppe/events{/privacy}","received_events_url":"https://api.github.com/users/rogpeppe/received_events","type":"User","site_admin":false},"repo":{"id":36725125,"name":"environschema","full_name":"rogpeppe/environschema","owner":{"login":"rogpeppe","id":66491,"avatar_url":"https://avatars.githubusercontent.com/u/66491?v=3","gravatar_id":"","url":"https://api.github.com/users/rogpeppe","html_url":"https://github.com/rogpeppe","followers_url":"https://api.github.com/users/rogpeppe/followers","following_url":"https://api.github.com/users/rogpeppe/following{/other_user}","gists_url":"https://api.github.com/users/rogpeppe/gists{/gist_id}","starred_url":"https://api.github.com/users/rogpeppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogpeppe/subscriptions","organizations_url":"https://api.github.com/users/rogpeppe/orgs","repos_url":"https://api.github.com/users/rogpeppe/repos","events_url":"https://api.github.com/users/rogpeppe/events{/privacy}","received_events_url":"https://api.github.com/users/rogpeppe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rogpeppe/environschema","description":"schema descriptions for Juju environment configurations","fork":true,"url":"https://api.github.com/repos/rogpeppe/environschema","forks_url":"https://api.github.com/repos/rogpeppe/environschema/forks","keys_url":"https://api.github.com/repos/rogpeppe/environschema/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rogpeppe/environschema/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rogpeppe/environschema/teams","hooks_url":"https://api.github.com/repos/rogpeppe/environschema/hooks","issue_events_url":"https://api.github.com/repos/rogpeppe/environschema/issues/events{/number}","events_url":"https://api.github.com/repos/rogpeppe/environschema/events","assignees_url":"https://api.github.com/repos/rogpeppe/environschema/assignees{/user}","branches_url":"https://api.github.com/repos/rogpeppe/environschema/branches{/branch}","tags_url":"https://api.github.com/repos/rogpeppe/environschema/tags","blobs_url":"https://api.github.com/repos/rogpeppe/environschema/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rogpeppe/environschema/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rogpeppe/environschema/git/refs{/sha}","trees_url":"https://api.github.com/repos/rogpeppe/environschema/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rogpeppe/environschema/statuses/{sha}","languages_url":"https://api.github.com/repos/rogpeppe/environschema/languages","stargazers_url":"https://api.github.com/repos/rogpeppe/environschema/stargazers","contributors_url":"https://api.github.com/repos/rogpeppe/environschema/contributors","subscribers_url":"https://api.github.com/repos/rogpeppe/environschema/subscribers","subscription_url":"https://api.github.com/repos/rogpeppe/environschema/subscription","commits_url":"https://api.github.com/repos/rogpeppe/environschema/commits{/sha}","git_commits_url":"https://api.github.com/repos/rogpeppe/environschema/git/commits{/sha}","comments_url":"https://api.github.com/repos/rogpeppe/environschema/comments{/number}","issue_comment_url":"https://api.github.com/repos/rogpeppe/environschema/issues/comments{/number}","contents_url":"https://api.github.com/repos/rogpeppe/environschema/contents/{+path}","compare_url":"https://api.github.com/repos/rogpeppe/environschema/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rogpeppe/environschema/merges","archive_url":"https://api.github.com/repos/rogpeppe/environschema/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rogpeppe/environschema/downloads","issues_url":"https://api.github.com/repos/rogpeppe/environschema/issues{/number}","pulls_url":"https://api.github.com/repos/rogpeppe/environschema/pulls{/number}","milestones_url":"https://api.github.com/repos/rogpeppe/environschema/milestones{/number}","notifications_url":"https://api.github.com/repos/rogpeppe/environschema/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rogpeppe/environschema/labels{/name}","releases_url":"https://api.github.com/repos/rogpeppe/environschema/releases{/id}","created_at":"2015-06-02T10:14:25Z","updated_at":"2015-06-02T10:10:40Z","pushed_at":"2015-08-28T13:02:12Z","git_url":"git://github.com/rogpeppe/environschema.git","ssh_url":"git@github.com:rogpeppe/environschema.git","clone_url":"https://github.com/rogpeppe/environschema.git","svn_url":"https://github.com/rogpeppe/environschema","homepage":null,"size":116,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"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":"v1"}},"base":{"label":"juju:v1","ref":"v1","sha":"16cc59268c09c22870cb4de8eb6248652535f315","user":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"repo":{"id":36724935,"name":"environschema","full_name":"juju/environschema","owner":{"login":"juju","id":4604548,"avatar_url":"https://avatars.githubusercontent.com/u/4604548?v=3","gravatar_id":"","url":"https://api.github.com/users/juju","html_url":"https://github.com/juju","followers_url":"https://api.github.com/users/juju/followers","following_url":"https://api.github.com/users/juju/following{/other_user}","gists_url":"https://api.github.com/users/juju/gists{/gist_id}","starred_url":"https://api.github.com/users/juju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juju/subscriptions","organizations_url":"https://api.github.com/users/juju/orgs","repos_url":"https://api.github.com/users/juju/repos","events_url":"https://api.github.com/users/juju/events{/privacy}","received_events_url":"https://api.github.com/users/juju/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/juju/environschema","description":"schema descriptions for Juju environment configurations","fork":false,"url":"https://api.github.com/repos/juju/environschema","forks_url":"https://api.github.com/repos/juju/environschema/forks","keys_url":"https://api.github.com/repos/juju/environschema/keys{/key_id}","collaborators_url":"https://api.github.com/repos/juju/environschema/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/juju/environschema/teams","hooks_url":"https://api.github.com/repos/juju/environschema/hooks","issue_events_url":"https://api.github.com/repos/juju/environschema/issues/events{/number}","events_url":"https://api.github.com/repos/juju/environschema/events","assignees_url":"https://api.github.com/repos/juju/environschema/assignees{/user}","branches_url":"https://api.github.com/repos/juju/environschema/branches{/branch}","tags_url":"https://api.github.com/repos/juju/environschema/tags","blobs_url":"https://api.github.com/repos/juju/environschema/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/juju/environschema/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/juju/environschema/git/refs{/sha}","trees_url":"https://api.github.com/repos/juju/environschema/git/trees{/sha}","statuses_url":"https://api.github.com/repos/juju/environschema/statuses/{sha}","languages_url":"https://api.github.com/repos/juju/environschema/languages","stargazers_url":"https://api.github.com/repos/juju/environschema/stargazers","contributors_url":"https://api.github.com/repos/juju/environschema/contributors","subscribers_url":"https://api.github.com/repos/juju/environschema/subscribers","subscription_url":"https://api.github.com/repos/juju/environschema/subscription","commits_url":"https://api.github.com/repos/juju/environschema/commits{/sha}","git_commits_url":"https://api.github.com/repos/juju/environschema/git/commits{/sha}","comments_url":"https://api.github.com/repos/juju/environschema/comments{/number}","issue_comment_url":"https://api.github.com/repos/juju/environschema/issues/comments{/number}","contents_url":"https://api.github.com/repos/juju/environschema/contents/{+path}","compare_url":"https://api.github.com/repos/juju/environschema/compare/{base}...{head}","merges_url":"https://api.github.com/repos/juju/environschema/merges","archive_url":"https://api.github.com/repos/juju/environschema/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/juju/environschema/downloads","issues_url":"https://api.github.com/repos/juju/environschema/issues{/number}","pulls_url":"https://api.github.com/repos/juju/environschema/pulls{/number}","milestones_url":"https://api.github.com/repos/juju/environschema/milestones{/number}","notifications_url":"https://api.github.com/repos/juju/environschema/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/juju/environschema/labels{/name}","releases_url":"https://api.github.com/repos/juju/environschema/releases{/id}","created_at":"2015-06-02T10:10:40Z","updated_at":"2015-06-02T10:10:40Z","pushed_at":"2015-08-28T13:03:25Z","git_url":"git://github.com/juju/environschema.git","ssh_url":"git@github.com:juju/environschema.git","clone_url":"https://github.com/juju/environschema.git","svn_url":"https://github.com/juju/environschema","homepage":null,"size":214,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"v1"}},"_links":{"self":{"href":"https://api.github.com/repos/juju/environschema/pulls/6"},"html":{"href":"https://github.com/juju/environschema/pull/6"},"issue":{"href":"https://api.github.com/repos/juju/environschema/issues/6"},"comments":{"href":"https://api.github.com/repos/juju/environschema/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/juju/environschema/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/juju/environschema/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/juju/environschema/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/juju/environschema/statuses/a9820aebfdf419910b97e847503b46c140405f31"}}}}