Skip to content

Commit

Permalink
added smartPrefix function
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Jan 25, 2023
1 parent 35e084d commit e0d5dbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions render/render.go
Expand Up @@ -26,6 +26,7 @@ func Render(template string, def parser.Definition, params map[string]interface{
ctx.Set("format_comment_text", formatCommentText)
ctx.Set("format_comment_html", formatCommentHTML)
ctx.Set("format_tags", formatTags)
ctx.Set("smart_prefix", smartPrefix)
s, err := plush.Render(string(template), ctx)
if err != nil {
return "", err
Expand Down Expand Up @@ -80,3 +81,13 @@ func formatTags(tags ...string) (template.HTML, error) {
tagsStr = "`" + tagsStr + "`"
return template.HTML(tagsStr), nil
}

// smartPrefix prepends a string before s, allowing for the specific use
// case of pointers to objects. If the s begins with * (as in, *Object), the
// result will be *prefixObject to preserve its original meaning.
func smartPrefix(prefix, s string) string {
if strings.HasPrefix(s, "*") {
return "*" + prefix + s[1:]
}
return prefix + s
}
11 changes: 11 additions & 0 deletions render/render_test.go
Expand Up @@ -112,3 +112,14 @@ func TestFormatCommentText(t *testing.T) {
is.Equal(actual, `// What about new lines?`)

}

func TestSmartPrefix(t *testing.T) {
is := is.New(t)

actual := smartPrefix("public", "*Object")
is.Equal(actual, "*publicObject")

actual = smartPrefix("public", "Object")
is.Equal(actual, "publicObject")

}

0 comments on commit e0d5dbc

Please sign in to comment.