Skip to content

Commit

Permalink
Support to render data (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Jun 16, 2022
1 parent 153a480 commit 31ffcfe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Flags:
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| `printToc` | `{{printToc}}` | Print the [TOC](https://en.wikipedia.org/wiki/TOC) of the template file |
| `printContributors` | `{{printContributors "linuxsuren" "yaml-readme"}}` | Print all the contributors of an repository |
| `printStarHistory` | `{{printStarHistory "linuxsuren" "yaml-readme"}}` | Print the star history of an repository ` |
| `printVisitorCount` | `{{printVisitorCount "repo-id"}}` | Print the visitor count chart of an repository ` |
| `printStarHistory` | `{{printStarHistory "linuxsuren" "yaml-readme"}}` | Print the star history of an repository |
| `printVisitorCount` | `{{printVisitorCount "repo-id"}}` | Print the visitor count chart of an repository |
| `render` | `{{render true}}` | Make the value be readable, turn `true` to `:white_check_mark:` |

### Ignore particular items

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/h2non/gock v1.0.9
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)

require (
Expand All @@ -14,5 +15,4 @@ require (
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func getFuncMap(readmeTpl string) template.FuncMap {
"printVisitorCount": func(id string) string {
return fmt.Sprintf(`![Visitor Count](https://profile-counter.glitch.me/%s/count.svg)`, id)
},
"render": dataRender,
}
}

Expand Down Expand Up @@ -284,6 +285,20 @@ func printStarHistory(owner, repo string) string {
owner, repo)
}

func dataRender(data interface{}) string {
switch val := data.(type) {
case bool:
if val {
return ":white_check_mark:"
} else {
return ":x:"
}
case string:
return val
}
return ""
}

var contributorsTpl = `{{- range $i, $val := .}}
<td align="center">
<a href="{{$val.html_url}}">
Expand Down
34 changes: 34 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,37 @@ func Test_getFuncMap(t *testing.T) {
assert.Contains(t, buf.String(), k)
}
}

func Test_dataRender(t *testing.T) {
type args struct {
data interface{}
}
tests := []struct {
name string
args args
want string
}{{
name: "bool type with true value",
args: args{
data: true,
},
want: ":white_check_mark:",
}, {
name: "bool type with false value",
args: args{
data: false,
},
want: ":x:",
}, {
name: "normal string value fake",
args: args {
data:"fake",
},
want:"fake",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, dataRender(tt.args.data), "dataRender(%v)", tt.args.data)
})
}
}

0 comments on commit 31ffcfe

Please sign in to comment.