Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate most of "templatelib" in favor of Sprig #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tianon
Copy link
Member

@tianon tianon commented Dec 22, 2020

It implements many of the same functions like first, last, ternary, etc, some just with a different name like toJson vs json, and gives us many more useful functions.

Closes #22

(This includes a workaround/implementation for Masterminds/sprig#276 which could hopefully be dropped in a future version. 🤞 😅)

@tianon
Copy link
Member Author

tianon commented Dec 22, 2020

I have successfully tested this against all of https://github.com/docker-library/docs with no changes. 😅

@@ -68,55 +69,33 @@ func stringsModifierActionFactory(a func(string, string) string) func([]string,
}
}

var FuncMap = template.FuncMap{
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I could get fancy with github.com/imdario/mergo to avoid changing the signature of templatelib.FuncMap, but I'm not aware of any actual users of it, so I'm not sure it's worth going to the trouble? 😇

(I'd love to convert our uses of functions like trimSuffixes, trimPrefixes, join, and getenv to be compatible with their Sprig counterparts so we could deprecate/remove our custom versions too, which shouldn't be too much work.)

Copy link
Member Author

Choose a reason for hiding this comment

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

It implements many of the same functions like `first`, `last`, `ternary`, etc, some just with a different name like `toJson` vs `json`, and gives us *many* more useful functions.
// turns: git://github.com/jsmith/some-repo.git
// into: https://github.com/jsmith/some-repo
funcMap["trimPrefixes"] = stringsActionFactory("trimPrefixes", false, stringsModifierActionFactory(strings.TrimPrefix))
funcMap["trimSuffixes"] = stringsActionFactory("trimSuffixes", false, stringsModifierActionFactory(strings.TrimSuffix))
Copy link
Member Author

Choose a reason for hiding this comment

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

These support a list of prefixes/suffixes, but I'm reasonably certain we've never actually used that functionality, so all our uses should be able to port to trimPrefix and trimSuffix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just confirmed, trimPrefixes is something we've never actually used, and trimSuffixes is literally only used with .git as a single argument. 😅

// Everybody: {{- join ", " .Names -}}
// Concat: {{- join "/" "https://github.com" "jsmith" "some-repo" -}}
funcMap["join"] = stringsActionFactory("join", true, strings.Join)
// (this differs slightly from the Sprig "join" in that it accepts either a list of strings or multiple arguments - Sprig instead has an explicit "list" function which can create a list of strings *from* a list of arguments so that multiple-signature usability like this is not necessary)
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess in looking at https://masterminds.github.io/sprig/string_slice.html, the key here will be to verify that we can start porting to the list ... | join ... syntax with this implementation so that at some point we can drop this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using sprig
1 participant