From e0d5dbcf98a9c382b1b5f5bf896b909d208f5e6b Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Wed, 25 Jan 2023 21:11:45 +0000 Subject: [PATCH] added smartPrefix function --- render/render.go | 11 +++++++++++ render/render_test.go | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/render/render.go b/render/render.go index 974c173..2a0f9c7 100644 --- a/render/render.go +++ b/render/render.go @@ -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 @@ -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 +} diff --git a/render/render_test.go b/render/render_test.go index 33acd1b..f59e061 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -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") + +}