Skip to content

feat(goctl): templatex adds more built-in functions to make template … - #5122

Open
jaronnie wants to merge 1 commit into
zeromicro:masterfrom
jzero-io:goctl_templatex
Open

jaronnie wants to merge 1 commit into
zeromicro:masterfrom
jzero-io:goctl_templatex

Conversation

@jaronnie

Copy link
Copy Markdown
Contributor

templatex adds more built-in functions to make template features more playable

…features more playable

feat(goctl): templatex adds more built-in functions to make template features more playable
@codecov

codecov Bot commented Aug 27, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

// Execute returns the codes after the template executed
func (t *DefaultTemplate) Execute(data any) (*bytes.Buffer, error) {
tem, err := template.New(t.name).Parse(t.text)
tem, err := template.New(t.name).Funcs(sprig.TxtFuncMap()).Parse(t.text)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for this at the moment, but we'll import it if needed.

@kesonan kesonan added the do-not-merge/hold Indicates that a PR should not merge because of known issues or need more discussion. label Sep 6, 2025
@jaronnie

jaronnie commented Sep 6, 2025

Copy link
Copy Markdown
Contributor Author

It is very necessary. When I want to customize the template, I may need to perform some operations on the variables. If there are more built-in functions, we will have more room for operation on this template feature.

@jaronnie

jaronnie commented Sep 6, 2025

Copy link
Copy Markdown
Contributor Author

In many cases, we need to modify the default template. If there are more built-in functions, this will give the template features more gameplay.

@jaronnie

jaronnie commented Sep 6, 2025

Copy link
Copy Markdown
Contributor Author

e.g.
我可以让提供的变量变成小写 {{.Type | lower}},大写 {{.Type | upper}}, 可以截取字符串等等 // Strings

"abbrev":     abbrev,
"abbrevboth": abbrevboth,
"trunc":      trunc,
"trim":       strings.TrimSpace,
"upper":      strings.ToUpper,
"lower":      strings.ToLower,
"title":      strings.Title,
"untitle":    untitle,
"substr":     substring,
// Switch order so that "foo" | repeat 5
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
// Deprecated: Use trimAll.
"trimall": func(a, b string) string { return strings.Trim(b, a) },
// Switch order so that "$foo" | trimall "$"
"trimAll":      func(a, b string) string { return strings.Trim(b, a) },
"trimSuffix":   func(a, b string) string { return strings.TrimSuffix(b, a) },
"trimPrefix":   func(a, b string) string { return strings.TrimPrefix(b, a) },
"nospace":      util.DeleteWhiteSpace,
"initials":     initials,
"randAlphaNum": randAlphaNumeric,
"randAlpha":    randAlpha,
"randAscii":    randAscii,
"randNumeric":  randNumeric,
"swapcase":     util.SwapCase,
"shuffle":      xstrings.Shuffle,
"snakecase":    xstrings.ToSnakeCase,
// camelcase used to call xstrings.ToCamelCase, but that function had a breaking change in version
// 1.5 that moved it from upper camel case to lower camel case. This is a breaking change for sprig.
// A new xstrings.ToPascalCase function was added that provided upper camel case.
"camelcase": xstrings.ToPascalCase,
"kebabcase": xstrings.ToKebabCase,
"wrap":      func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith":  func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains":   func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix":  func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix":  func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote":      quote,
"squote":     squote,
"cat":        cat,
"indent":     indent,
"nindent":    nindent,
"replace":    replace,
"plural":     plural,
"sha1sum":    sha1sum,
"sha256sum":  sha256sum,
"sha512sum":  sha512sum,
"adler32sum": adler32sum,
"toString":   strval,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/hold Indicates that a PR should not merge because of known issues or need more discussion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants